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
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.

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.