-4

Hi im trying to write the data from my database and this the code thats meant to be doing it but i get two of the 'Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given' and dont know why????

$data = mysql_query("SELECT * FROM teams");
$result = mysql_fetch_array($data);


while($result = mysql_fetch_array($data)) {
    print "<b>Name:</b>" .$result['Team Name'] . " ";
}
Jonnny
  • 4,671
  • 11
  • 60
  • 88
Jonny_Appleby12
  • 55
  • 1
  • 2
  • 9
  • 2
    You should really be using mysqli not mysql – Jonnny Jul 11 '13 at 21:57
  • im just learning at the minute – Jonny_Appleby12 Jul 11 '13 at 21:57
  • Well a good place is to use mysqli in the place of mysql http://php.net/manual/en/book.mysqli.php – Jonnny Jul 11 '13 at 21:58
  • Your query fails and returns false. Be sure you have the table you're looking for. – BudwiseЯ Jul 11 '13 at 21:58
  • if you want to print your result like $result['Team Name'] you need to use mysql_fetch_assoc(). –  Jul 11 '13 at 21:59
  • 3
    Take a step back. Read your error message. It tells you that your parameter is a boolean. Why is it a boolean? This parameter is `$data`. This means your mysql_query() is returning a boolean. What scenarios result in this function returning a boolean? Read the [documentation](http://www.php.net/manual/en/function.mysql-query.php). Also, while you're there, take note of the red box. Try to use one of the other extensions. `mysql_` methods have been deprecated. – BLaZuRE Jul 11 '13 at 21:59
  • See [this answer](http://stackoverflow.com/a/11674313/250259) for how to troubleshoot this. – John Conde Jul 12 '13 at 01:23

2 Answers2

2

the boolean is given because your assuming the query has run ok. For some reason your query has failed maybe because you havent selected a database ?

Either way all new code shouldnt be using mysql_* instead look at mysqli_* or PDO

to see what the actual error is

if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    die($message);
}

between the query and the fetch result

exussum
  • 17,675
  • 8
  • 30
  • 64
-1

You should be using PDO anyway, mysql_query() and mysql_fetch_array() are being deprecated. That error usually means that your query is failing, which could be a kagillion different things based on your question lol....

A.O.
  • 3,719
  • 6
  • 29
  • 49
  • This answer is usually appropriate as a comment instead, which is probably the reason you are being downvoted. I understand you don't have the reputation to comment yet, however. – BLaZuRE Jul 11 '13 at 22:03
  • thanks for the sympathy lol.....either way, it looks like he'll be able to figure it out now – A.O. Jul 11 '13 at 22:05