0

我有产品名称并想选择下载路径。据它说,我得到一个错误

警告:mysql_num_rows() 期望参数 1 是资源,布尔值在...

好像没有这样的入口。

<?php
$keyarray=array('num_cart_items'=>2, 'item_name2'=>'5', 'item_name1'=>'6' );
$itemname='';
for ($i=1; $i<= $keyarray['num_cart_items'] ;$i++){
        $itemname[]= $keyarray['item_name'.$i];};

    foreach($itemname as & $var){  
    echo $var; 
    $sql = mysql_query("SELECT * FROM products WHERE product_name='$var' ");
    $productCount = mysql_num_rows($sql); // count the output amount
    $checkout_path ="";
    if ($productCount > 0) {

        // get path
        $row = mysql_fetch_array($sql);
                 $path = $row["path"];
                 $checkout_path[]=array('path'=>$path);

          echo $checkout_path;
       }
    }
    //echo  $checkout_path;
?>
4

2 回答 2

0

At the beginning of the script you have declared $itemname=''; as a string and then later on used it as an array $itemname[]= $keyarray['item_name'.$i];};.

Use $itemname=array(); to delare it as an array.

And use either PDO or mysqli, as mysql is deprecated and is not supported in the later versions

于 2013-10-10T05:38:17.937 回答
0

如果在运行查询后出现错误,请先查看

echo mysql_errno() . ": " . mysql_error() . "\n";

同样关于结果,您可能有多个结果,因此您需要进行 foreach 循环以对所有结果进行 grt,例如:

while ($row = mysql_fetch_array($sql)) {

}

另一个注意事项阻止使用mysql_*函数,因为:

此扩展自 PHP 5.5.0 起已弃用,并将在未来删除。相反,应该使用 MySQLi 或 PDO_MySQL 扩展。

于 2013-10-10T05:27:22.953 回答