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

  • Home
  • SEARCH
  • 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 4251088
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:36:53+00:00 2026-05-21T04:36:53+00:00

I have a stored procedure in MySql to insert some rows. The stored procedure

  • 0

I have a stored procedure in MySql to insert some rows. The stored procedure works fine when calling from the command line and with different values.

Now I have a php script that scrapes a page and then tries to insert items into the db using the script. However only the first item in the list gets inserted.
code snippet:

$dbStatement = $dbHandler->prepare("CALL insertItem(:in_itemID,:in_itemName)");

foreach($matches as $match) { 
    echo '<div>'.$match[3].' '.$match[5].'</div>';
    $dbStatement->bindValue(':in_itemID', $match[3]);
    $dbStatement->bindValue(':in_itemName', $match[5]);
    $dbStatement->execute();
}

So this will echo all a long list of items/ids to insert, but only the first one ever gets inserted (in the stored procedure there is logic to not insert duplicates)

My question is why is it only getting called with the first set of values?
I saw this question A problem while bind params for PDO statement inside loop and tried the suggested solution but it didn’t do anything different – still only the values from the first loop getting inserted.

foreach($matches as $match => $value) { 
    echo '<div>'.$value[3].' '.$value[5].'</div>';
    $dbStatement->bindValue(':in_itemID', $value[3]);
    $dbStatement->bindValue(':in_itemName', value[5]);
    $dbStatement->execute();
}

I am new to PHP and MySql (c# and mssql background)

Thanks!

ADDITIONAL:
To output errors I added (thank you Charles)

$dbHandler->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );

And my error output is

Warning: PDOStatement::execute(): SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active.  Consider using PDOStatement::fetchAll().  Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.

So I did some searching and added/changed (thanks Phil)

if (!$dbStatement->execute()) {
   $err = $dbHandler->errorInfo();
   throw new Exception($err[2]);
}
$result = $dbStatement->fetchAll();

But still getting the same error, even though that looks to be the right syntax for fetchAll();

SOLUTION:

change

$result = $dbStatement->fetchAll();

to

$result = $dbStatement->closeCursor();

Thank you Phil!!

  • 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-05-21T04:36:54+00:00Added an answer on May 21, 2026 at 4:36 am

    As Charles mentioned, make sure errors are reported. You can also check the return value from PDOStatement::execute().

    Another consideration and this is purely subjective, use bindParam() instead of bindValue()

    $dbHandler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $dbStatement = $dbHandler->prepare("CALL insertItem(:in_itemID,:in_itemName)");
    $dbStatement->bindParam('in_itemID', $itemId);
    $dbStatement->bindParam('in_itemName', $itemName);
    
    foreach($matches as $match) {
        $itemId = $match[3];
        $itemName = $match[5];
        if (!$dbStatement->execute()) {
            $err = $dbHandler->errorInfo();
            throw new Exception($err[2]);
        }
    }
    

    Update

    That error is saying it can’t execute because you have an open cursor on a previous statement. You either need to complete fetching, close the cursor using PDOStatement::closeCursor() or enable query buffering.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a MySql stored procedure that inserts a record with some
Well I have this MySQL stored procedure that I wrote and if I run
I have stored procedure: ALTER PROCEDURE [dbo].[k_ShoppingCart_DELETE] @cartGUID nvarchar AS DELETE FROM [dbo].[k_ShoppingCart] WHERE
I am having a strange problem with MySQL Stored Procedure. I have written a
I have a stored procedure in MySQL that should update a column in a
I have a strange, sporadic issue. I have stored procedure that returns back 5
I have a stored procedure in SQL 2005. The Stored Procedure is actually creating
I have a stored procedure that consists of a single select query used to
I have a stored procedure with the following header: FUNCTION SaveShipment (p_user_id IN INTEGER,
I have a stored procedure that returns multiple tables. How can I execute and

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.