两条错误消息(我改为mysqli,现在得到不同的错误消息!)

时间:2012-11-10 20:52:15

标签: php mysql mysqli

现在数据库中唯一的名字是“Paul”,当我输入“Paul”时,我收到2条错误消息:

Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/ftpbpan/public_html/ThornAJAX/info.php on line 10
which is:   $query = mysqli_query("select * from peopleTwo where name='$name' limit 1");

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/ftpbpan/public_html/ThornAJAX/info.php on line 15
which is:    $num_rows = mysqli_num_rows($query);

以下是代码:

<?php
$connect = mysqli_connect('localhost','****','******');

mysqli_select_db($connect, 'people'); 

if(strlen($_GET['user']) >0){
$name = $_GET['user'];
$query = mysqli_query("select * from peopleTwo where name='$name' limit 1");
if (false === $query) {
die(mysql_error());
}
$num_rows = mysqli_num_rows($query);

if($num_rows == 1) {
$row = mysqli_fetch_assoc($query);

2 个答案:

答案 0 :(得分:2)

第二个错误直接来自第一个错误,因为mysqli_query在程序形式中使用时会获取连接的参数和查询字符串,例如mysqli_query($db, $query)

要修复此问题,请将数据库连接添加为参数。

$query = mysqli_query($connect, "select * from peopleTwo where name='$name' limit 1");

答案 1 :(得分:0)

你应该传递一个参数,它是你函数中的连接资源,或者称之为面向对象的方式:

//procedural :
$connection = mysqli_connect('localhost','yourdb','root','');

$query = mysqli_query($connection,"select * from peopleTwo where name='$name' limit 1");

//OOP
$query = $connection->query("select * from peopleTwo where name='$name' limit 1");