Jump to content

error: Looping an Insert Into mysql


Colton.Wagner

Recommended Posts

I am tryin to loop the same number into a databas 17659 times but with the following code:

 

<?php

error_reporting(E_ALL);
$con = mysql_connect('localhost', 'root', '');
   if (!$con) {
      die('Connection Error:' . mysql_error());
   }
mysql_select_db('catgories', $con);
      
for($i=0;$i<=17659;$i++)
{
$query="INSERT INTO categories(`products_id`,`categories_id`) select $i,categories_id from categories"; 
mysql_query($query) or trigger_error("SQL: $query, ERROR: ", E_USER_ERROR);
}
?>

 

However I get the following error:

 

Fatal error: SQL: INSERT INTO categories ('products_id'. 'categories_id') VALUES ('', '18'), ERROR: in H:\Xampp\xampplite\htdocs\categories.php on line 13

Link to comment
https://forums.phpfreaks.com/topic/177966-error-looping-an-insert-into-mysql/
Share on other sites

This is the second thread you've started on this problem. The trigger error has . mysql_error() missing from it (as I forgot to include it originally in your last thread) so we can't actually see why the query failed. But as was mentioned in the last thread, theres a good change it doesn't like the '' for an auto increment field nor the single quotes around an integer value. Not to mention the fact that the error message your showing us is nothing todo with that code, since the last line is set to echo out $query, and the string in your error message doesn't match the value of $query.

<?php

error_reporting(E_ALL);
$con = mysql_connect('localhost', 'root', '');
   if (!$con) {
      die('Connection Error:' . mysql_error());
   }
mysql_select_db('catgories', $con);
      
for($i=0;$i<=17659;$i++)
{
$query="INSERT INTO categories(products_id, categories_id) select $i,categories_id from categories"; 
mysql_query($query) or trigger_error("SQL: $query, ERROR: ", E_USER_ERROR);
}
?>

this would be an SQL issue, not PHP since your loop is fine .. it's just your query.

 

well, your isn't 100% if you want to loop 17659 and not 17660 .. change $i=0 to $i=1 .. this way, you will loop through from 1-17659, and not from 0-17659;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.