为什么我收到mysql_fetch_assoc()错误?

时间:2014-01-18 16:29:27

标签: php

单击“提交”按钮时,所选行已成功删除,但也会显示以下错误:

  

警告:mysql_fetch_assoc().....

这是我的代码:

<html>
<head>
<script type="text/javascript" language="javascript">

function checkAll(formname, checktoggle)
{

var checkboxes = new Array();
    checkboxes = document.forms[formname].getElementsByTagName("input");

for (var i=0; i<checkboxes.length; i++)  {

    if (checkboxes[i].type == "checkbox")   {

      checkboxes[i].checked = checktoggle;

        }
    }
}

// For Delete Button

</script>
</head>
<body bgcolor='lightgray'>
<a onclick="checkAll('myform',true);" href="#">check all</a>
<br>
<a onclick="checkAll('myform',false);" href="#">uncheck all</a>

<?php

    if(isset($_POST['delete']))

{

$delete_id = $_POST['chk'];
$id = count($delete_id );

    if(count($id) > 0)
    {
    foreach ($delete_id as $id_d)
        {
    $sql = "DELETE FROM pm WHERE pm_id='$id_d'";
    $delete = mysql_query($sql);
        }
    }
    if($delete)
        {
    echo $id." Records deleted Successfully.";
        }
}

?>

<?php
echo "<form name='myform' method='post' action='delete.php'>";
echo "<table border='1'>";
    while($rows=mysql_fetch_assoc($sql))
    {
    echo "<tr><td><input type='checkbox' name='chk[]' value='$rows[pm_id]' /></td><td></td><td>$rows[subject]</td></tr>";
    }

echo "</table>";
echo "<p><input id='delete' type='submit' class='button' name='delete' value='Delete Selected Items'/></p>";
echo "</form>";

?>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

while( $rows = mysql_fetch_assoc($delete) )

mysql_fetch_assoc 参数:正在评估的结果资源。这个结果来自对mysql_query()的调用。

<强> PS。使用mysqli或pdo。不推荐使用mysql

答案 1 :(得分:0)

   while($rows=mysql_fetch_assoc($sql))

在上面一行中,您尝试获取字符串而不是结果资源。 你应该在$sql中拥有mysql结果资源 你错过了下面的步骤:

应该是这样的:

$sql = mysql_query("select * from table");

然后你可以像这样获取它:

while($rows=mysql_fetch_assoc($sql))

我觉得这很有道理!