簡體   English   中英

PHP中的購物車(mysql_num_rows)

[英]Shopping cart in php (mysql_num_rows)

我正在嘗試用php做購物車。

我收到以下錯誤

“警告:mysql_num_rows()期望參數1為資源,在第19行的C:\\ wamp \\ www \\ shoppingCart \\ cart.php中給出的字符串”,並且返回沒有產品。

代碼中的錯誤在第19行:

if(mysql_num_rows($get)==0)[
<?php
  session_start();
  $page='index.php';
  $server="localhost";
  $username="root";
  $password="";
  $database="jennifer_db";
  $conn = new mysqli($server, $username, $password, $database);

  if($conn->connect_error){
    die("Connection failed: " .$conn->Connect_error);
  }

  function products(){
    $get='mysql_query("select Product_ID, Product_name, Product_desc,
        Product_price from products where quantity>0 order by id desc")';

    if(mysql_num_rows($get)== 0){
      echo "There ae no products to display!";
    }
    else{
    echo "Success!";
    }
  }
?>

您正在混合mysqlimysql函數。 請使用mysqliPDO

我在您的代碼中做了一些修改(我使用mysqli),我認為它應該可以工作,請查看注釋:

session_start();

$page = 'index.php';
$server = 'localhost';
$username = 'root';
$password = '';
$database = 'jennifer_db';

// this is the connection object
$mysqli = new mysqli($server, $username, $password, $database);

// if we have an error we abort
if ($mysqli->connect_errno)
  die('Connection failed: ' . $mysqli->connect_error);


function products(&$conn)
{
    // we pass $conn to this function and make the query
    // note that only the SQL query string should be in ""
    $result = $conn->query("select Product_ID, Product_name, Product_desc, Product_price from products where quantity > 0 order by id desc");

    if ($result)
    {
        // checking row count
        echo (!$result->num_rows ? 'There are no products to display!' : 'Success');

        // we close the result set
        $result->close();
    }
    else 
        echo "Error: {$conn->error}";
}

// call products() here
products($mysqli);

// we close the connection
$conn->close();

有關mysqli的更多信息,請訪問http://php.net/manual/zh/book.mysqli.php

還要簽出PDO並確定要使用哪一個(mysqli或PDO) http://php.net/manual/zh/book.pdo.php

我想您也可以從中學到: http : //davidwalsh.name/php-shorthand-if-else-ternary-operators

希望我能幫上忙。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM