错误:调用未定义函数mysql_query()

时间:2019-05-07 20:44:32

标签: php mysql sql mysqli

出什么问题了?

我想显示数据库中的数据,但是代码有问题

有人说mysql_quety在PHP 7中不起作用

好,那我该如何解决?

<?php 
    if (isset($_GET['id'])) {
        include('database_connection.php'); 
        $id = preg_replace('#[^0-9]#i', '', $_GET['id']); 
        $sql = mysql_query("SELECT * FROM shoes WHERE id='$id' LIMIT 1");
        $productCount = mysql_num_rows($sql); 
        if ($productCount > 0) {
            while($row = mysql_fetch_array($sql)){ 
                 $model_name = $row["model_name"];
                 $price = $row["price"];
                 $brand = $row["brand"];
                 $color = $row["color"];
                 $type = $row["type"];
             }

        } else {
            echo "That item does not exist.";
            exit();
        }

    } else {
        echo "Data to render this page is missing.";
        exit();
    }
    mysql_close();
    ?>

1 个答案:

答案 0 :(得分:0)

mysql在PHP 5.5.0中已弃用,在PHP 7.0.0中已将其删除。

所以改用mysqli

    $sql = mysqli_query($db_connection, "SELECT * FROM shoes WHERE id='$id' LIMIT 1");
    $productCount = mysqli_num_rows($sql); 
    if ($productCount > 0) {
        while($row = mysqli_fetch_array($sql)){ 
             $model_name = $row["model_name"];
             $price = $row["price"];
             $brand = $row["brand"];
             $color = $row["color"];
             $type = $row["type"];
         }

    }