0
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C ...

This is code

function db_result_to_array($result) {

$res_array = array();

for ($count=0; $row = mysql_fetch_array($result); $count++) { 

    $res_array[$count] = $row;
}

return $res_array;

}

So, the error is in this line

    for ($count=0; $row = mysql_fetch_array($result); $count++)

I tried

    mysql_fetch_object()
    mysql_fetch_assoc()
    mysql_fetch_row()

and didn't work

I tried

    mysql_fetch_array($result, MYSQL_ASSOC)
    mysql_fetch_array($result, MYSQL_NUM)
    mysql_fetch_array($result, MYSQL_BOTH)

and it didn't work. Could you help me resolve this?

Vinay
  • 6,890
  • 4
  • 31
  • 50
  • Well, what is `$result`? – sachleen Nov 03 '12 at 03:25
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – tereško Apr 15 '13 at 21:46

3 Answers3

2

You should be running the code like this:

$result = mysql_query(...) or die(mysql_error());
$data = array();
while($row = mysql_fetch_assoc($result)) {
    $data[] = $row;
}

There is almost NEVER a need to run a normal for() loop to fetch rows from a result set. It's by far more reliable/easier to use a while loop. It'll auto-terminate when there's no more data to fetch, and you don't run into the case you are, where your loop is running off the end of the fetch.

Marc B
  • 348,685
  • 41
  • 398
  • 480
1

$result should be the result of a sql statement, and query that return rows of record e.g. SELECT * From someTable and the query must be syntax-correct to have a valid result

$sql = 'SELECT * FROM users'; //make sure no syntax error from generated query
$result = mysql_query($sql); //this should be a valid result [resouce id]

$recordsArray = db_result_to_array($result); //call your function with the valid result. boolean cannot be passed

Also change your for to while as suggested.

Use of mysql_ extension is discouraged

Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used ... Alternatives to this function include:

  • mysqli_query()

  • PDO::query()

Update

From MySQL link posted above

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

So, you will get valid resource if SELECT, SHOW, DESCRIBE, EXPLAIN executed successfully and a boolean (FALSE) if not.

For INSERT, UPDATE, DELETE, DROP you'll get TRUE (success) or FALSE (error)

codingbiz
  • 25,638
  • 8
  • 55
  • 93
-1

Look at how you are assigning the $result variable before you make the call to your function. What is the query used in assigning this $result?

rharrison33
  • 1,200
  • 4
  • 16
  • 33
  • -1 This does not answer the question *at all* - questions and comments should be left as comments on the original question. – sachleen Nov 03 '12 at 03:45