Mysql获取数组

时间:2011-09-13 13:16:57

标签: php mysql arrays fetch

我遇到了mysql_fetch_array的一些问题。 它说:

  

警告:mysql_fetch_assoc()要求参数1为资源,字符串在第49行的/home/yeyddhiw/public_html/v.php中给出

我真的不能看到我的问题。

我的代码是:

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

1 个答案:

答案 0 :(得分:0)

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

您从未对mysql_query的结果执行基本错误检查(如果发生错误,则为FALSE。)

最天真,这可能意味着:

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

执行此操作,您将看到一条错误消息,告知您SQL查询无效:最后有一个),不应该在那里。

BTW,$result2$result3是多余的,因为INSERT / DELETE语句无法向您返回数据。