Why DB UPDATE is simpler than SELECT?

Codes here !

Moderators: macek, egami, gesf

kscislav
New php-forum User
New php-forum User
Posts: 3
Joined: Wed Apr 09, 2014 10:54 pm

Why DB UPDATE is simpler than SELECT?

Postby kscislav » Wed Apr 09, 2014 11:03 pm

If I may ask some question about working code. This is short (NO question about it): $db=new PDO('mysql:host=localhost;dbname=test;charset=utf8', '$usr', '$pass'); $db->exec("UPDATE CNT SET cn=cn+1");

BUT! Question: why do I need not one but several lines:

Code: Select all

$rs=$db->query("SELECT cn FROM CNT");
$row = $rs->fetch(PDO::FETCH_ASSOC);
$count = $row['cn'];
echo $count;

Why not just?:

Code: Select all

$rs=$db->query("SELECT cn FROM CNT");echo $rs;


...SHOULD I use assoc,fetch,pdo::?

PS: I have a MySQL database
of 1 table
which consists of 1 column
and that one column consists of one counter (one row)
=> one single cell.

Is lots of code obligatory?

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Why DB UPDATE is simpler than SELECT?

Postby seandisanti » Wed Apr 09, 2014 11:28 pm

you could just do:

Code: Select all


$rs
=$db->query("SELECT cn FROM CNT",PDO::FETCH_ASSOC); //so you can reference results by field name
print_r($rs->fetch()); // to output array of associative values.
 

but the thing is that you want to add in error checking so that you can respond to errors and results that have no rows.

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Why DB UPDATE is simpler than SELECT?

Postby seandisanti » Wed Apr 09, 2014 11:29 pm

also, to find out the number of rows returned, you can just echo $rs->rowCount() if the query was successful.

kscislav
New php-forum User
New php-forum User
Posts: 3
Joined: Wed Apr 09, 2014 10:54 pm

Re: Why DB UPDATE is simpler than SELECT?

Postby kscislav » Thu Apr 10, 2014 12:50 am

Code: Select all

mysql> SELECT * FROM CNT;
+-------+
| cn    |
+-------+
| 20    |
+-------+


One db, one column, one row, one cell. Cell increments.
No authorization. No array (because 1 single value, updating).

Thank you for the explanation.

kscislav
New php-forum User
New php-forum User
Posts: 3
Joined: Wed Apr 09, 2014 10:54 pm

Re: Why DB UPDATE is simpler than SELECT?

Postby kscislav » Thu Apr 10, 2014 1:34 am

seandisanti wrote:[code=php]
$rs=$db->query("SELECT cn FROM CNT",PDO::FETCH_ASSOC);
print_r($rs->fetch());

Interestingly the page output isn't a number:
Array ( [cn] => 33 )


It seems that it thies to output the whole array of associated values after addressing a field name

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Why DB UPDATE is simpler than SELECT?

Postby seandisanti » Thu Apr 10, 2014 8:12 am

the FETCH_ASSOC dictates that the result of fetch() will be an associative array. If you want to access a single element of the array, it would be like:

Code: Select all


$rs
=$db->query("SELECT cn FROM CNT",PDO::FETCH_ASSOC);
$row $rs->fetch();
echo 
$row['cn'];
 


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 1 guest