Forum Moderators: coopster

Message Too Old, No Replies

Sqlite: database resource unavailable in for loops?

         

Rune

5:42 pm on Mar 14, 2010 (gmt 0)

10+ Year Member



Recently I've switched to using SQLite databases and a problem that keeps presenting itself is the database resource set at the top of a page not being available in foreach or for loops.

Example:
$dbloc = "c:\web\db\test.sqlite";if($db = sqlite_open("$dbloc", 0666, $sqlite_error)){}else{die ($sqlite_error);}

for($i=0; $i<8; $i++){
echo $db;
}


In that example $db outputs nothing, instead of "Resource #". Adding "global $db;" to the loop does nothing.

Matthew1980

6:36 pm on Mar 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Rune,

If you are calling info from the database and then processing it, the way to do this is with a:-

while($yourvar = sqlite_fetch_array($query_to_db))

and not:-

foreach() or for()

At least this is how I have always processed data from a db.

Welcome to the forum too!

Resource # usually means that the function you have used is working and is returning an id number, not sure on the technicalities of the numbers, I just get the data and make sure that a field value can get echo'd before I carry on with anything else, but hey, we all have different ways of working ;-p


Global wouldn't do anything as this is just used when for example you are referencing a $var from within a function, prefix the $var with global and then PHP will look everwhere within the script and any reference include(); paths until it finds a $var with that name.

Cheers,
MRb