Re: select statement with variables ???

From: Date: Wed, 21 Dec 2005 14:17:17 +0000
Subject: Re: select statement with variables ???
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-227771@lists.php.net to get a copy of this message
<side-question> Jay how come you though concating would give a different result to interpolation? </side-question> Jay Blanchard wrote:
[snip]
$query=" SELECT title FROM $cat WHERE id='$id'";
[/snip] echo $query; // does it look right to you? Alway throw an error when in question if(!($result = mysql_query($query, $connection))){ echo mysql_error() . "<br>\n"; exit(); }
up to here I agree with Jay 100%, especially the 'echo' part (also get familiar with var_dump() and print_r() functions to help debug your problems...
My bet is that you need to concatenate $query = "SELECT title FROM " . $cat . " WHERE id = '". $id ."' ";
now unless either $cat or $id is actually an object with a 'magic' __toString() method defined and the engine has been changed to fully/properly support 'magic' object2string casting I don't agree that concat'ing will help (even all of what I sAid was true I don't think it would help either), the reaosn being that AFAICT the following 2 statements leave you with the same string: $cat = "mytable"; $id = 1234; $one = "SELECT title FROM $cat WHERE id='$id'"; $two = "SELECT title FROM ".$cat." WHERE id = '".$id."'"; var_dump( ($one === $two) ); // <-- will show you that this equates to TRUE.

« previous php.general (#227771) next »