473,466 Members | 1,354 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Trying to make prepared statements work in a loop

I'm trying to move data from one or more tables to identical table(s)
in a different database, likely on a different server. This prevents
me from using INSERT..SELECT, so up until now I've done a select on
the source table and looped through the results inserting them
individually into the target table. I thought that a prepared
statement might improve the performance, but I can't get it to work.
Below is some sample code, and I imagine it's in the binding of
$arrRowData (using call_user_func_array) that's not working. Any ideas
welcome.

<?php

$strSourceDB = "dbSource";
$strTargetDB = "dbTarget";

$objSourceDB = mysqli_connect("", "", "", $strSourceDB);
$objTargetDB = mysqli_connect("", "", "", $strTargetDB);

$strSourceTable = "tblA";
$strTargetTable = "tblB";

$arrRowData = array();
$strColumnNames = "";
$strParamTypes = "";
$strPlaceholders = "";

// Get column names and their properties

$result = $objSourceDB->query("SHOW COLUMNS FROM $strSourceDB.
$strSourceTable") or die("Query: ".$objSourceDB->error);

while ($row = $result->fetch_row())
{
$arrRowData[] = "";

$strColumnNames .= $row[0].", ";

if (strpos($row[1], "int") || strpos($row[1], "bool"))
$strParamTypes .= "i";
elseif (strpos($row[1], "blob")) $strParamTypes .= "b";
elseif (strpos($row[1], "big") || strpos($row[1], "double"))
$strParamTypes .= "d";
else $strParamTypes .= "s";

$strPlaceholders .= "?, ";
}

$strColumnNames = substr($strColumnNames, 0, strlen($strColumnNames)
- 2);
$strPlaceholders = substr($strPlaceholders, 0,
strlen($strPlaceholders) - 2);

// Create prepared statement

$objStatement = $objTargetDB->stmt_init() or die($objStatement-
>error);
$objStatement->prepare("INSERT INTO $strTargetDB.$strTargetTable
($strColumnNames) VALUES ($strPlaceholders)") or die("Prepare: ".
$objStatement->error);

array_unshift($arrRowData, $strParamTypes);

if (!call_user_func_array(array(&$objStatement, "bind_param"),
$arrRowData)) die("Bind Param: ".$objStatement->error);

array_shift($arrRowData);

// Select and Insert rows

$objResult = $objSourceDB->query("SELECT $strColumnNames FROM
$strSourceDatabase.$strSourceTable WHERE DateTime <=
DATE_SUB(CURDATE(), INTERVAL 90 DAY)") or die("Query: ".$objSourceDB-
>error);
while ($arrRowData = $objResult->fetch_row())
{
$objStatement->execute() or die("Execute: ".$objStatement-error);
}

?>

May 17 '07 #1
3 2651
Ciaran Byrne wrote:
$objStatement->execute()
You need to pass some parameters to execute().

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
May 17 '07 #2
On May 17, 5:13 pm, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
Ciaran Byrne wrote:
$objStatement->execute()

You need to pass some parameters to execute().

--
Toby A Inkster BSc (Hons) ARCShttp://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
http://uk.php.net/manual/en/function...mt-execute.php

says:

Object oriented style (method):
class mysqli_stmt {
bool execute ( void )
}

i.e. $objStatement->execute() has no parameters.

May 18 '07 #3
Ciaran Byrne wrote:
http://uk.php.net/manual/en/function...mt-execute.php
Oh yes -- I missed that you were using MySQLi -- had assumed PDO.

In which case, the problem is probably with call_user_func_array passing
its array elements by value, but bind_param expecting to receive its
arguments passed by reference.

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
May 18 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mark | last post by:
hello! mysqli in PHP5 comes with prepared statements functionality. However, without persistent connections or connection pooling in this code library, one has to ask: why bother? are...
1
by: RayP | last post by:
I'd appreciate some help I'm having trying to run a cursor. First, some background. The Status field of all records on Table A needs changing from 1 to 0 where there is no corresponding record...
1
by: Tom D | last post by:
I'm rewriting a database interface that our company currently has. Currently it's using the Pear::DB interface, but we found that that was introducing a bit too much overhead. I'm rewriting the...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
14
by: dba_222 | last post by:
Dear experts, Again, sorry to bother you again with such a seemingly dumb question, but I'm having some really mysterious results here. ie. Create procedure the_test As
2
by: Pugi! | last post by:
It is by accident that I noticed that I forgot to use mysql_real_escape_string in part of my webapp. I tested input with following text : Hélène 51°56'12'' http://www.mysite.org/folder 3 functions...
4
by: TechieGrl | last post by:
Prepared statements are new to me and having to do this with a multi- dimensional array is beyond me. Here is the prepared statement block: // Prepare to insert a record into table1...
2
by: clumsy_ninja | last post by:
Why will the first snippet of code will not work with prepared statements, while the second example works fine? $table_name = 'my_table'; $query = 'SELECT * FROM ?'; if ($stmt->prepare($query))...
6
by: Gordon | last post by:
I want to search a folder structure represented in a database. I'm using the ID and Parent columns model, and would like to be able to search a folder's subfolders as well as teh folder itself. ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.