Tutorial :how to handle 1 value returned from sql in php



Question:

$query = "SELECT val FROM table WHERE id = 1"; mysql_query($query);

This returns a single peice of data from the database. all the ways I know how to deal with this data are mysql_fetch_array() and mysql_fetch_assoc() is there a way to deal with 1 peice of data that doesn't involve pulling it into an array?


Solution:1

There is mysql_fetch_field which appears to do what you want.

mysql_fetch_field(0,0);  


Solution:2

Many wrapper libraries offer this, for example, with ADODb, it's simple

$value=$db->GetOne("select val from table where id=1");  

It's not hard to roll your own either.


Solution:3

You can use mysql_result() like this;

mysql_result(mysql_query($query), 0, 0);  

The second parameter selects the row, and the third is the field number.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »