获取警告“mysql_fetch_assoc():提供的参数不是有效的MySQL结果资源”

时间:2013-05-11 11:50:53

标签: php

我无法找到解决此问题的方法。我遇到了大问题我不知道解决了我会等待解决方案并谢谢你。是域名中的问题吗?

<?php

session_start();

require_once("../Connections/Store.php");

if(isset($_GET['page'])){

    $pages = array("products","cart");


if(in_array($_GET['page'],$pages)){


        $page=$_GET['page'];

        }else {

            $page="products";


    }

}else {



    $page="products";

}


?>


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

<link rel="stylesheet" href="css/reset.css" />
<link rel="stylesheet" href="css/style.css" />


<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

</head>

<body>
<div id="container">
<div id="main"><?php require($page. ".php"); ?></div>

<div id="sidebar"></div>

<?php

if(isset($_SESSION['cart'])){


    $sql .="SELECT * FROM products WHERE pro_id IN (";


foreach($_SESSION['cart'] as $id => $value){

        $sql .=$id . ",";


        }


        $sql = substr($sql,0,-1). ") ORDER BY pro_id ASC";


$query=mysql_query($sql);
        while($row=mysql_fetch_assoc($query)){




         ?>

         <p><?php echo $row['pro_name'];?><?php echo $_SESSION['cart'][$row['pro_id']]

['quantity'];?></p>
         <a href="index.php?page=cart">Go to Cart </a>

         <?php

            }



}else {

            echo"Your Cart is empty. <br> please add some product";

            }



         ?>




</div>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

您的查询未能使用mysql_error查看错误

$query=mysql_query($sql) or die(mysql_error());

从此处删除.

$sql .="SELECT * FROM products WHERE pro_id IN (";
    ^^

注意: Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial