armlocker Posted July 19, 2014 Share Posted July 19, 2014 Hi there, just trying to get some practice in by creating a simple php game - can someone tell me where am I going wrong please: Thanks in advance <?php $con = mysql_connect('localhost', 'root', ''); mysql_select_db("krish"); ?> QUESTION 1<br><br> How many bytes are used to represent the primitive...? <br><br> Choose the right answer:<br><br> <?php $query = mysql_query("SELECT * FROM questions WHERE question_id = 'Q1'"); while ($row = mysql_fetch_assoc($query)) { echo $row['option1'] . '<input type="radio" name="option_1" value="option1"><br>'; echo $row['option2'] . '<input type="radio" name="option_1" value="option2"><br>'; echo $row['option3'] . '<input type="radio" name="option_1" value="option3"><br>'; echo $row['option4'] . '<input type="radio" name="option_1" value="option4"><br>'; } $var = "select from answer questions where answer ='4'"; if (isset($var)) { echo 'correct answer'; } else { echo 'wrong answer'; } ?> Link to comment Share on other sites More sharing options...
Larry Posted July 20, 2014 Share Posted July 20, 2014 What you're doing wrong depends upon two things: your database structure and the actual results that aren't working. I would start by confirming the results of this query using a separate interface: SELECT * FROM questions WHERE question_id = 'Q1' If that doesn't make it obvious what's wrong, then please share what it's working or how it's not working. Link to comment Share on other sites More sharing options...
armlocker Posted July 20, 2014 Author Share Posted July 20, 2014 Hello again Yoda of PHP I have echoed the $sql; under the select query above and now I get the : Notice: Undefined variable: sql in C:\xampp\htdocs\game\index.php on line 16 my database (questions) structure is simple. here are the coloumns questions_record_number question_id question answer option1 option2 option3 option4 subject NOTHING IS WORKING AT THE MOMENT - AND HERES WHATS SHOWING =============================================================== QUESTION 1How many bytes are used to represent the primitive...? Choose the right answer:Notice: Undefined variable: sql in C:\xampp\htdocs\game\index.php on line 162481correct answer ============================================================== I know its something really simple - I just cant figure it out, and is an excellent exercise for getting results from the database, useful in many situations. .... over to you Sir Link to comment Share on other sites More sharing options...
Larry Posted July 20, 2014 Share Posted July 20, 2014 Your variable is called $query, not $sql. But you don't need to print out the variable's value, as you already know exactly what the value is. Link to comment Share on other sites More sharing options...
armlocker Posted July 20, 2014 Author Share Posted July 20, 2014 ... woops - my bad ... here's what is now showing after echo $query; =============================================================== QUESTION 1How many bytes are used to represent the primitive...? Choose the right answer:Resource id #42481correct answer Link to comment Share on other sites More sharing options...
HartleySan Posted August 3, 2014 Share Posted August 3, 2014 Please use the MySQLi (notice the "i") family of functions, not the old and out-of-date MySQL functions. After that, add the following code below where you set up your DB connection to make sure that it's okay: echo '<pre>'; print_r($con); echo '</pre>'; Link to comment Share on other sites More sharing options...
Recommended Posts