Mitch8910
Mitch8910

Reputation: 183

Loop through all items in a table mySQL

I'm going to use this as a cronjob, I'm trying to loop through all of the rows in a table in my database, then update each item in the the row. (I update by calling another website's API to get updated information.) I connect to the database using PDO. My code:

$loop = $dbh->prepare("SELECT * FROM item_list");
    $loop->execute();

    while($row = mysql_fetch_array($loop)){
    ...get new info and update database...
    }

I have error checking on and my error is: "Warning: mysql_fetch_array() expects parameter 1 to be resource, object given in page/directory.php on line 71"

I have all the code written to update each item as it loops through, I just can't get it to loop though.

Upvotes: 0

Views: 132

Answers (2)

user3127286
user3127286

Reputation: 111

$loop = $dbh->prepare("SELECT * FROM item_list");
$loop->execute();

while($row = $loop->fetch()){
...get new info and update database...
}

Try this!

Upvotes: 1

Honza Haering
Honza Haering

Reputation: 812

You are mixing PDO and mysql extension. Use PDO only:

while ($row = $loop->fetch()) {
...
}

Upvotes: 1

Related Questions