0

I have a simple query which does work in a plain environment, but doesn't work on PHPUnit.

Here's the code, but I don't think it will be of any use:

$a = mysql_query("SELECT id,img FROM images");
$b = mysql_fetch_array($a);

I'm connected to the database.

The error is:

mysql_fetch_array() expects parameter 1 to be resource, boolean given.

Error is pointing to the fetch command.

Jayela
  • 365
  • 1
  • 4
  • 7
  • check if you have a valid connection as far as I know for PHP UNIT we write code in a sub-folder and this will need a valid sql connection for doing sql operations for assert something. – Abhik Chakraborty Mar 14 '14 at 07:15
  • You should also use Mocks for testing to not actually require the database and control returned data. Later, DBUnit may be added for testing the Database. – Steven Scott Mar 14 '14 at 16:08
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – John Conde Mar 18 '14 at 17:35

1 Answers1

0

The query failed. When queries fail the mysql_query() function returns false.

You should always check for errors. You should also read the documentation when your code does not work; it tells you what to look for.

$a = mysql_query("SELECT id,img FROM images");
if ($a === false) {
    die(mysql_error());
}
$b = mysql_fetch_array($a);
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
  • 1
    You should also switch from `mysql` to `mysqli` functions, because the mysql functions are deprecated and are getting removed. – Sven Mar 15 '14 at 01:44