1

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

I have tow mysql data bases. the first data base location is latin1_swedish_ci and the second data base is utf_unicode_ci.I am trying to read tha data bases with tha php code below.

<?php
mysql_connect("localhost","admin","***");
mysql_select_db("MyDB");
$sql=mysql_query("select * from menu where avail=1");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>

When I am runing this code via my php server the first data base the one with latin1_swedish_ci is readed but when I am trying to read the second one it displays the following messages:

"Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in...."
"Notice: Undefined variable: output in...."

I tried to give mysql_query("SET NAMES utf8;"); in my code but it didnt worked Can anyone tell me what is going wrong here?

Community
  • 1
  • 1
George32x
  • 37
  • 4
  • 7

3 Answers3

1

First of all, you should add $output = array(); before the loop.

Then, fix your query so it doesn't fail. If mysql_query() returns a boolean (false), it means the query failed.

You can add an ugly construct to see why it failed:

$sql=mysql_query("select * from menu where avail=1") or die(mysql_error());
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
1

You had an error in your query. The error caused this line:

$sql=mysql_query("select * from menu where avail=1");

to return false. This false when fed to the next line:

while($row=mysql_fetch_assoc($sql))

raised the first error. The while was skipped hence this line was never executed:

$output[]=$row;

Hence the second warning.

Add a mysql_error() after your mysql_* statements like this and make this a habit:

mysql_connect("localhost","admin","***") or die(mysql_error());
.
.
.
mysql_select_db("MyDB") or die(mysql_error());
.
.
.
$sql=mysql_query("select * from menu where avail=1") or die(mysql_error());

These the die(mysql_error()) statement terminates the script after displaying the error message. While it may or may not be necessary to make the script die on mysql errors, most people keep it that way. Instead of using die statement, you can check the return value instead and terminate the script if necessary.

The error in your case seems to be at the database selection line. May be the database name you specify in mysql_select_db was incorrect or may be you do not have permission to read that database (establishing connection with a given user name/password does not mean that that user can read all databases). Post the error message (again).

Salman A
  • 262,204
  • 82
  • 430
  • 521
  • it prinds "No database selected" – George32x May 23 '11 at 17:36
  • I've edited my answer. You might have made a typo in your `mysql_select_db()` statement or you do not have permissions to read the database. There could be other uncommon reasons too. – Salman A May 24 '11 at 04:20
-1

Try mysql_fetch_array instead of mysql_fetch_assoc

Also define $output before you use it:

$output = array();
while($row=mysql_fetch_array($sql))
   $output[]=$row;
Naftali
  • 144,921
  • 39
  • 244
  • 303
  • Yes: `mysql_fetch_assoc()` returns an associative array while `mysql_fetch_array()` returns an array with both associative and numerical indexes. So it not only bloats his json but also adds data he most likely doesn't need. – ThiefMaster May 23 '11 at 17:23