kamlooper Posted April 6, 2011 Share Posted April 6, 2011 I have tried several attempts and can get data to select and using echo display it, however, I need to take this data and insert it into the database in a separate table. I have the following which does hafl the job, can I get some pointers on the rest. I have looked everywhere and not found a solution, at all. // Selects the data I need <?php mysql_connect("PRIVATE INFO","PRIVATE INFO","PRIVATE INFO") or die("Could not connect: " . mysql_error()); mysql_select_db("wpdb"); $result = mysql_query("SELECT ID FROM wp_posts WHERE post_title LIKE '%future%' AND post_status = 'publish' OR post_title LIKE '%option%' AND post_content LIKE '%fundamental%' AND post_status = 'publish' ORDER BY post_date DESC"); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo $row['ID']; echo "<br />";}; ?> Thought maybe something like the following would work, but am at a loss: INSERT INTO wp_term_relationships (object_id, term_taxonomy_id, term_order) SELECT ID FROM wp_posts WHERE post_title LIKE '%future%' AND post_status = 'publish' OR post_title LIKE '%option%' AND post_content LIKE '%fundamental%' AND post_status = 'publish' ORDER BY post_date DESC while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) VALUES ($row['ID'], '25', '0') Link to comment https://forums.phpfreaks.com/topic/232866-how-do-i-take-data-from-a-select-query-and-insert-into-another-table/ Share on other sites More sharing options...
gristoi Posted April 6, 2011 Share Posted April 6, 2011 Try doing it in one query with a nested select: INSERT INTO table2 ( value1, value2, value3) VALUES ( SELECT value1, value2, value3 FROM table1 WHERE something = something) Link to comment https://forums.phpfreaks.com/topic/232866-how-do-i-take-data-from-a-select-query-and-insert-into-another-table/#findComment-1197722 Share on other sites More sharing options...
kamlooper Posted April 6, 2011 Author Share Posted April 6, 2011 I have looked at your suggestion and have a few questions. I am only pulling the ID information from one table to post into a second, leaving the other two columns to be auto populated with information I will provide. Example (value1, value2, value3) = (value1 carried out multiple times from a looking array, value2 a number i set, lavue 3 again a number i set) (row[iD], 25, 0) I dont know if this makes any sense, I have been looking at it so long i am cross eyed. basically i want to take ID data (multiple results) from a query of one table and insert it with other hard coded data into a separate table? Link to comment https://forums.phpfreaks.com/topic/232866-how-do-i-take-data-from-a-select-query-and-insert-into-another-table/#findComment-1197725 Share on other sites More sharing options...
kamlooper Posted April 6, 2011 Author Share Posted April 6, 2011 I used the following and it worked!!! THANK YOU. $sql="INSERT INTO wp_term_relationships (field1, field2, field3) SELECT ID, hardcoded number1, hardcoded number2 FROM wp_posts WHERE post_title LIKE '%future%' AND post_status = 'publish' OR post_title LIKE '%option%' AND post_content LIKE '%fundamental%' AND post_status = 'publish' "; if (!mysql_query($sql,$con)) { die('Unfortunately the ID chosen is already in use please click the back button and select a different ID.' ); } Link to comment https://forums.phpfreaks.com/topic/232866-how-do-i-take-data-from-a-select-query-and-insert-into-another-table/#findComment-1197812 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.