Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8101177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:55:15+00:00 2026-06-05T22:55:15+00:00

So I am trying to move selected rows from 1 table to another in

  • 0

So I am trying to “move” selected rows from 1 table to another in different databases.

It in theory works (but if anyone wants to give any opinions please do, I am very new to PDO. I however keep getting a “SQLSTATE[HY000]: General error” error.

Any advice?

  private function broken() {
    try {
        $sql = "SELECT * FROM `calls` WHERE `calls`.`status`=0 AND `calls`.`stage` < 4 AND `calls`.`answer` < (NOW() + INTERVAL 10 MINUTE)";
        $query = $this->staging->query($sql);
        while($row = $query->fetch(PDO::FETCH_ASSOC)) {

            // Insert in production database:
            $sql = "INSERT INTO `ivr_incomplete` (`id`,`sip_id`,`extension`,`caller_id`,`stage`,`status`,`survey_id`,`start`,`answer`,`hangup`,`end`) VALUES (:id, :sip_id, :extension, :caller_id, :stage, :status, :survey_id, :start, :answer, :hangup, :end)";
            $query = $this->production->prepare($sql);
            $query->execute($row);

            // Delete from staging:
            $sql = "DELETE FROM `calls` WHERE `id`='".$row['id']."'";
            $this->staging->query($sql);

        }
    }
    catch(PDOException $e) {
        $this->informer("FATAL", "Unable to process broken IVR surveys. Error: ".$e->getMessage());
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:55:16+00:00Added an answer on June 5, 2026 at 10:55 pm

    Two points:

    1. You are preparing the INSERT on every iteration, which sort of eliminates half of the point of using a prepared statement – all you are using it for is escaping. One of the points of prepared statements is that the query is only parsed once, so if you need to execute the same query repeatedly with different values, calling prepare() once and then simply calling execute() with the different data sets can significantly boost performance.

    2. This whole thing could be accomplished in 2 queries: Removed due to use of two separate DB connections

    EDIT

    Try this code:

    You will likely need to adjust the error handling to meet your needs, particularly around how it is handled if there is an error with an INSERT, since I doubt you would want to break the whole operation and leave the rows that have been successfully processed in the source table.

    private function broken() {
    
        try {
    
            // Fetch records to move
            $sql = "
              SELECT *
              FROM `calls`
              WHERE `status` = 0
                AND `stage` < 4
                AND `answer` < (NOW() + INTERVAL 10 MINUTE)
            ";
            $query = $this->staging->query($sql);
            if (!$query) {
                $errorInfo = $this->staging->errorInfo();
                throw new Exception("MySQL error at SELECT: $errorInfo[1] ($errorInfo[0]): $errorInfo[2]");
            }
    
            // Prepare the INSERT statement
            $sql = "
              INSERT INTO `ivr_incomplete`
                (`id`,`sip_id`,`extension`,`caller_id`,`stage`,`status`,`survey_id`,`start`,`answer`,`hangup`,`end`)
              VALUES
                (:id, :sip_id, :extension, :caller_id, :stage, :status, :survey_id, :start, :answer, :hangup, :end)
            ";
            if (!$stmt = $this->production->prepare($sql)) {
                $errorInfo = $this->production->errorInfo();
                throw new Exception("MySQL error at prepare INSERT: $errorInfo[1] ($errorInfo[0]): $errorInfo[2]");
            }
    
            // A list of the row IDs we are working with
            $rowIds = array();
    
            // Loop the results and insert them
            for ($i = 1; $row = $query->fetch(PDO::FETCH_ASSOC); $i++) {
    
                if (!$stmt->execute($row)) {
                    $errorInfo = $stmt->errorInfo();
                    throw new Exception("MySQL error at INSERT row $i (id: {$row['id']}): $errorInfo[1] ($errorInfo[0]): $errorInfo[2]");
                }
    
                $rowIds[] = (int) $row['id'];
    
            }
    
            // Delete from staging:
            if ($rowIds) {
    
                $sql = "
                  DELETE FROM `calls`
                  WHERE `id` IN (".implode(', ', $rowIds).")
                ";
                if (!$this->staging->query($sql)) {
                    $errorInfo = $this->staging->errorInfo();
                    throw new Exception("MySQL error at DELETE: $errorInfo[1] ($errorInfo[0]): $errorInfo[2]");
                }
    
            }
    
        } catch(PDOException $e) {
    
            $this->informer("FATAL", "Unable to process broken IVR surveys (PDO). Error: ".$e->getMessage());
    
        } catch (Exception $e) {
    
            $this->informer("FATAL", "Unable to process broken IVR surveys (MySQL). Error: ".$e->getMessage());
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to move the data from one table to another based on names
I'm trying to move data from a selected row in a table to a
I'm trying to move a control from one parent to another (if this will
Ok so I'm trying to move items from one listbox to another by using
I am trying to pass a selected checkbox value from one page to another
I'm trying to move the menu structure from a JMenu into a JMenuBar. The
I'm trying to move a MySQL database from a Linux machine to one running
I'm trying to move some Excel-Data to MySQL, but having troubles with encoding. What
I'm trying to move a file but I want to ensure that it exists
I'm trying to move the status bar in Android (Froyo) from the top of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.