-2

What is the problem?

I want to display data from database but something is wrong with code

Someone said that mysql_quety doesn't work in PHP 7

OK then How i can fix it?

<?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
    The [`ext/mysql`](https://www.php.net/manual/en/migration70.incompatible.php#migration70.incompatible.removed-functions.mysql) related functions were removed in PHP 7. You will need to use [`ext/mysqli`](https://php.net/manual/en/book.mysqli.php) or [`PDO_MYSQL`](https://www.php.net/manual/en/book.pdo.php) or otherwise use a [`shim`](https://github.com/dshafik/php7-mysql-shim) while you migrate your application to the appropriate functions. – Will B. May 07 '19 at 20:53

1 Answers1

0

mysql was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0.

So use mysqli instead

    $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"];
         }

    } 
Mike Volmar
  • 1,709
  • 18
  • 29
  • thanks but it doesn't work :( Warning: mysqli_query() expects at least 2 parameters, 1 given in D:\XAAMP\htdocs\ict\single.php on line 5 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in D:\XAAMP\htdocs\ict\single.php on line 6 – Kurier Сhannel May 07 '19 at 21:01
  • what can i do??? – Kurier Сhannel May 07 '19 at 21:03
  • It's worth adding a `prepare()` call here to deal with the nasty [SQL injection bug](http://bobby-tables.com/) as well. – tadman May 07 '19 at 21:13
  • tadman is correct. i also edited my code example for you to include the $db_connection – Mike Volmar May 07 '19 at 22:04