Jump to content

[SOLVED] Ah! mysql prob, by php


DEVILofDARKNESS

Recommended Posts

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

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.

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'];

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.