blub
blub

Reputation: 2166

mysql fetching only one field

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.

Upvotes: 2

Views: 4041

Answers (3)

VolkerK
VolkerK

Reputation: 96159

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.

Upvotes: 6

grateful.dev
grateful.dev

Reputation: 1437

$field1 = mysql_fetch_object(mysql_query("SELECT field1 FROM table"))->field1;

Upvotes: 0

Jake
Jake

Reputation: 242

Maybe

$sql = "SELECT field1 FROM table";
$result = mysql_query($sql) or die(mysql_error());
$field1 = mysql_fetch_object($result)->field1;

Upvotes: 0

Related Questions