Getting just one value ?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Death
Forum Newbie
Posts: 17
Joined: Tue Apr 19, 2005 11:52 am

Getting just one value ?

Post by Death »

Hi guys,

Sometimes I need just one value.

Like so:

Code: Select all

// $id is unique, no duplicates
$query=&quote;SELECT field02 FROM table01 WHERE field01=$id&quote;;

$result=mysql_query($query);
$row=mysql_fetch_array($result);

// here is the value I want
$myValue=$rowї&quote;field02&quote;];
So is all that necessary ? Maybe there is a faster way ?

Thanks.


feyd | Please review how to post code using

Code: Select all

and

Code: Select all

tags. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

If you set up your table where field01 is an "Index", it should run quite quickly. Have you done that?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

please start using the forum's [syntax=php]tags.[/syntax]
mudvein
Forum Commoner
Posts: 45
Joined: Wed Mar 16, 2005 4:39 pm

Post by mudvein »

you could use mysql_result() if you are just wanting to select 1 single result... ie: just 1 id #.

If you are returning multiple ID's in one query (say 5 or 10) then yeah, mysql_fetch_assoc (or array, whichever you prefer) is the better way to do this.
Death
Forum Newbie
Posts: 17
Joined: Tue Apr 19, 2005 11:52 am

Post by Death »

Hi guys,

No, I didn't set the ID field as an "index" but that sounds like something I might want to do, is that what it's called, an "index" ? I'm still getting the hang of MySQL.

I'll check out that mysql_result() function as well.

Thanks.

PS - I'll use the php tags from now on, sorry about that.
Post Reply