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 6174625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:47:04+00:00 2026-05-23T23:47:04+00:00

EDIT (2011-07-23) Have gotten some very helpful answers, both of which I’ve tried implementing.

  • 0

EDIT (2011-07-23)

Have gotten some very helpful answers, both of which I’ve tried implementing. But I can’t seem to get back the id from my Get_Security statement. I’m pretty sure my problem is that, in my first call statement Get_Security, the last three parameters are set to NULL. Seems like other people have the same problem. Doesn’t seem like there’s much documentation on having NULL as an input. How does one go about this?

NEW CODE

$stmt = mysqli_stmt_init($link);
$sql = "CALL Get_Security('$symbol', '$tagName', NULL, NULL, NULL)";
if (!mysqli_stmt_prepare($stmt, $sql)){
    $error = 'Failed to prepare statement. Error No: ' . mysqli_errno($link) . ': ' . mysqli_error($link);
    include '../error.html.php';
    exit();
}
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_assoc($result)) {
    $id = $row['id'];
}
mysqli_stmt_close($stmt);
mysqli_close($link);

include $_SERVER['DOCUMENT_ROOT'] . 'mypath-to-database-link';  //this gets $link
$stmt = mysqli_stmt_init($link);
$sql = "CALL Add_Active('$id','Research')";
if (!mysqli_stmt_prepare($stmt, $sql)){
    $error = 'Failed to prepare statement Add_Active. Error No: ' . mysqli_errno($link) . ': ' . mysqli_error($link);
    include '../error.html.php';
    exit();
}
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
mysqli_close($link);

include $_SERVER['DOCUMENT_ROOT'] . 'mypath-to-database-link'; //this gets $link
$sql = "INSERT INTO MyTable SET
        id='$id',
        open_items='$openItems',
        attachments='$attachments'
    ";
$stmt = mysqli_stmt_init($link);
if (!mysqli_stmt_prepare($stmt, $sql)){
    $error = 'Failed to INSERT INTO Research_Security. Error No: ' . mysqli_errno($link) . ': ' . mysqli_error($link);
    include '../error.html.php';
    exit();
}
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
mysqli_close($link);

ORIGINAL ENTRY

Searched extensively (e.g. PHP Manual, SO questions) but answers are confusing.

I need to execute 3 of SQL statements in a row:

  1. Call stored procedure Get_Security that takes some inputs and returns an array, including the id.

  2. Call another stored procedure Add_Active that takes the returned id from Get_Security as an input.

  3. Insert some variables into my table.

Problem: I’m getting the MySQL Error Number 2014: “Commands out of sync; you can’t run this command now”.

I know I have to use mysqli_stmt_prepare, mysqli_stmt_execute, and mysqli_stmt_close to resolve this, but it’s very confusing how to do this.

Would very much appreciate help in how to translate this using the above functions.

CODE:

$sql = "CALL Get_Security('$symbol', '$tagName', NULL, NULL, NULL)";   
$result = mysqli_query($link, $sql);
if (!$result){
        $error = 'Error calling stored procedure Get_Security.';
        include '../error.html.php';
        exit();
}
while($row = mysqli_fetch_array($result)){
    $tags[] = array('id' => $row['id']);
}
foreach ($tags as $tag){
    $id = $tag['id'];
}


$sql = "CALL Add_Active('$id','Research')";
$result = mysqli_query($link, $sql);
if (!$result){
        $error = 'Error calling stored procedure Add_Active. Error No: ' . mysqli_errno($link) . ': ' . mysqli_error($link);
        include '../error.html.php';
        exit();
}

$sql = "INSERT INTO MyTable SET
            id='$id',
            open_items='$openItems',
            attachments='$attachments'
        ";
if (!mysqli_query($link, $sql)){
        $error = 'Error adding submitted tag into Research_Security. Error No: ' . mysqli_errno($link) . ': ' . mysqli_error($link);
        include '../error.html.php';
        exit();
}
  • 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-23T23:47:05+00:00Added an answer on May 23, 2026 at 11:47 pm

    I hope this helps. From what I can tell you aren’t doing anything too fancy, so this should suffice. PDO does also support IN/OUT params to stored procedures as well, but I didn’t see you using them.

    Please note, PDO handles errors in different ways depending on how it is initialized. So I’ve skipped error handling here. Please let me know if you have questions.

    Also note that until you add a DSN (MySQL’s for example) this code doesn’t care what database type it is, so the DSN can be a config value making your code more portable. I’m sure you could also see how this code could easily be expanded into a class/model structure (specifically the security check SP could become a PHP method)

    $db = new PDO(); // http://www.php.net/manual/en/pdo.construct.php for params
    
    // These generate PDO_Statement (see: http://www.php.net/manual/en/class.pdostatement.php)
    $securityStmt = $db->prepare("CALL Get_Security( ?, ?, ?, ?, ? )");
    $addActiveStmt = $db->prepare("CALL Add_Active( ?, ? )");
    $insertStmt = $db->prepare("INSERT INTO MyTable SET id=?, open_items=?, attachments=?");
    
    // Security CALL
    $securityStmt->bindParam( 1, $symbol, PDO::PARAM_STR );
    $securityStmt->bindParam( 2, $tagName, PDO::PARAM_STR );
    $securityStmt->bindParam( 3, NULL, PDO::PARAM_NULL );
    $securityStmt->bindParam( 4, NULL, PDO::PARAM_NULL );
    $securityStmt->bindParam( 5, NULL, PDO::PARAM_NULL );
    
    $securityStmt->execute();
    
    // Bind the ID to a variable is useful sometimes...
    $securityStmt->bindColumn( 'id', $securityId );
    $securityStmt->fetch( PDO::FETCH_BOUND );
    
    /*
        Insert + Active call
        These are much simpler because we don't need to set the data types of the input
        (they are all string I hope...you didn't mention what the last 2 were in the insert).
    */
    
    $addActiveStmt->execute(
        array(
            $securityId,
            'Wedge Research'
        )
    );
    
    $insertStmt->execute(
        array(
            $securityId,
            $openItems,
            $attachments
        )
    );
    

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

Sidebar

Related Questions

I have some grid which use form edit. But after edit success then page
I have a live db that has some 2011 dates that were entered as
I have one Case form which is resolved,and i want to edit the case
I have an array List<SomeObject> which contain ascending order of dates (and some more
I have the following date: 2011-05-24T11:40:41Z How can I convert this into a Joda
I have a path like this: /news/2011/05/26/some-story-path How do I find the nid for
I have a plist file and i want edit this file, but setObject() in
I have seen some places referring the C++11 standard as ISO/IEC 14882:2011(E) , while
I have tried running the code without the collision, and it works fine. But
EDIT: See my answer below--> I am wanting to have a view that when

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.