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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:10:23+00:00 2026-06-17T04:10:23+00:00

Quick question. Whilst using the prepare method in mysqli. It is possible/a good idea;

  • 0

Quick question. Whilst using the prepare method in mysqli. It is possible/a good idea; to use it twice within the same mysqli connection? Example:

OOP layered

public function getStuff(){
    $posts=array();
    $query = $this->DBH->prepare('SELECT * FROM table WHERE stuff =?');
    $query->bind_param('s','param');
    $query->execute();
    $query->bind_result($ID,$col1,$col2,$etc);
    while($query->fetch()){
        $posts[]=array('ID'=>$ID,'col1'=>$col1,'extras'=>$this->getExtras($ID));
    }
    $query->close();
    return $posts;
}
private function getExtra($postID){
    $extras=array();
    $query = $this->DBH->prepare('SELECT * FROM anotherTable WHERE moreStuff =?');
    $query->bind_param('s',$postID);
    $query->execute();
    $query->bind_result($ID,$col1,$col2,$etc);
    while($query->fetch()){
        $extras[]=array('ID'=>$ID,'col1'=>$col1,'etc'=>$etc);
    }
    $query->close();
    return $extras;
}

Right my possible error is that I’ve used the same variable and the same database connection. I’m not 100% sure this will work as I’ve called $this->DBH whilst it is already being used in the parent function. Is there a better method to what I’m trying to achieve or is there a better structure I can use. Or should I just give up and use a separate variable? lol

Hopeful outcome:

$posts=array('ID'=>'column ID number','col1'=>'column1 data', 'extras'=>array('ID'=>'second table\'s ID number','col1'=>'second tables data','etc'=>'etc etc etc'));
  • 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-17T04:10:24+00:00Added an answer on June 17, 2026 at 4:10 am

    In your example above, the variables which matter are $query. Each of those is local to its own method, and so the variables themselves will not collide. The MySQLi connection $this->DBH is capable of handling multiple open statements at once if circumstances are right.

    The place where you need to use caution is with their execution order. If you prepare and execute a statement but do not fetch all rows from it, you may not be able to prepare() the next one until all rows have been fetched unless you first close it with mysqli_stmt::close() to deallocate the open statement handle.

    For example:

    // Prepares successfully:
    $s1 = $mysqli->prepare("SELECT * FROM t1");
    // Also prepares successfully (previous one not executed)
    $s2 = $mysqli->prepare("SELECT * FROM t2");
    
    // Then consider:
    $s1 = $mysqli->prepare("SELECT id, name FROM t1");
    $s1->bind_result($id, $name);
    $s1->execute();
    // And attempt to prepare another
    $s2 = $mysqli->prepare("SELECT id, name FROM t2");
    // Fails because $s1 has rows waiting to be fetched.
    echo $m->error;
    // "Commands out of sync; you can't run this command now"
    

    Edit: misread your example…

    Looking at your example above, you are indeed calling getExtras() while you are fetching from the getStuff() statement. You may run into the issue described above. Your two operations in this case may be able to be handled with a single JOIN instead, from which you fetch in only one loop to populate all your variables and build your output array as you want it. Depending on your need, this should either be an INNER JOIN if a related row is expected to exist in the othertable, or a LEFT JOIN if the related othertable may or may not have a row that matches the given ID.

    SELECT 
      maintable.id, 
      maintable.col1,
      othertable.col2,
      othertable.col3
    FROM
      maintable
      JOIN othertable ON maintable.id = othertable.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very quick question - whilst the following is possible in C#: var a =
Quick question ... Using J2ME (CLDC 1.1, MIDP-2.1) is it possible to sleep the
Quick question. I am using Eclipse and I am getting the The method must
Quick question: Would it be a good or a bad idea to implement my
Quick question. If i am trying to clear multiple divs with the same class
Quick question, I was wondering if there is a way to use Solr's API
Quick question, I'm starting a linux daemon (motion) from a webpage using a python
Quick question, is it good practice to initialize all blank or empty variables when
Quick question: I'd like to hear your thoughts on when to use State versus
Quick question about general MVC design principle in PHP, using CodeIgniter or Kohana (I'm

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.