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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:47:52+00:00 2026-06-07T11:47:52+00:00

Why it stops to work on second $mysqli->prepare($query) statement? $mysqli = @new mysqli(HOSTNAME, USERNAME,

  • 0

Why it stops to work on second $mysqli->prepare($query) statement?

$mysqli = @new mysqli(HOSTNAME, USERNAME, PASSWORD, DATABASE);
...
if ($stmt = $mysqli->prepare($query)) {
    // Code is working fine
    ...
    if ($stmt2 = $mysqli->prepare($query2)) {
        // Code does not work
        ...
    }
}

And work fine with repeated mysqli connection:

$mysqli = @new mysqli(HOSTNAME, USERNAME, PASSWORD, DATABASE);
$mysqli2 = @new mysqli(HOSTNAME, USERNAME, PASSWORD, DATABASE);
...
if ($stmt = $mysqli->prepare($query)) {
    // Code is working fine
    ...
    if ($stmt2 = $mysqli2->prepare($query2)) {
        // Code is working fine
        ...
    }
}

How to avoid repeated mysqli connection for second prepare statement?


Update: As I see the community wants a real example:

db table fruits with data:

CREATE TABLE IF NOT EXISTS `fruits` (
  `id` varchar(8) NOT NULL,
  `group` varchar(8) NOT NULL,
  `name` varchar(250) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `fruits` (`id`, `group`, `name`) VALUES
('03E7', '', 'Berries'),
('0618', '03E7', 'blueberry'),
('051B', '03E7', 'raspberry'),
('02AA', '03E7', 'strawberry'),
('035F', '', 'Citrus'),
('07A5', '035F', 'grapefruit'),
('0633', '035F', 'lime'),
('05E1', '', 'Pear');

php script:

<?php

$mysqli = new mysqli('localhost', 'root', 'password', 'test');
//$mysqli2 = new mysqli('localhost', 'root', 'password', 'test');

$query1 = "SELECT id, name FROM fruits WHERE `group`=''";
$query2 = "SELECT name FROM fruits WHERE `group`=?";

$stmt1 = $mysqli->stmt_init();
$stmt2 = $mysqli->stmt_init();
//$stmt2 = $mysqli2->stmt_init();

if($stmt1->prepare($query1)){
    $stmt1->execute();
    $stmt1->bind_result($id, $name1);

    while($stmt1->fetch()){
        echo $name1;

        if($stmt2->prepare($query2)){
            $stmt2->bind_param('s', $group);
            $group = $id;
            $stmt2->execute();
            $stmt2->bind_result($name2);

            echo ':';

            while($stmt2->fetch()){
                echo ' ' . $name2 . ',';
            }
        }

        echo '<br>';
    }
}

?>

Result:

Berries
Citrus
Pear

Expected result:

Berries: blueberry, raspberry, strawberry,
Citrus: grapefruit, lime,
Pear:
  • 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-07T11:47:53+00:00Added an answer on June 7, 2026 at 11:47 am

    To fix the issue remove

    $stmt1 = $mysqli->stmt_init();
    $stmt2 = $mysqli->stmt_init();
    

    and add after $stmt1->execute();

    $stmt1->store_result();
    

    and replace $stmt1->prepare($query1), $stmt2->prepare($query1) with

    $stmt1 = $mysqli->prepare($query1)
    $stmt2 = $mysqli->prepare($query2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why doesn't this work? The program stops on: this.textBox1.Text = (New text); Thread demoThread;
FINAL UPDATE: The second code block is working--I just miss-typed the URL. I'm trying
The xmlns attribute in the following code stops me getting the value I need.
The function associated with the selector stops working when I replace it's contents using
Currently, my cURL multi exec stops if one url it connects to doesn't work,
For work I'm dealing with a large database (160 million + rows a year,
Autofocus is not working on the first AVCaptureSession when I create a second AVCaptureSession.
Hibernate 3.6.8.Final Does anyone know how example criterias in hibernate work with the query
EDIT: It does work (sorry). Something in this script is causing it to stop
Does the G'MIC (Grey's Magic Image Converter) library work on iOS ? what steps

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.