Jump to content

PHP variables in MySQL query?


WebCheez

Recommended Posts

So, I'm working on a quiz system. The text and choices are stored in a MySQL database. I haven't gotten to the choices yet, I'm still having trouble with the text. Here's my code:

$querytext = "SELECT text FROM quiz id = '$prob'";
$result = mysql_query($querytext) or die(mysql_error());
echo $result;

Yes, I'm already connected and stuff, that's just the snippet. I made sure that $prob is 1.

Here's the MySQL Error I'm getting:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= ''' at line 1

I've looked all over the web and through the MySQL/PHP section of a 991-page PHP book. What am I doing wrong?

I bet that I really, really failed epicly this time, but I never catch my epic fails and have to ask questions about them in order to fix the problem. So please reply ;)

Link to comment
Share on other sites

You need to crawl before you walk and walk before you run. I suggest you look at some tutorials regarding MySQL queries.

 

The problem is you are trying to filter the records based upon the ID using a WHERE clause, but you forgot the "WHERE" part of the WHERE clause.

$querytext = "SELECT text
              FROM quiz
              WHERE id = '$prob'";

Link to comment
Share on other sites

So, I'm working on a quiz system. The text and choices are stored in a MySQL database. I haven't gotten to the choices yet, I'm still having trouble with the text. Here's my code:

$querytext = "SELECT text FROM quiz id = '$prob'";
$result = mysql_query($querytext) or die(mysql_error());
echo $result;

Yes, I'm already connected and stuff, that's just the snippet. I made sure that $prob is 1.

Here's the MySQL Error I'm getting:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= ''' at line 1

I've looked all over the web and through the MySQL/PHP section of a 991-page PHP book. What am I doing wrong?

I bet that I really, really failed epicly this time, but I never catch my epic fails and have to ask questions about them in order to fix the problem. So please reply ;)

 

try building your queries like this:

<?php
$sql = "SELECT text FROM quiz WHERE id = '".$prob."'";
?>

Link to comment
Share on other sites

The problem is you are trying to filter the records based upon the ID using a WHERE clause, but you forgot the "WHERE" part of the WHERE clause.

$querytext = "SELECT text
              FROM quiz
              WHERE id = '$prob'";

The above sql will work too ?

 

I always use

 

$querytext = "SELECT text

              FROM quiz

              WHERE id = '".$prob."'";

 

 

Link to comment
Share on other sites

*jumps off a bridge*

Wow... that is the most epicly I have failed in my LIFE!

I knew you were supposed to put the WHERE there, I could have SWORN that I did...

but I didn't.

Thanks. ::)

 

P.S. Wow... while I was typing two people posted new replies.

Link to comment
Share on other sites

You should read a bit more before trying real stuff. Anyway, mysql_query() will return just an unusable [by itself] resource identifier. To convert that resource to real data, you need to fetch it into an array with one of mysql_fetch_*() functions.

 

<?php
$results = mysql_query("SELECT text FROM quiz WHERE id = '$prob'");
$values = mysql_fetch_assoc($results);

echo $values['text'];
?>

 

If your query would return more than 1 row, you would need to loop through the results:

<?php
$results = mysql_query("SELECT text FROM quiz");
while ($values = mysql_fetch_assoc($results)) {
     echo $values['text'] . '<br />';
}
?>

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.