tags:

views:

80

answers:

3

Is there an easy method to extract only one field. For instance:

$sql = "SELECT field1 FROM table";
$res = mysql_query($sql) or die(mysql_error());
$arr = mysql_fetch_assoc($res);
$field1 = $arr['field1'];

My feeling says this could be done much easier.

A: 

Maybe

$sql = "SELECT field1 FROM table";
$result = mysql_query($sql) or die(mysql_error());
$field1 = mysql_fetch_object($result)->field1;
Jake
If a method returns an array or object you cannot access it's members at the same time you call the method.
Unkwntech
+5  A: 

You can use mysql_result().

string mysql_result ( resource $result , int $row [, mixed $field= 0 ] )
Retrieves the contents of one cell from a MySQL result set.
VolkerK
Ah I already thought the guys at the mySQL company should have made an easier way to do this.
blub
A: 
$field1 = mysql_fetch_object(mysql_query("SELECT field1 FROM table"))->field1;
Saggi Malachi
Hmm this isn't very pretty... Those nested thingy's aren't very good for the readability of the code!
blub