dreamwest Posted January 4, 2009 Share Posted January 4, 2009 im just learning mysql and im trying to use variables in the "INSERT INTO" command can anyone tell me what im doing wrong here: mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("dbname") or die(mysql_error()); // Retrieve all the data from the "example" table $result = mysql_query("SELECT * FROM example") or die(mysql_error()); echo "ok"; // store the record of the "example" table into $row $row = mysql_fetch_array( $result ); mysql_query("INSERT INTO example_2 (column1, column2) VALUES($row['column1'], $row['column2']) ") or die(mysql_error()); echo "ok 2"; Im basically just transferring: Table: example Columns: column1 and column2 TO: Table: example2 Columns: column1 and column2 any help would be appreciated...... Quote Link to comment https://forums.phpfreaks.com/topic/139412-insert-into/ Share on other sites More sharing options...
salami1_1 Posted January 4, 2009 Share Posted January 4, 2009 hmm don't really see any error.. what is the error you receive? Quote Link to comment https://forums.phpfreaks.com/topic/139412-insert-into/#findComment-729248 Share on other sites More sharing options...
xtopolis Posted January 4, 2009 Share Posted January 4, 2009 mysql_query("INSERT INTO example_2 (column1, column2) VALUES($row['column1'], $row['column2']) ") I think it's a problem with your $row['column1/2']; mysql values need to be in quotes. mysql_query("INSERT INTO example_2 (column1, column2) VALUES ('".$row['column1']."', '".$row['column2']."')); Quote Link to comment https://forums.phpfreaks.com/topic/139412-insert-into/#findComment-729249 Share on other sites More sharing options...
dreamwest Posted January 4, 2009 Author Share Posted January 4, 2009 mysql_query("INSERT INTO example_2 (column1, column2) VALUES($row['column1'], $row['column2']) ") I think it's a problem with your $row['column1/2']; mysql values need to be in quotes. mysql_query("INSERT INTO example_2 (column1, column2) VALUES ('".$row['column1']."', '".$row['column2']."')); Thanks that was it! Can i ask one more question about the same script... I have 100 rows in column1, how can i make this transfer all rows over instead of just one. Ive tried the while command but no luck: mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("dbname") or die(mysql_error()); // Retrieve all the data from the "example" table $result = mysql_query("SELECT * FROM example") or die(mysql_error()); echo "ok"; // store the record of the "example" table into $row while($row = mysql_fetch_array($result)) { mysql_query("INSERT INTO example_2 (column1, column2) VALUES ('".$row['column1']."', '".$row['column2']."')); or die(mysql_error()); } echo "ok2"; Quote Link to comment https://forums.phpfreaks.com/topic/139412-insert-into/#findComment-729252 Share on other sites More sharing options...
fenway Posted January 4, 2009 Share Posted January 4, 2009 I have 100 rows in column1, how can i make this transfer all rows over instead of just one. What are you trying to accomplish??? Quote Link to comment https://forums.phpfreaks.com/topic/139412-insert-into/#findComment-729263 Share on other sites More sharing options...
dreamwest Posted January 4, 2009 Author Share Posted January 4, 2009 I have 100 rows in column1, how can i make this transfer all rows over instead of just one. What are you trying to accomplish??? Im trying to seperate the 2 tables just in case i screw something up. Once i have the info in example2 table i can use another script to work of that table instead of the example table. Of course example2 table will have extra columns so this will make it easier when writing scripts Quote Link to comment https://forums.phpfreaks.com/topic/139412-insert-into/#findComment-729264 Share on other sites More sharing options...
dreamwest Posted January 5, 2009 Author Share Posted January 5, 2009 Im trying to seperate the 2 tables just in case i screw something up. Once i have the info in example2 table i can use another script to work of that table instead of the example table. Of course example2 table will have extra columns so this will make it easier when writing scripts I scrapped this idea and just addedd the extra columns to the original table Quote Link to comment https://forums.phpfreaks.com/topic/139412-insert-into/#findComment-729693 Share on other sites More sharing options...
fenway Posted January 5, 2009 Share Posted January 5, 2009 So, solved? Quote Link to comment https://forums.phpfreaks.com/topic/139412-insert-into/#findComment-729738 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.