Mysql to mysqli

How can I switch from MYSQL to MYSQLI? Because I tried, but it returns a null value. Thanks in advance!

MYSQL Code:

if(isset($_POST["username"])&& isset($_POST["password"])){

    $manager = preg_replace('#[^A-Za-z0-9]#i','',$_POST["username"]);
    $password = preg_replace('#[^A-Za-z0-9]#i','',$_POST["password"]);
    include "includes/connect_db.php";
    $sql = mysql_query("SELECT id FROM admin WHERE username='$manager'
        AND password=$password' LIMIT 1");

    $existCount = mysql_num_rows($sql);//count the row nums
    if($existCount == 1){//evaluate the count
        while($row=mysql_fetch_array($sql)){
            $id=$row["id"];
        }
        $_SESSION["id"] = $id;
        $_SESSION["manager"]=$manager;
        $_SESSION["password"]=$password;
        header("location:index.php");
        exit();

    }else{
        echo 'That information is incorrect, try again <a href="index.php">Click   Here</a>';
        exit();
    }

This is what I tried before, but will show me a message stating that mysqli_num_rows has a logical result.

MYSQLI Code:

if(isset($_POST["username"])&& isset($_POST["password"])){

   $manager = preg_replace('#[^A-Za-z0-9]#i','',$_POST["username"]);
   $password = preg_replace('#[^A-Za-z0-9]#i','',$_POST["password"]); 
   include("includes/insert_db.php"); // i change the database connection
   $sql = "SELECT * FROM admin WHERE username='$manager' 
   AND password=$password'";

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

   $existCount = mysql_num_rows($result);//count the row nums
   if($existCount == 1){//evaluate the count
   while($row=mysqli_fetch_array($result)){
    $id=$row["id"];
   }
   $_SESSION["id"] = $id;
   $_SESSION["manager"]=$manager;
   $_SESSION["password"]=$password;
   header("location:index.php");
   exit();

  }else{
    echo 'That information is incorrect, try again <a href="index.php">Click Here</a>';
    exit(); 
  }
 }
+4
source share
2 answers

try changing this:

$sql = mysql_query("SELECT id FROM admin WHERE username='$manager' 
AND password=$password' LIMIT 1");

:

$sql = "SELECT id FROM admin WHERE username='$manager' AND password='$password' LIMIT 1";

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

or that:

$sql = "SELECT id FROM admin WHERE username='$manager' AND password='$password' LIMIT 1"; 
$result = $conn->query($sql); 

($ conn is a connection variable if it is different.) I hope this helps.

also change mysql_fetch_array($sql)tomysqli_fetch_array($sql)

+5
source

. php script, php mysql mysqli . , .

https://github.com/philip/MySQLConverterTool

-1

Source: https://habr.com/ru/post/1614406/


All Articles