gpt4 book ai didi

php - Mysql到Mysqli

转载 作者:可可西里 更新时间:2023-11-01 08:11:08 24 4
gpt4 key购买 nike

如何从mysql切换到mysql i?因为我尝试了但是返回了一个空值。提前谢谢!
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有布尔结果。
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是连接变量,如果不同,请更改它。)
希望这有帮助。
也将 mysql_fetch_array($sql)更改为 mysqli_fetch_array($sql)

关于php - Mysql到Mysqli,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33517819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com