mysql_query()期望参数2为资源,布尔值 [英] mysql_query() expects parameter 2 to be resource, boolean
问题描述
我正在尝试从数据库中获取数据,但是遇到此错误.查询中没有错误,因为它可以在mysql中完美运行.
I am trying to get data from the database but experiencing this error. There is no error in the query as it runs perfectly in mysql.
那我为什么会收到这个错误?
Why i am getting this error then ?
[client 127.0.0.1] PHP Warning: mysql_query() expects parameter 2 to be resource, boolean given in /var/www/imdb.php on line 35
[client 127.0.0.1] PHP Warning: mysql_error() expects parameter 1 to be resource, boolean given in /var/www/imdb.php on line 35
php代码:
php code:
function getdirector($director)
{
global $db;
$query = 'select name from peopledb where peopleid = '.$director;
$result = mysql_query($query,$db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $name;
}
function getactor($actor)
{
$query = 'select name from peopledb where peopleid = '.$actor;
$result = mysql_query($query,$db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $name;
}
function getmovietype($type)
{
$query = 'select movietype from movietype where movieid = '.$type;
$result = mysql_query($query,$db) or die(mysql_error($db));
$row = mysql_fetch_assoc($result);
extract($row);
return $movietype;
}
$db = mysql_connect('localhost','root','saw123') or die("Connection error");
$db = mysql_select_db("moviesite",$db) or die(myql_error($db));
$query = 'select moviename,releaseyear,director,actor,type from movie';
$result = mysql_query($query,$db) or die(mysql_error($db));
$table = <<<ENDHTML
<div style="text-align: center;" >
<h2>The Ultimate movie database</h2>
<table border='1' cellpadding='1' cellspacing='2'
style="width: 70%; margin-left: auto; margin-right: auto;">
<tr>
<th>Movie title</th>
<th>year of release </th>
<th>Movie director</th>
<th>Movie Actor</th>
<th>Movie type</th>
</tr>
ENDHTML;
while ($row = mysql_fetch_assoc($result))
{
extract($row);
$director1 = getdirector($director);
$actor1 = getactor($actor);
$movietype1 = getmovietype($type);
$table .= <<<ENDHTML
<tr>
<td>$moviename</td>
<td>$releaseyear</td>
<td>$director1</td>
<td>$actor1</td>
<td>$movietype1</td>
</tr>
ENDHTML;
}
$table .=<<<ENDHTML
</table>
</div>
ENDHTML;
echo $table;
?>
* 更新:::无论我在哪里使用mysql_error()函数,它都显示错误.现在,删除所有"$ db"参数后,它就可以工作了.为什么?*
推荐答案
只需这样命名:
$result = mysql_query($query);
如果您查看mysql_connect函数,它会在失败时返回false(布尔值):
If you look at the mysql_connect function it returns false ( a boolean) on failure:
http://php.net/manual/en/function.mysql- connect.php
因此您的连接可能甚至没有建立.
So your connection is likely not even established.
查看此函数: http://php.net/manual/en/function.mysql-select-db.php
您正在呼叫:
$db = mysql_connect('localhost','root','saw123') or die("Connection error");
$db = mysql_select_db("moviesite",$db) or die(myql_error($db));
第二行将$ db设置为true或false-因为mysql_select_db仅返回布尔值.
The second line sets $db to either true or false - since mysql_select_db only ever returns a boolean.
您可能的意思是:
$db = mysql_connect('localhost','root','saw123') or die("Connection error");
mysql_select_db("moviesite",$db) or die(myql_error($db));
在这种情况下,
$ db不能正确命名;它会更准确地称为$ connection或$ db_connection或类似的东西.
$db in this case would not be appropriately named; it'd be more accurately called $connection or $db_connection or something like that.
这篇关于mysql_query()期望参数2为资源,布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!