Page 1 of 1

"Select * from" Alternative?

Posted: Thu Apr 29, 2010 8:13 pm
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 :)

Re: "Select * from" Alternative?

Posted: Thu Apr 29, 2010 8:20 pm
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
}

Re: "Select * from" Alternative?

Posted: Thu Apr 29, 2010 8:23 pm
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!