bores_escalovsk Posted June 8, 2014 Share Posted June 8, 2014 finally got some time to code anyway i cant figure out why this doesnt work, i think its an error in my logic... <?php $db = new PDO('mysql:host=localhost; dbname=test;', 'root', ''); $db2 = new PDO('mysql:host=localhost; dbname=test2;', 'root', ''); foreach($db->query('SELECT * FROM database1 LIMIT 1') as $row) { $thing1 = $row['COL 1']; $thing2 = $row['COL 2']; $thing3 = $row['COL 4']; $thing4 = $row['COL 6']; $thing5 = $row['COL 7']; $content = "\"big text with $things\""; addslashes($content); $db2->exec("INSERT INTO database2(col1,col2,col3) VALUES ('$thing1','text','$content')"); } ?> my foreach command should read all lines from database one and write one by one on database two? thx in advance Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted June 8, 2014 Share Posted June 8, 2014 Not when you have LIMIT 1 in your first query. Why are you duplicating data in separate databases? Quote Link to comment Share on other sites More sharing options...
bores_escalovsk Posted June 8, 2014 Author Share Posted June 8, 2014 Not when you have LIMIT 1 in your first query. Why are you duplicating data in separate databases? im modifying data in the process , i tried with out the limit 1 but database 1 got many many rows and i eventually run out of memory Quote Link to comment Share on other sites More sharing options...
Barand Posted June 8, 2014 Share Posted June 8, 2014 Also it's extremely inefficient ot run queries inside loops, especially when a single query will do the job. EG INSERT INTO database2(col1,col2,col3) SELECT thing1, thing2, thing3 FROM database1 Quote Link to comment Share on other sites More sharing options...
bores_escalovsk Posted June 8, 2014 Author Share Posted June 8, 2014 (edited) Also it's extremely inefficient ot run queries inside loops, especially when a single query will do the job. EG INSERT INTO database2(col1,col2,col3) SELECT thing1, thing2, thing3 FROM database1 sorry by the extreme n00b question but i can insert and select from different databases in the same query???? mind blow Edited June 8, 2014 by bores_escalovsk Quote Link to comment Share on other sites More sharing options...
Barand Posted June 8, 2014 Share Posted June 8, 2014 see http://dev.mysql.com/doc/refman/5.6/en/insert-select.html Quote Link to comment Share on other sites More sharing options...
bores_escalovsk Posted June 8, 2014 Author Share Posted June 8, 2014 see http://dev.mysql.com/doc/refman/5.6/en/insert-select.html thanks a freaking lot, but how do i modify what i am writing in database 2 in this way? Quote Link to comment Share on other sites More sharing options...
Barand Posted June 8, 2014 Share Posted June 8, 2014 Frankly, I have no idea. With all your "thing" references there is no way of knowing exactly what you are trying to do in order to answer that question, but something along the lines of INSERT INTO database2(col1,col2,col3) SELECT thing1, 'text', '$content' FROM database1 but I do not how how you want to derive $content from the code you gave Quote Link to comment Share on other sites More sharing options...
bores_escalovsk Posted June 17, 2014 Author Share Posted June 17, 2014 the code works with 10 lines but when i jump to a million it just go crazy on ram memory... any help? <?php $db2 = new PDO('mysql:host=localhost; dbname=dbname1;', 'root', ''); $db2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); foreach($db2->query('SELECT * FROM dbname2') as $row) { $href = $row['href']; $title = $row['title']; $thumb = $row['thumb']; $tags = $row['tags']; $category = $row['category']; $content = " file=$strfinal image=$thumb]\"; [/php] #####$tags##### %%%%%$category%%%%% !!!!!$title!!!!!"; addslashes($content); $sql = "INSERT INTO wp2_posts(post_content) VALUES (:parameter)"; $q = $db2->prepare($sql); $q->execute(array(':parameter'=>$content)); } ?> Quote Link to comment Share on other sites More sharing options...
Solution ginerjm Posted June 17, 2014 Solution Share Posted June 17, 2014 1 - you are using prepared statements so no need to add slashes. 2 - The beauty of prepared statements is you only prepare it once and then run the execute with the specific arguments you have for each query. Move the prepare up before your loop. 3 - I don't understand your content at all, but it appears you have one odd " mark in the string each time. Is that what you intend? Plus - what is [\php] supposed to represent? Quote Link to comment Share on other sites More sharing options...
bores_escalovsk Posted June 17, 2014 Author Share Posted June 17, 2014 (edited) 1 - you are using prepared statements so no need to add slashes. 2 - The beauty of prepared statements is you only prepare it once and then run the execute with the specific arguments you have for each query. Move the prepare up before your loop. 3 - I don't understand your content at all, but it appears you have one odd " mark in the string each time. Is that what you intend? Plus - what is [\php] supposed to represent? <?php $db2 = new PDO('mysql:host=localhost; dbname=dbname1;', 'root', ''); $db2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); $sql = "INSERT INTO wp2_posts(post_content) VALUES (:parameter)"; $content = "#####$tags##### %%%%%$category%%%%% !!!!!$title!!!!!"; addslashes($content); foreach($db2->query('SELECT * FROM dbname2') as $row) { $href = $row['href']; $title = $row['title']; $thumb = $row['thumb']; $tags = $row['tags']; $category = $row['category']; $q = $db2->prepare($sql); $q->execute(array(':parameter'=>$content)); } ?> so final result should be this ?? .-. im confused, should i dump the variables at the end of the loop or im talking non sense ? Edited June 17, 2014 by bores_escalovsk Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 17, 2014 Share Posted June 17, 2014 You didn't follow my #1 or #2 tips. Quote Link to comment Share on other sites More sharing options...
bores_escalovsk Posted June 17, 2014 Author Share Posted June 17, 2014 You didn't follow my #1 or #2 tips. php obi-wan kenobi i will solve that but how can is solve my ram problems , seriously is using more than 2gb of ram ...run line by line or something Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 17, 2014 Share Posted June 17, 2014 My plan is that using the prepare repeatedly is eating up your ram. Can't hurt to try my recommendations. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted June 17, 2014 Share Posted June 17, 2014 this is the second thread you have started for this task. in your previous thread, you were shown how you can run ONE query, with no loops. i am merging your two threads together so that anyone currently replying will have the information presented in the previous thread. if your $content string you are showing use is what you are really doing, you would form that expression in the SELECT part of the INSERT ... SELECT Syntax you were given in the previous thread Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 17, 2014 Share Posted June 17, 2014 Seems like I'm wasting my time here. You've been supplied with ample advice that you apparently don't want to use. Why should I expect you to follow my advice then, especially since you have already ignored my first response? Good bye. Quote Link to comment Share on other sites More sharing options...
bores_escalovsk Posted June 18, 2014 Author Share Posted June 18, 2014 as always i screwed up everything(sorry) , thanks for the patience , i will follow the tips and figure out the rest my self Quote Link to comment 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.