Colton.Wagner Posted October 15, 2009 Share Posted October 15, 2009 I have 17659 entries that are all the same. There are only two fields in the mysql databse Products_id witch auto increments and categories_id witch is an int 10. This source code throws no errors. <?php error_reporting(E_ALL); $con = mysql_connect('localhost', 'root', ''); if (!$con) { die('Connection Error:' . mysql_error()); } mysql_select_db('catgories', $con); $result = mysql_query("SELECT * FROM categories"); $query = "INSERT INTO categories ('products_id', 'categories_id') VALUES ('', '18')"; $i = 1; while ($i <= 17659) { mysql_query($query); $i++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/177793-solved-using-while-statement-to-insert-numbers/ Share on other sites More sharing options...
cags Posted October 15, 2009 Share Posted October 15, 2009 Good for you... are you trying to say that it's not working? If products_id is auto increment and caegories_id is int as you say then you could try this... $query = "INSERT INTO categories ('categories_id') VALUES (18)"; Omit the products_id it's not required on an auto increment field and some versions of MySQL do not like recieving a blank string as a value for auto increment fields. I also removed the single quotes from around categories_id since it is an integer not a string. Quote Link to comment https://forums.phpfreaks.com/topic/177793-solved-using-while-statement-to-insert-numbers/#findComment-937477 Share on other sites More sharing options...
taquitosensei Posted October 15, 2009 Share Posted October 15, 2009 I'm confused. Are you wanting to put 1-17659 for each categoryid pulled from the database? something like this productid categoryid 1 1 2 1 3 1 ..to 17659 1 2 2 2 3 2 ..to 17659 Quote Link to comment https://forums.phpfreaks.com/topic/177793-solved-using-while-statement-to-insert-numbers/#findComment-937479 Share on other sites More sharing options...
Colton.Wagner Posted October 15, 2009 Author Share Posted October 15, 2009 Good for you... are you trying to say that it's not working? If products_id is auto increment and caegories_id is int as you say then you could try this... $query = "INSERT INTO categories ('categories_id') VALUES (18)"; Omit the products_id it's not required on an auto increment field and some versions of MySQL do not like recieving a blank string as a value for auto increment fields. I also removed the single quotes from around categories_id since it is an integer not a string. Didn't help and that shouldn't effect it anyways. Quote Link to comment https://forums.phpfreaks.com/topic/177793-solved-using-while-statement-to-insert-numbers/#findComment-937485 Share on other sites More sharing options...
Colton.Wagner Posted October 15, 2009 Author Share Posted October 15, 2009 I'm confused. Are you wanting to put 1-17659 for each categoryid pulled from the database? something like this productid categoryid 1 1 2 1 3 1 ..to 17659 1 2 2 2 3 2 ..to 17659 Yes, products_id categories 1 76 2 76 3 76 4 76 to 17659.. Quote Link to comment https://forums.phpfreaks.com/topic/177793-solved-using-while-statement-to-insert-numbers/#findComment-937489 Share on other sites More sharing options...
taquitosensei Posted October 15, 2009 Share Posted October 15, 2009 This should do it. There's probably a more efficient way, but this should work. 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); } Quote Link to comment https://forums.phpfreaks.com/topic/177793-solved-using-while-statement-to-insert-numbers/#findComment-937492 Share on other sites More sharing options...
Colton.Wagner Posted October 15, 2009 Author Share Posted October 15, 2009 This should do it. There's probably a more efficient way, but this should work. 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); } Nope it didn't work with no errors. I'm telling you I couldn't figure it out. Quote Link to comment https://forums.phpfreaks.com/topic/177793-solved-using-while-statement-to-insert-numbers/#findComment-937501 Share on other sites More sharing options...
cags Posted October 15, 2009 Share Posted October 15, 2009 When you say it doesn't work, what do you mean? Is nothing getting inserted into the database? Quote Link to comment https://forums.phpfreaks.com/topic/177793-solved-using-while-statement-to-insert-numbers/#findComment-937504 Share on other sites More sharing options...
Colton.Wagner Posted October 15, 2009 Author Share Posted October 15, 2009 Nothing gets inserted into the database and it throws no errors even with the e_all reporting method enabled. It takes awhile to go through all 17659 instances but it does not insert anything. Quote Link to comment https://forums.phpfreaks.com/topic/177793-solved-using-while-statement-to-insert-numbers/#findComment-937505 Share on other sites More sharing options...
cags Posted October 15, 2009 Share Posted October 15, 2009 Add display_errors(1); below error_reporting and change your query call to mysql_query($query) or trigger_error("SQL: $query, ERROR: " . mysql_error(), E_USER_ERROR); Edit: oops forgot to add the actual error. Quote Link to comment https://forums.phpfreaks.com/topic/177793-solved-using-while-statement-to-insert-numbers/#findComment-937510 Share on other sites More sharing options...
ialsoagree Posted October 15, 2009 Share Posted October 15, 2009 Having trouble remembering, doesn't a MySQL error need to be reported explicitly? In other words, if you tried to insert a non-unique value into a unique column, wouldn't you need to report the returned MySQL error explicitly in PHP? Quote Link to comment https://forums.phpfreaks.com/topic/177793-solved-using-while-statement-to-insert-numbers/#findComment-937511 Share on other sites More sharing options...
Colton.Wagner Posted October 15, 2009 Author Share Posted October 15, 2009 Fatal error: SQL: INSERT INTO categories(`products_id`,`categories_id`) select 0,categories_id from categories, ERROR: in F:\Xampp\xampplite\htdocs\categories.php on line 13 Quote Link to comment https://forums.phpfreaks.com/topic/177793-solved-using-while-statement-to-insert-numbers/#findComment-937513 Share on other sites More sharing options...
cags Posted October 15, 2009 Share Posted October 15, 2009 What message do you get when using... $query = "INSERT INTO categories (categories_id) VALUES (18)"; I'm not saying the functionality will work as you intend but it shouldn't throw an error. Edit: The single quotes around the field names would have thrown an error, I just realised I left that in when I last posted this line. Quote Link to comment https://forums.phpfreaks.com/topic/177793-solved-using-while-statement-to-insert-numbers/#findComment-937514 Share on other sites More sharing options...
ialsoagree Posted October 15, 2009 Share Posted October 15, 2009 Is this the entire code? If nothing it happening when the code is run you should almost certainly be getting an error message. Have you tried using view source to see what if any HTML is written to the browser? Quote Link to comment https://forums.phpfreaks.com/topic/177793-solved-using-while-statement-to-insert-numbers/#findComment-937517 Share on other sites More sharing options...
Colton.Wagner Posted October 15, 2009 Author Share Posted October 15, 2009 Is this the entire code? If nothing it happening when the code is run you should almost certainly be getting an error message. Have you tried using view source to see what if any HTML is written to the browser? cags: the same error The html document contains an error now because we set it to tell us. Fatal error: SQL: INSERT INTO categories(`products_id`,`categories_id`) select 0,categories_id from categories, ERROR: in F:\Xampp\xampplite\htdocs\categories.php on line 13 Quote Link to comment https://forums.phpfreaks.com/topic/177793-solved-using-while-statement-to-insert-numbers/#findComment-937518 Share on other sites More sharing options...
cags Posted October 15, 2009 Share Posted October 15, 2009 You need to add the mysql_error() part in that I added with an edit, so that we can see the reason mysql is giving. mysql_query($query) or trigger_error("SQL: $query, ERROR: " . mysql_error(), E_USER_ERROR); Quote Link to comment https://forums.phpfreaks.com/topic/177793-solved-using-while-statement-to-insert-numbers/#findComment-937520 Share on other sites More sharing options...
Colton.Wagner Posted October 15, 2009 Author Share Posted October 15, 2009 i added that and it threw this error: Fatal error: SQL: INSERT INTO categories(`products_id`,`categories_id`) select 0,categories_id from categories, ERROR: in F:\Xampp\xampplite\htdocs\categories.php on line 13 Quote Link to comment https://forums.phpfreaks.com/topic/177793-solved-using-while-statement-to-insert-numbers/#findComment-937523 Share on other sites More sharing options...
cags Posted October 15, 2009 Share Posted October 15, 2009 Well I'm not sure what to say. You obviously haven't changed it as suggested because it's still outputting the SQL statement suggested by taquitosensei... $query = "INSERT INTO categories (categories_id) VALUES (18)"; Quote Link to comment https://forums.phpfreaks.com/topic/177793-solved-using-while-statement-to-insert-numbers/#findComment-937531 Share on other sites More sharing options...
mrMarcus Posted October 15, 2009 Share Posted October 15, 2009 gonna give the thread starter a heads up on how to get help... this... I have 17659 entries that are all the same. There are only two fields in the mysql databse Products_id witch auto increments and categories_id witch is an int 10. This source code throws no errors. means nothing to me, and 99% of the rest of the people on the board. next time, be very descriptive as to what the problem is, the errors that you are receiving, and what you are trying to acheive .. because right off the bat, you lose over 50% of the people who are capable of creating a solution very quickly, like myself. and i'm not going to read all the other posts in the thread just to try and figure out YOUR problem. i love to help, but i only have so much time, and that time was just spent explaining to you how to get what you came for .. a solution. Quote Link to comment https://forums.phpfreaks.com/topic/177793-solved-using-while-statement-to-insert-numbers/#findComment-937533 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.