我是PHP新手,请关注this tutorial将php webservices连接到android下面是我的代码我将MySQL
更改为MySQLi
,但仍显示错误:
<?php
$host="localhost"; //replace with database hostname
$username="root"; //replace with database username
$password=""; //replace with database password
$db_name="emp_info"; //replace with database name
$con=mysqli_connect("$host", "$username", "$password")or die("cannot
connect");
mysqli_select_db("$db_name")or die("cannot select DB");
$sql = "select * from emp_info";
$result = mysqli_query($sql);
$json = array();
if(mysqli_num_rows($result)){
while($row=mysqli_fetch_assoc($result)){
$json['emp_info'][]=$row;
}
}
mysqli_close($con);
echo json_encode($json);
?>
答案 0 :(得分:0)
有两种解决方案,首先使用mysqli_connect()
中的第四个参数。
像这样:
$con=mysqli_connect($host, $username, $password, $db_name)
其次,您需要在mysqli_select_db()
$con=mysqli_connect($host, $username, $password);
mysqli_select_db($con, $db_name);
MySQLi中的几乎所有函数都需要第一个参数的连接,所以mysqli_query()
也需要连接。
$con=mysqli_connect($host, $username, $password, $db_name)or die("cannot connect");
$sql = "select * from emp_info";
$result = mysqli_query($con, $sql); //<--- here query need the connection