-3

我正在尝试从数据库中获取数据,但遇到此错误。查询中没有错误,因为它在 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;


    ?>

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

4

1 回答 1

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 或类似的东西。

于 2012-10-28T11:15:06.167 回答