Welcome to WebmasterWorld Guest from 54.147.0.174

Forum Moderators: rogerd & travelin cat

Message Too Old, No Replies

while loop produces errors in db data retrieval

     

surrealillusions

2:14 pm on Sep 17, 2012 (gmt 0)

5+ Year Member



Having a bit of trouble in getting data from a couple of custom database tables.

This error is produced:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in <url> on line 19


$qry = "SELECT *
FROM wp_customtable";
$data = mysql_query($qry);
while($row = mysql_fetch_array($data,MYSQL_ASSOC)) {
// a whole bunch of stuff dealing with the data
}


Trying a foreach loop doesnt produce any errors, but also produces no data.

I've tried numerous bits of code from around the code but with no success.

How do you get the while loop to work within wordpress?

All this code is within a custom page template, no plugins used for this.

lorax

10:21 pm on Sep 17, 2012 (gmt 0)

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Silly question but did you connect to the database yet?

surrealillusions

3:27 pm on Sep 18, 2012 (gmt 0)

5+ Year Member



Yep. I get data if I use a foreach statement,

With the while loop there, no data is displayed and I get that error.

Any reason why the while loop produces an error and the foreach statement is fine?

lorax

10:39 pm on Sep 19, 2012 (gmt 0)

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



No good reason that I can think of but I haven't dug into the details of your code. In your opening post you said you didn't get data. Now you do? I am not aware of any particular reason the while loop wouldn't run. SO... troubleshooting time.

What happens if you remove the while statement and simply try to output the number of rows returned or the data from a single row?
 

Featured Threads

Hot Threads This Week

Hot Threads This Month