WebCheez Posted May 17, 2011 Share Posted May 17, 2011 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 17, 2011 Share Posted May 17, 2011 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'"; Quote Link to comment Share on other sites More sharing options...
The Letter E Posted May 17, 2011 Share Posted May 17, 2011 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."'"; ?> Quote Link to comment Share on other sites More sharing options...
Nuv Posted May 17, 2011 Share Posted May 17, 2011 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."'"; Quote Link to comment Share on other sites More sharing options...
WebCheez Posted May 17, 2011 Author Share Posted May 17, 2011 *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. Quote Link to comment Share on other sites More sharing options...
WebCheez Posted May 17, 2011 Author Share Posted May 17, 2011 Wait a minute... It outputs: Resource id #6 Quote Link to comment Share on other sites More sharing options...
Fadion Posted May 18, 2011 Share Posted May 18, 2011 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 />'; } ?> Quote Link to comment Share on other sites More sharing options...
WebCheez Posted May 28, 2011 Author Share Posted May 28, 2011 Sorry for the late reply, and thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.