Ryan
Ryan

Reputation: 12113

how to handle 1 value returned from sql in php

$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?

Upvotes: 0

Views: 87

Answers (3)

Reputation:

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.

Upvotes: 1

Quentin
Quentin

Reputation: 943108

There is mysql_fetch_field which appears to do what you want.

mysql_fetch_field(0,0);

Upvotes: 1

Paul Dixon
Paul Dixon

Reputation: 300825

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.

Upvotes: 1

Related Questions