PHP将mysql转换为mysqli [英] PHP Convert mysql to mysqli

查看:289
本文介绍了PHP将mysql转换为mysqli的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下脚本可以正常工作:

The following script works fine:

$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";
}

为什么,当我仅用以下内容替换mysql_connect和mysql_select_db语句(包括所有密码的所有其他内容保持不变)时,我得到错误的用户名或密码"回显吗?

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");

推荐答案

这是因为MySQL和MySQLi数据库连接未链接.使用MySQLi与数据库建立连接时,必须使用该connectio.

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");

这篇关于PHP将mysql转换为mysqli的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆