Jump to content

Runing out of memory in pdo insert [merged with previous thread]


Go to solution Solved by ginerjm,

Recommended Posts

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

Link to comment
Share on other sites

 

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 by bores_escalovsk
Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 2 weeks later...

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));
}
?>
Link to comment
Share on other sites

  • Solution

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?

Link to comment
Share on other sites

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 by bores_escalovsk
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.