今天在写一个小小的php验证页面,代码如下:


<?php  
// 接受表单提交值
$username = $_POST["username"];
$password = $_POST["password"];


// 连接服务器
 $dbhost = 'localhost:3306'; 
// mysql服务器主机地址
$dbuser = 'root'; 
// mysql用户名
$dbpass = '123'; 
// mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn ){ die('Could not connect: ' . mysqli_error());
}
//选择test数据库 
$selected_table = "test";
if(mysqli_select_db($conn, $selected_table)){
$sql = "select * from user";
$return_value = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($return_value, MYSQLI_ASSOC)){
if(($username == $row["UserName"]) && ($password==$row["Pass"])){
echo "验证通过!正在为你跳转,请稍候……";
echo "<script>location.href='welcome.php';</script>"; 
}else{
echo "验证失败!";
}}
}else{
echo '表格选择失败!';
}mysqli_close($conn); ?>

以为很快就能写完,结果在本地 mac上写完后跑得很嗨森,上传到阿里云的 centOS环境,就出现了以下的错误:

Warning: mysqli_fetch_array() expects parameter 2 to be integer, string given in /var/www/html/projects/mysql_connection.php on line 41

excuse me???!!!

翻遍了stackoverflow,检查了两边的mysql的版本号和各种环境,依然未果。耗费了将近2小时的时间进行debug。后来心灰意冷的我重新再看了一遍这个提示,然后查看了一下php manual的mysqli_fetch_array()这个函数: http://jp2.php.net/manual/en/mysqli-result.fetch-array.php

然后修改了其中这一部分:

if(mysqli_select_db($conn, $selected_table)){
$sql = "select * from user";
$return_value = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($return_value, MYSQLI_NUM)){
	if(($username == $row[1]) && ($password==$row[2])){
				echo "验证通过!正在为你跳转,请稍候……";
				echo "<script>location.href='welcome.php';</script>"; 
		}else{
		echo "验证失败!";
		}	
	}
}else{
	echo '表格选择失败!';	
}

即将MYSQLI_ASSOC 换成 MYSQLI_NUM,即修复该问题。[捂脸]


Logo

更多推荐