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
https://forums.phpfreaks.com/topic/236690-php-variables-in-mysql-query/
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'";

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."'";
?>

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."'";

 

 

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 />';
}
?>

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.