Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Most efficient way to select single row

Status
Not open for further replies.

Tokhra

Programmer
Joined
Oct 1, 2003
Messages
134
Location
ES
Hi all,

Whenever I see PHP code that selects a single row from the database they just use LIMIT 1 in the query and still use a WHILE loop to select the data, theres got to be a more efficient way? (i.e. without the loop)

Any examples?
Thanks,
Matt.
 
echo mysql_result($result, 0); // change 0 for the index of the result required

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Another way (any of the row fetching functions will do):
Code:
$row = mysql_fetch_assoc($result);

A thought:
Ideally fetching a single row should always cover all bases of possible errors:
- no result
- more than one result
With LIMIT 1 you'll never find out if there are duplicates (unless it's a unique key).
 
mysql_num_rows($result) is your friend :-)

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top