简体   繁体   中英

How to replace mysql_result in PHP 7.2?

I have upgraded my site from PHP 5.6 to 7.2 and I made the switch in the code from mysql to mysqli, unfortunately mysql_result function is removed from 7.2, so after I searched here I found a replacement but unfortunately it doesn't work. Before the upgrade my table was populated, but after the upgrade the table is empty?!

Here is the code before the upgrade:

if (isset($_POST['submit'])){

   $sql = "SELECT id FROM users
   WHERE user='{$_POST['user']}'
   AND pass='{$_POST['passwd']}'";
   $result = mysql_query($sql);

   if (mysql_num_rows($result) == 0){
     header("Location: index.php?badlogin=");
     exit;
   }

   $_SESSION['sess_id_tid'] = mysql_result($result, 0); 
   $_SESSION['sess_user_tid'] = $_POST['user'];

   header("Location: tid.php");
   exit;
}

This how I changed the code after the upgrade to 7.2:

if (isset($_POST['submit'])){

   $sql = "SELECT id FROM users WHERE user='{$_POST['user']}' AND pass='{$_POST['passwd']}'";
   $result = mysqli_query($conn, $sql);


   if (mysqli_num_rows($result) == 0){
     header("Location: index.php?badlogin=");
     exit;
   }


   function mysqli_result($res,$row=0,$col=0){
     $numrows = mysqli_num_rows($res);
     if ($numrows && $row <= ($numrows-1) && $row >=0){
       mysqli_data_seek($res,$row);
       $resrow = (is_numeric($col)) ? mysqli_fetch_row($res) : mysqli_fetch_assoc($res);
       if (isset($resrow[$col])){
         return $resrow[$col];
       }
     }
     return false;
   }

   $_SESSION['sess_id_tid'] = mysqli_result($result, 0, 0);
   $_SESSION['sess_user_tid'] = $_POST['user'];

   header("Location: tid.php");
   exit;

}

What did I miss?

Update: Thanks @Phil & @Madhuri Patel,

Here is an image of the problem I'm facing:

Image of the tables before PHP 5.6 and after PHP 7.2

mysql_query returns you a mysqli_result object. From there, you can get the value you want.

if($query_run = mysqli_query($link, $query)) {
    // This gets you one row at a time, use a while if there are multiple rows
    // while($row = mysqli_fetch_assoc($query_run)){}
    $row = mysqli_fetch_assoc($query_run);
    $count = $row['count'];
    // Do whatever with $count
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM