mysqi_ *给出错误mysql_ * didn&#t; t

时间:2014-11-28 21:14:07

标签: php mysqli

我是php新手,我正在将脚本从使用mysql_ *更改为mysqli_ *函数。切换后我收到以下错误:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in cart.php on line 115

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in cart.php on line 115

我的代码

<title>Cart</title>
<?php
    require("../mysqli_connect.php");
?>
</head>
<body>
<?php

    $product_id = $_GET[id];     //the product id from the URL 
    $action     = $_GET[action]; //the action from the URL 

    //if there is an product_id and that product_id doesn't exist display an error message
    if($product_id && !productExists($product_id)) {
        die("Error. Product Doesn't Exist");
    }

    switch($action) {   //decide what to do 

        case "add":
            $_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id 
        break;

        case "remove":
            $_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id 
            if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items. 
        break;

        case "empty":
            unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 
        break;

    }

?>


<?php   

    if($_SESSION['cart']) { //if the cart isn't empty
        //show the cart

        echo "<table border=\"1\" padding=\"3\" width=\"40%\">";    //format the cart using a HTML table

            //iterate through the cart, the $product_id is the key and $quantity is the value
            foreach($_SESSION['cart'] as $product_id => $quantity) {    

                //get the name, description and price from the database - this will depend on your database implementation.
                //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
                $sql = sprintf("SELECT name, description, price FROM products WHERE id = %d;",
                                $product_id); 

                $result = mysqli_query($dbc, $sql);

                //Only display the row if there is a product (though there should always be as we have already checked)
                if(mysqli_num_rows($result) > 0) {

                    list($name, $description, $price) = mysqli_fetch_row($result);

                    $line_cost = $price * $quantity;        //work out the line cost
                    $total = $total + $line_cost;           //add to the total cost

                    echo "<tr>";
                        //show this information in table cells
                        echo "<td align=\"center\">$name</td>";
                        //along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product
                        echo "<td align=\"center\">$quantity <a href=\"$_SERVER[PHP_SELF]?action=remove&id=$product_id\">X</a></td>";
                        echo "<td align=\"center\">$line_cost</td>";

                    echo "</tr>";

                }

            }

            //show the total
            echo "<tr>";
                echo "<td colspan=\"2\" align=\"right\">Total</td>";
                echo "<td align=\"right\">$total</td>";
            echo "</tr>";

            //show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation
            echo "<tr>";
                echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[PHP_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>";
            echo "</tr>";       
        echo "</table>";



    }else{
        //otherwise tell the user they have no items in their cart
        echo "You have no items in your shopping cart.";

    }

    //function to check if a product exists
    function productExists($product_id) {
            //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
            $sql = sprintf("SELECT * FROM products WHERE id = %d;",
                            $product_id); 

            return mysqli_num_rows(mysqli_query($dbc, $sql)) > 0; ***//this is line 15***
    }
?>

<a href="products.php">Continue Shopping</a>


<?php

/*

products table:
    CREATE TABLE `products` (
        `id` INT NOT NULL AUTO_INCREMENT ,
        `name` VARCHAR( 255 ) NOT NULL ,
        `description` TEXT,
        `price` DOUBLE DEFAULT '0.00' NOT NULL ,
        PRIMARY KEY ( `id` )
    );

*/

?>



</body>
</html>

这是产品页面。

<title>Products</title>   
<?php
    require("../mysqli_connect.php");
?>
</head>
<body>
<table border="1">

    <?php

        $sql = "SELECT id, name, description, price FROM products;";

        $result = mysqli_query($dbc, $sql);

        while(list($id, $name, $description, $price) = mysqli_fetch_row($result)) {

            echo "<tr>";

                echo "<td>$name</td>";
                echo "<td>$description</td>";
                echo "<td>$price</td>";
                echo "<td><a href=\"cart.php?action=add&id=$id\">Add To Cart</a></td>";

            echo "</tr>";
        }

    ?>
</table>
<a href="cart.php">View Cart</a>
</body>
</html>

数据库连接

DEFINE ('DB_USER', 'username');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'domain_user');

// Make the connection:
$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );

// Set the encoding...
mysqli_set_charset($dbc, 'utf8');

1 个答案:

答案 0 :(得分:0)

您的连接未进入该功能。快速解决方法是在函数中标记变量GLOBAL。

function productExists($product_id) {
    GLOBAL $dbc;

    $sql = sprintf("SELECT * FROM products WHERE id = %d;", $product_id); 
     return mysqli_num_rows(mysqli_query($dbc, $sql)) > 0;
}

这种方式$dbc在您的函数中可用。

你可能想帮助让它更具可读性(至少在我看来......)

function productExists($product_id) {
    GLOBAL $dbc;

    $sql    = sprintf("SELECT * FROM products WHERE id = %d;", $product_id); 
    $result = mysqli_query($dbc, $sql) or die(mysqli_error($dbc));
    $rows   = mysqli_num_rows($result);

    return $rows;
}

我发现更容易阅读两行代替将函数嵌套到一行。