Loop goes only through first iteration

I have a function in functions.php, but loop in it goes only through first iteration. What should I do?
function some_wp_function() {
global $wpdb;
$sql=“”;
$i=0;
while ($i <= 3) {
$sql.=“INSERT INTO test_table (category_id, category_posts_count, category_comments_count) VALUES ('”.$i.“', '”.$i.“', '”.$i."'); ";
$i++;
}
$result=$wpdb->get_results($sql);

Did you check for errors?

Your code might stop if it has errors.
Also, I’m assuming you only posted part of the code because running this will give a syntax error due to the unclosed function (You need another } at the end of your code if this is all of it).

Are you sure it only goes through one iteration? Or are you only seeing one insert and assuming that’s caused by the PHP code not working?

My guess is that chaining the multiple queries with ; doesn’t work. Not all MySQL libraries support it Could you try executing the query from the for loop and see if that changes something?

2 Likes

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.