本文介紹了PHP 將 mysql 轉(zhuǎn)換為 mysqli的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問題描述
以下腳本工作正常:
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
為什么,當(dāng)我僅用以下內(nèi)容替換 mysql_connect 和 mysql_select_db 語(yǔ)句(保持其他所有內(nèi)容,包括所有密碼相同)時(shí),是否會(huì)收到錯(cuò)誤的用戶名或密碼"回聲?
Why, when I replace only the mysql_connect and mysql_select_db statements (keeping everything else including all passwords the same) with the following, do i get the "Wrong Username or Password" echo?
mysqli_connect($host,$username,$password,$db_name) or die("cannot connect");
推薦答案
這是因?yàn)?MySQL 和 MySQLi 數(shù)據(jù)庫(kù)連接沒有鏈接.當(dāng)您使用 MySQLi 連接到數(shù)據(jù)庫(kù)時(shí),您必須使用該連接.
It is because the MySQL and MySQLi database connections are not linked. When you make a connection to database with MySQLi, you have to use that connectio.
示例:
$mysqli = new mysqli('localhost', 'user', 'password', 'database');
$result = $mysqli->query("SELECT * FROM users");
這篇關(guān)于PHP 將 mysql 轉(zhuǎn)換為 mysqli的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!