Forum Moderators: coopster

Message Too Old, No Replies

Variable inside query from another query?

         

h3ktlk

5:42 pm on May 24, 2007 (gmt 0)

10+ Year Member



Is there a way where inside of this

$spaceq = mysql_query("SELECT SUM(IF(Session1_9 = 'Wild About the Zoo - 9:00',1,0)) AS total FROM Students");

can I substitue 'Wild About the Zoo - 9:00' with a variable assigned to that field?

jatar_k

6:00 pm on May 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



absolutely

I usually creat the query on one line and then put my call to mysql_query on a seperate line. This will make a big difference in debugging.

$mysearchcriteria = 'Wild About the Zoo - 9:00';
$q = "SELECT SUM(IF(Session1_9 = '" . $mysearchcriteria . "',1,0)) AS total FROM Students";
$spaceq = mysql_query($q);

then if you are debugging this you can always set it up like so to see if your query is being properly constructed

$mysearchcriteria = 'Wild About the Zoo - 9:00';
$q = "SELECT SUM(IF(Session1_9 = '" . $mysearchcriteria . "',1,0)) AS total FROM Students";
echo $q;
die();
$spaceq = mysql_query($q);

if you didn't want to execute it, you should also add or die statements to queries while developing, or you can create an error handler but the dies are easier

$spaceq = mysql_query($q) or die ('select query died: ' . mysql_error());

h3ktlk

6:13 pm on May 24, 2007 (gmt 0)

10+ Year Member



This is what I have

$row = mysql_fetch_object($query);
$class=$row['EventName'];
$spaceq = mysql_query("SELECT SUM(IF(Session1_9 = '".$class."',1,0)) AS total FROM Students");

it doesnt seem to be readin the $class string right as if i type in the amount it does it right but if i echo row['eventname'] its works

jatar_k

6:15 pm on May 24, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try changing the variable name of $class, it's a reserved word

[php.net...]

wsmeyer

9:44 pm on May 24, 2007 (gmt 0)

10+ Year Member



Check out <b>Subquery Syntax</b> in the MySQL manual. Basically they look like this:

SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);

From the title of the discussion it sounds like what you're looking for anyway.

William.