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 Link to comment https://forums.phpfreaks.com/topic/153129-solved-ah-mysql-prob-by-php/ 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']; Link to comment https://forums.phpfreaks.com/topic/153129-solved-ah-mysql-prob-by-php/#findComment-804343 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? Link to comment https://forums.phpfreaks.com/topic/153129-solved-ah-mysql-prob-by-php/#findComment-804347 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. Link to comment https://forums.phpfreaks.com/topic/153129-solved-ah-mysql-prob-by-php/#findComment-804363 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']; Link to comment https://forums.phpfreaks.com/topic/153129-solved-ah-mysql-prob-by-php/#findComment-804367 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? Link to comment https://forums.phpfreaks.com/topic/153129-solved-ah-mysql-prob-by-php/#findComment-804434 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]; Link to comment https://forums.phpfreaks.com/topic/153129-solved-ah-mysql-prob-by-php/#findComment-804454 Share on other sites More sharing options...
DEVILofDARKNESS Posted April 8, 2009 Author Share Posted April 8, 2009 Ok, thanks I got it Link to comment https://forums.phpfreaks.com/topic/153129-solved-ah-mysql-prob-by-php/#findComment-804456 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.