源代码
<?php
$db_host = 'localhost';
$db_username= 'root';
$db_password= '';
$db_database= 'website';
$db = mysql_connect($db_host, $db_username, $db_password);
if (!$db) {
echo "连接数据库失败!";
exit;
}
mysql_select_db($db_database, $db);
$username = $_POST['username'];
$passwd = $_POST['userpwd'];
$query = "select * from users where username = '$username' and password = '$passwd' ";
$result = mysql_query($query) or die('数据库查询失败') ;;
$num_results = mysql_numrows($result);//取得数据库中的记录行
echo "$query <br>";
if($num_results == 0)
echo '用户名或密码错误!!';
else
echo '登陆成功!!';
?>
<p><a href="login.html">返回登陆</a></p>
问题1.Fatal error: Uncaught Error: Call to undefined function mysql_connect()
原因:
PHP7.x版本中已经舍弃了mysql_connect()。应该用mysqli_connect()
PHP5.x版本mysql_connect()和mysqli_connect()是都可以用的
类似的mysql_query(),mysql_select_db()
问题2.Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given
原因:mysql_select_db()和mysqli_select_db()
参数位置刚好相反
问题3Warning: mysqli_query() expects at least 2 parameters, 1 given
原因:mysqli_query()少了一个参数。加上$db
问题4Fatal error: Uncaught Error: Call to undefined function mysql_numrows()
原因:已弃用mysql_numrows()函数。应该用mysqli_num_rows()
问题5:PHP页面返回中文乱码
把html改为<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
把php.ini的default_charset
改为gb2312
修改后的源代码
<?php
$db_host = 'localhost';
$db_username= 'root';
$db_password= '';
$db_database= 'website';
$db = mysqli_connect($db_host, $db_username, $db_password);
if (!$db) {
echo "连接数据库失败!";
exit;
}
mysqli_select_db($db, $db_database);
$username = $_POST['username'];
$passwd = $_POST['userpwd'];
$query = "select * from users where username = '$username' and password = '$passwd' ";
$result = mysqli_query($db, $query) or die('数据库查询失败') ;
$num_results = mysqli_num_rows($result);//取得数据库中的记录行
echo "$query <br>";
if($num_results == 0)
echo '用户名或密码错误!!';
else
echo '登陆成功!!';
?>
<p><a href="login.html">返回登陆</a></p>