"Select * from" Alternative?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

"Select * from" Alternative?

Post by Trahb »

I'm wondering, sorry if this is stupid (I'm sure it is), if there's an alternative to "Select * from table_name". I want just ONE entry from my table.
Here is my code as of now :

Code: Select all

$a = mysql_query("SELECT `names` FROM `table_$type` WHERE `id`='$id'") or die(mysql_error());
But it doesn't seem to work.. I need that single entry (I don't want an array because it seems like a waste) because the rest of the code executes only if "names" is not null.

Thanks for your help in advance, and a quick response would be really nice; I'd love to have this done tonight before I go to bed :)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: "Select * from" Alternative?

Post by requinix »

Looking at that query I would think you only get one row: because you ask for rows matching id=$id, and typically an "id" is unique.

So maybe you're asking about just getting that one "name" field instead of an entire row? Doesn't work that way.
When you executed mysql_query it buffered all the results into memory. The entire row (and all the rows even) have been read. Functions like mysql_fetch_array are just to help you go through the results - it's not like they're reading from the resultset directly.

There's nothing wasted by getting an array.

Code: Select all

$row = mysql_fetch_array($a);
if ($row) {
    // found a row
    $name = $row["name"];
} else {
    // didn't find a matching row
}
Trahb
Forum Commoner
Posts: 36
Joined: Sat Jan 30, 2010 9:09 pm

Re: "Select * from" Alternative?

Post by Trahb »

Ah crud, thanks, I always thought there was--not exactly sure where I got that impression though. Thanks for clearing things up though!
Post Reply