-2

可能重复:
mysql_fetch_array() 期望参数 1 是资源,布尔值在选择中给出
警告: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<br/>", $row['artist_name']);
}

// free memory associated with the query
mysql_free_result($result);

//close the connection
mysql_close($link);
'
4

3 回答 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_*

于 2013-01-23T20:08:11.003 回答
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<br/>", $row['artist_name']);
}
于 2013-01-23T20:07:12.860 回答
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<br/>", $row['artist_name']);
}

// free memory associated with the query
mysqli_free_result($result);
}
//close the connection
mysqli_close($link);

?>

MYSQLI_ 版本的查询。我使用了我的默认变量名,如果你愿意,你可以将它们设置回你自己的变量名。BWT 检查是否确实调用了数据库test (2)。我不喜欢在变量、数据库等中使用括号或数字。

于 2013-01-23T20:21:39.533 回答