-2

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in

Please help I have set up a database in MYSQL and want my program to print data from the database. I have a slight issue a warning comes up saying: "Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in" Can any one help please

'

//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);
'
Community
  • 1
  • 1
Callum James
  • 27
  • 1
  • 1
  • 5
  • It says the issue comes up in line 21 while($row = mysql_fetch_array($result)) – Callum James Jan 23 '13 at 20:04
  • Do you see that list on the right? – PeeHaa Jan 23 '13 at 20:04
  • This question again today? ... – crush Jan 23 '13 at 20:05
  • 1
    Stop using mysql_* functions. They are unsaufe and deprecated. Move on to mysqli_* or PDO. – crush Jan 23 '13 at 20:05
  • `$result = mysql_query($sql) or die(mysql_error());` will tell you why your query is broken. – crush Jan 23 '13 at 20:06
  • I highly doubt you have selected a valid database, because `if ($db)` will be `true` when you select a database. Since this condition is not met, that means `$db` is set to `false`. **Bottom line**: I doubt your problem is with the query - It's with selecting a database. – nickb Jan 23 '13 at 20:10
  • @nickb is correct. Because $db is false, your code is allowed to continue executing, but also because $db is false, you can't execute a query, as no db has been selected. – crush Jan 23 '13 at 20:31

3 Answers3

2

Your query is not returning any results and so $result is not a resource. Try:

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

Also try to use mysqli_* group of commands instead of mysql_*

raidenace
  • 12,641
  • 1
  • 29
  • 34
0

That means that your query is failing.

//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']);
}
Bradley
  • 2,001
  • 1
  • 22
  • 30
0

Try this:

<?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_ version of your query. I have used my default variable names, you can set them back to your own variable names if you like. B.W.T. check if the database is in fact called test (2). I am not a fan of using brackets or numbers in variables, databases, etc.

Mr. Radical
  • 1,833
  • 1
  • 18
  • 28
  • `I am not a fan of using brackets or numbers in variables/ databases/ etc.` add spaces to that list too. – crush Jan 23 '13 at 20:28
  • @crush edit my answer if you like. I have no problem with people correcting my grammar or spelling. – Mr. Radical Jan 23 '13 at 20:30
  • It was more of a personal opinion, and not a critique on your grammar. I meant that I simply don't like spaces in my database names either, and frankly, I wasn't even aware that it was legal. – crush Jan 23 '13 at 20:32
  • @crush that was exactly the reason why I mentioned it. I wasn't sure it was possible to use brackets or numbers in a database name either. Therefore I decided to mention that I wouldn't try it out. Perhaps I should have rephrased my last sentence or left I out all together. – Mr. Radical Jan 23 '13 at 20:37
  • No, Mr. Radical. I think you are right in your suggestion. I think it is very good advice to restrict db names to alpha and underscores only, if nothing else, to make it more portable. – crush Jan 23 '13 at 20:38