Trying to make prepared statements work in a loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ciaran Byrne

    #1

    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 = "";
    $strPlaceholder s = "";

    // Get column names and their properties

    $result = $objSourceDB->query("SHOW COLUMNS FROM $strSourceDB.
    $strSourceTable ") or die("Query: ".$objSourc eDB->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";

    $strPlaceholder s .= "?, ";
    }

    $strColumnNames = substr($strColu mnNames, 0, strlen($strColu mnNames)
    - 2);
    $strPlaceholder s = substr($strPlac eholders, 0,
    strlen($strPlac eholders) - 2);

    // Create prepared statement

    $objStatement = $objTargetDB->stmt_init() or die($objStateme nt-
    >error);
    $objStatement->prepare("INSER T INTO $strTargetDB.$s trTargetTable
    ($strColumnName s) VALUES ($strPlaceholde rs)") or die("Prepare: ".
    $objStatement->error);

    array_unshift($ arrRowData, $strParamTypes) ;

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

    array_shift($ar rRowData);

    // Select and Insert rows

    $objResult = $objSourceDB->query("SELEC T $strColumnNames FROM
    $strSourceDatab ase.$strSourceT able WHERE DateTime <=
    DATE_SUB(CURDAT E(), INTERVAL 90 DAY)") or die("Query: ".$objSourc eDB-
    >error);
    while ($arrRowData = $objResult->fetch_row())
    {
    $objStatement->execute() or die("Execute: ".$objState ment-error);
    }

    ?>

  • Toby A Inkster

    #2
    Re: Trying to make prepared statements work in a loop

    Ciaran Byrne wrote:
    $objStatement->execute()
    You need to pass some parameters to execute().

    --
    Toby A Inkster BSc (Hons) ARCS
    Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

    Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux

    Comment

    • Ciaran Byrne

      #3
      Re: Trying to make prepared statements work in a loop

      On May 17, 5:13 pm, Toby A Inkster <usenet200...@t obyinkster.co.u k>
      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


      says:

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

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

      Comment

      • Toby A Inkster

        #4
        Re: Trying to make prepared statements work in a loop

        Ciaran Byrne wrote:
        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
        Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

        Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux

        Comment

        Working...