Mysql转Mysqli [英] Mysql to Mysqli
本文介绍了Mysql转Mysqli的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从MYSQL切换到MYSQLI?因为我尝试过但返回的是空值.请先谢谢您!
How I can switch from MYSQL to MYSQLI? Because i tried but returns a null value.Thank you in advance!
MYSQL代码:
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();
}
这是我之前尝试过的操作,但向我显示一条消息mysqli_num_rows具有布尔结果.
This what i tried before but show me a message that mysqli_num_rows have boolean result.
MYSQLI代码:
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();
}
}
推荐答案
尝试更改此内容:
$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);
或者这个:
$sql = "SELECT id FROM admin WHERE username='$manager' AND password='$password' LIMIT 1";
$result = $conn->query($sql);
($ conn是连接变量,如果不同,则将其更改.) 希望这会有所帮助.
($conn is the connection variable change it if it different.) hope this helps out.
还将mysql_fetch_array($sql)
更改为mysqli_fetch_array($sql)
这篇关于Mysql转Mysqli的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文