首页 文章

PHP问题:警告:mysql_fetch_array()期望参数1是资源,在[[duplicate]中给出布尔值

提问于
浏览
-2

可能重复:mysql_fetch_array()期望参数1是资源,在select中给出boolean警告:mysql_fetch_array()期望参数1是资源,布尔值在

请帮助我在MYSQL中设置数据库,并希望我的程序从数据库中打印数据 . 我有一个小小的问题,警告出现说:“警告:mysql_fetch_array()期望参数1是资源,布尔值在”可以任何一个帮助请

//open a MySQL Connection
$link = mysql_connect('localhost', 'root', '');
if(!$link)
{
    die('Connection failed:' .mysql_error());
}

//select the database to work with
$db = mysql_select_db('test (2)');
if ($db)
{
    die('Selected database unavailable:' .mysql_error());
}

//create and excute a mysql query
$sql = "SELECT artist_name FROM artist";
$result = mysql_query($sql);

//loop through the returned data and output it
while($row = mysql_fetch_array($result))
{
    printf("Artist: %s
", $row['artist_name']); } // free memory associated with the query mysql_free_result($result); //close the connection mysql_close($link); '

3 回答

  • 2

    您的查询未返回任何结果,因此 $result 不是资源 . 尝试:

    //create and excute a mysql query
    $sql = "SELECT artist_name FROM artist";
    $result = mysql_query($sql) or die(mysql_error());
    

    也尝试使用 mysqli_* 组命令而不是 mysql_*

  • 0

    这意味着您的查询失败 .

    //create and excute a mysql query
    $sql = "SELECT artist_name FROM artist";
    $result = mysql_query($sql);
    
    if ($result === false) mysql_error();
    
    //loop through the returned data and output it
    while($row = mysql_fetch_array($result))
    {
        printf("Artist: %s
    ", $row['artist_name']); }
  • 0

    试试这个:

    <?php
    
    //open a MySQL Connection
    $link = mysqli_connect('localhost', 'root', '','test (2)');
    if(!$link)
    {
        echo('Unable to connect to the database!');
    }
    ELSE {
    
    
    //create and excute a mysql query
    $query = "SELECT artist_name FROM artist";
    $result = mysqli_query($link, $query);
    
    //loop through the returned data and output it
    while($row = mysqli_fetch_array($result, MYSQLI_BOTH))
    {
        printf("Artist: %s
    ", $row['artist_name']); } // free memory associated with the query mysqli_free_result($result); } //close the connection mysqli_close($link); ?>

    您查询的MYSQLI_版本 . 我使用了我的默认变量名,如果你愿意,你可以将它们设置回你自己的变量名 . B.W.T.检查数据库是否实际上被称为 test (2) . 我不喜欢在变量,数据库等中使用括号或数字 .

相关问题