Variable in query [SOLVED]

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
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

Variable in query [SOLVED]

Post 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...
Last edited by robertb13 on Tue Aug 28, 2012 6:21 pm, edited 3 times in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: variable in query

Post by social_experiment »

This should do the trick;

Code: Select all

<?php
 $query = "select * from `survey_questions` where `survey_id` = '" . $survID . "' ";
?>
:idea: Use the PHP Code button to wrap your php code in; makes it easier to read :)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

Re: variable in query

Post 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);
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: variable in query

Post 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.)
Last edited by califdon on Fri Aug 17, 2012 1:50 pm, edited 1 time in total.
Reason: Added a small explanation.
robertb13
Forum Newbie
Posts: 22
Joined: Thu Aug 16, 2012 5:04 pm

*Solved* Re: variable in query

Post 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!!
Post Reply