1

Possible Duplicate:
PHP Error: mysql_fetch_array() expects parameter 1 to be resource, boolean given

I am trying to build an array from a mysql object after querying.

The number or rows returned in the query is 68, this is why I thought a forloop would be good. However php does not like my code inside the forloop.

Here is the code:

$result = $db->query("SELECT * FROM `networktariff`");


        $rows = mysql_num_rows($result);

        for ($i= 0; $i<$rows; $i++)
        {

            $tariffs[$i] = mysql_fetch_assoc($result[$i]);


        }

I am getting an error message saying :

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in /usr/local/www/apache22/data/quote/index.php on line 58

Community
  • 1
  • 1
tomaytotomato
  • 3,788
  • 16
  • 64
  • 119
  • The problem in your code is that `$result` is not an `array` it is a `resource`. – fyr Dec 16 '11 at 10:14
  • By the way, instead of using your own DB class, you might should consider PDO. – Corbin Dec 16 '11 at 10:17
  • This make no sense, is already fail at this line :- `$rows = mysql_num_rows($result);`, can you do a `var_dump($result);`? – ajreal Dec 16 '11 at 10:20
  • @Corbin PDO is the same raw API with extremely limited functionality. Instead of using this stub, you should use some own DB class. – Your Common Sense Dec 16 '11 at 10:21
  • It depends on the aim of the class. Even with a custom class though, I would probably still use PDO as the adapter instead of separate adapters (unless PDO didn't support all of the desired RDBMS or only 1 is desired to be supported). – Corbin Dec 16 '11 at 10:23
  • @Corbin it depends on the sane coding. doing zillions bindings manually is not the purpose of the programmer who values his time. – Your Common Sense Dec 16 '11 at 10:34
  • I'm not arguing that a custom class can be useful for common operations. For example, your mentioned query2arr method. I'm guessing though that his class is just a very thin, useless wrapper around mysql_* functions. I also believe that PDO would be a good adapter for a 'utility' class. If the db class were directly tied to DB communication (for example, directly wrapping mysql_* calls), the flexibility of the class would be significantly damaged. An adapter interface could be considered ideal, but using PDO as an adapter would still be very useful. – Corbin Dec 16 '11 at 10:38
  • I also don't feel that it's correct to call PDO "extremely limited functionality" compared to the raw API. The raw MySQL API knows MySQL information more intimately so it can interact with it in ways PDO might not be capable of. Those few things are rarely needed though. – Corbin Dec 16 '11 at 10:39
  • Hi all, I am a junior developer my database class just has a connect, select db and query function. – tomaytotomato Dec 16 '11 at 10:49
  • @loosebruce can you do a `var_dump($result);` ? – ajreal Dec 16 '11 at 10:56
  • Sure - "resource(4) of type (mysql result) " – tomaytotomato Dec 16 '11 at 11:12

3 Answers3

1

Yup, read the docs on mysql_fetch_assoc().

A better construction might be:

while($row = mysql_fetch_assoc($result)) {
  // Do stuff with $row
}

To elaborate: $result does not contain any data. It's a "resource" handle that points to a resultset that MySQL has created. To access the data of the resultset, you use mysql_fetch_* and pass it the resource handle. Each time you call a fetch function, the internal pointer is incremented one row. So you'll get fresh data each time until MySQL reaches the end of the results, at which point you'll get FALSE. This is why the while() loop works.

Interrobang
  • 16,984
  • 3
  • 55
  • 63
1

There is no such thing like "mysql object".
Your $db object belongs to some user-defined class and there is no point in asking anyone about this class' behavior.

Assuming that query() method just utilizing ordinary mysql_query() function call, it does return a resource, not object.

If you have a strange idea of using some class to run the query yet bare API functions for the rest,

$tariffs = array();
$result = $db->query("SELECT * FROM `networktariff`");
while ($row = mysql_fetch_assoc($result) {
  $tariffs[] = $row;
}

I dunno though why your class doesn't have some helper function to get the whole data at once, something like

$tariffs = $db->query2arr("SELECT * FROM `networktariff`");
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
1

print $result as $db->query is not standard php function, it might return you result set array. try same code with replacing $db->query to mysql_query