Im having some problems with mysql_fetch_array. It says:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in /home/yeyddhiw/public_html/v.php on line 49

And i cant really see my problem.

My code is this:

<?php session_start(); ?>
<?php 
if ($_SESSION['username'])
{
if($_SESSION['class'] == 'mod')
{
}

else
{
    header("location:/");
    exit();
}

}
else
{
header("location:/");
exit();
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org    /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Funnyshit</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>

<body bgcolor="#171717">

<div id="head" align="center">
    <?php include 'head.php';?>
</div>

<div id="content" align="center">
<?php
if ($_GET['id'])
{
    include 'dbconnect_images.php';
    $id = $_GET['id'];
    $id = mysql_real_escape_string($id);


    $sql = "SELECT * FROM images_notvalid WHERE id='$id')";
    $result = mysql_query($sql);

    while($row = mysql_fetch_assoc($result))
    {
        echo $row['name'];
        echo $row['path'];
        echo $row['uploader'];
        $sql2 = "INSERT INTO images (name, path, uploader) VALUES ('$name', '$path', '$uploader')";
        $result2 = mysql_query($sql2);

        $delete = "DELETE FROM images_notvalid WHERE name='$name' AND uploader='$uploader'";
        $result3 = mysql_query($delete);
        mysql_close($conn);
        echo '<div class="error">Bildet er validert!</div>';
    }

}

else
{
    echo '<div class="error">Her mangler det data!</div>';
}
?>
</div>

link|improve this question

61% accept rate
Do the mysql_query statement return an error (maybe the table is images_not_valid)? – Eineki Sep 13 '11 at 13:19
3  
check your code - > $sql = "SELECT * FROM images_notvalid WHERE id='$id')"; It is wrong...remove ')' !! – Rikesh Shah Sep 13 '11 at 13:20
feedback

1 Answer

$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))

You never performed basic error checking on the result of mysql_query (which is FALSE if an error occured).

At its most naive, that might mean:

$result = mysql_query($sql);
if (!$result)
   die(mysql_error());
while($row = mysql_fetch_assoc($result))

Doing this, you will see an error message that tells you that your SQL query is invalid: it has an ) at the end that should not be there.

BTW, $result2 and $result3 are redundant since INSERT/DELETE statements can't return data to you.

link|improve this answer
i personally prefer the cleaner "or die(mysql_error());" after the query :) – Jan Dragsbaek Sep 13 '11 at 13:26
@Nayena: Your opinion statement includes the unfounded assertion that that is automatically cleaner, which I dispute (mainly because it's not as easy then to swap die for something that's actually useful, such as throw). – Lightness Races in Orbit Sep 13 '11 at 13:31
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.