mysql_query()期望参数2是resource,boolean

时间:2012-10-28 11:13:48

标签: php mysql

我正在尝试从数据库中获取数据但遇到此错误。查询中没有错误,因为它在mysql中运行完美。

为什么我会收到此错误?

[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代码:       

    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;


    ?>

* UPDATE :::无论我在哪里使用mysql_error()函数,都显示错误。现在删除所有“$ db”参数后,它就可以了。为什么?*

1 个答案:

答案 0 :(得分:1)

只需这样称呼:

$result = mysql_query($query);

如果查看mysql_connect函数,它会在失败时返回false(布尔值):

http://php.net/manual/en/function.mysql-connect.php

所以你的联系甚至可能都没有确定。

编辑:看看这个功能: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只返回一个布尔值。

编辑2: 你的意思是:

$db = mysql_connect('localhost','root','saw123') or die("Connection error");
mysql_select_db("moviesite",$db) or die(myql_error($db));
在这种情况下,

$ db将无法正确命名;它更准确地称为$ connection或$ db_connection或类似的东西。