Page 1 of 1
Variable in query [SOLVED]
Posted: Fri Aug 17, 2012 5:35 am
by robertb13
another one that's killing me.
This work perfectly...
$query = ("select * FROM survey_questions WHERE survey_id=1");
I want to substitute the "1" for a variable
$survID=1
I've echoed the variable and confirmed it outputs "1"
tried:
$query = ("select * FROM survey_questions WHERE survey_id='" . $survID . "'");
$query = ("select * FROM survey_questions WHERE survey_id=' '$survID''");
and about 10 other combos.
I'm at a loss...
Thanks for your help...
Re: variable in query
Posted: Fri Aug 17, 2012 9:10 am
by social_experiment
This should do the trick;
Code: Select all
<?php
$query = "select * from `survey_questions` where `survey_id` = '" . $survID . "' ";
?>

Use the PHP Code button to wrap your php code in; makes it easier to read

Re: variable in query
Posted: Fri Aug 17, 2012 1:14 pm
by robertb13
I copied and pasted your code exactly.
It did not work.
http://www.robertbartz.com/survey/time_spent.php
You can see that the echo $survID; returned "1"
(The echo 34; I change each edit to make sure the page is refreshing)
Code: Select all
<?php session_start(); ?>
<?php
$survID = $_SESSION["surveyid"];
/* ################################ */
/* ###### Sandbox Document ######## */
/* ################################ */
/* ################################ */
/* #### Get Survey Questions ###### */
/* ################################ */
echo $survID;?>
<br />
<?php echo 34;
function getQuestions($dbc) {
$query = "select * from `survey_questions` where `survey_id` = '" . $survID . "' ";
$result = mysqli_query($dbc, $query);
Re: variable in query
Posted: Fri Aug 17, 2012 1:47 pm
by califdon
If, as you said in your original post, the query works when you use the literal "1" (without quotes), then simply substitute the variable within the double-quoted $query string:
Code: Select all
$query = ("select * FROM survey_questions WHERE survey_id=$survID");
(Within a double-quoted string, PHP substitutes simple variables exactly. In this case, your database field is evidently an integer, so no quotes are required.)
*Solved* Re: variable in query
Posted: Fri Aug 17, 2012 3:04 pm
by robertb13
I figured it out...
The query was within a function so I needed to set the variable $_SESSION values within the function.
Thanks!!