DEVILofDARKNESS Posted April 8, 2009 Share Posted April 8, 2009 ah, I've got this piece of code: <?php session_start(); /*DATABASE SETTINGS */ $userid = $_SESSION['userid']; ?> <html> <head> <title>Cryptoworld</title> <meta name="author" content="Kruptein"> </head> <body> <table border="1"> <tr> <td>Challenge id</td><td>Challenge name</td><td>Solved?</td></tr> <?php $query = "SELECT * FROM crypto"; $result = mysql_query($query); while($challenge = mysql_fetch_array($result)) { $chal = $challenge['crypto_id']; $query = "SELECT solved_id FROM solved WHERE user_id = '$userid' AND $crypto_id='$chal'"; $result = mysql_query($query); list($solved) = mysql_query($query); if(isset($solved)) { $solv = "Yes!"; }else{ $solv = "No!"; } echo "<tr><td>" . $challenge['crypto_id'] . "</td><td>" . $challenge['crypto_name'] . "</td><td>" . $solv . "</td></tr>"; }?> </table> </body> </html> And if I run it it says that there is a problem with my mysql on line 21, which is this code: $query = "SELECT * FROM crypto"; $result = mysql_query($query); while($challenge = mysql_fetch_array($result)) { but this query worked fine until I added to the code the following: $chal = $challenge['crypto_id']; $query = "SELECT solved_id FROM solved WHERE user_id = '$userid' AND $crypto_id='$chal'"; $result = mysql_query($query); list($solved) = mysql_query($query); if(isset($solved)) { $solv = "Yes!"; }else{ $solv = "No!"; } Can somebody tell me what I've done wrong? Btw: the first row from the table crypto is shown :-s Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 Replace this: $result = mysql_query($query); list($solved) = mysql_query($query); With this... $result = mysql_query($query); $row = mysql_fetch_assoc($result); $solved=$row['solved_id']; Quote Link to comment Share on other sites More sharing options...
DEVILofDARKNESS Posted April 8, 2009 Author Share Posted April 8, 2009 Thanks! Can you explain what exactly the difference makes? I know what happens but what is assoc? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 Using the list() method you use a different mysql function call to return the data. I never do it this way and can't remember what the function is so I've had to change it to how I do it. Using mysql_fetch_assoc() will return the data back into an array (in this case $row) allowing you to reference the results from your query in an array manner. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 Example: $query=mysql_query("SELECT name,age,height FROM table WHERE clause"); If we use $row=mysql_fetch_assoc($query)); We can get at the data by treating $row as an array and use the field names to access them: echo $row['name']; echo $row['age']; echo $row['height']; Quote Link to comment Share on other sites More sharing options...
DEVILofDARKNESS Posted April 8, 2009 Author Share Posted April 8, 2009 Ah, I've got it,! and what is the different between fetch_array and fetch_assoc? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted April 8, 2009 Share Posted April 8, 2009 mysql_fetch_array() will return the array with a numerical index where mysql_fetch_assoc() returns the array with named indexes. In my above example with mysql_fetch_array() you'd use this instead: echo $row[0]; echo $row[1]; echo $row[2]; Quote Link to comment Share on other sites More sharing options...
DEVILofDARKNESS Posted April 8, 2009 Author Share Posted April 8, 2009 Ok, thanks I got it 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.