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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:56:20+00:00 2026-06-07T05:56:20+00:00

How to convert this simple MySQL example to MySQLi object oriented style: $query =

  • 0

How to convert this simple MySQL example to MySQLi object oriented style:

$query = mysql_query("SELECT id, name FROM fruits WHERE `group`=''");

if ($query) {
    while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
        echo $row['name'];

        $query2   = mysql_query("SELECT name FROM fruits WHERE `group`='{$row['id']}'");

        if (mysql_num_rows($query2)) {
            echo ':';

            while ($row2 = mysql_fetch_array($query2, MYSQL_ASSOC)) {
                echo ' '. $row2['name'] . ',';
            }
            mysql_free_result($query2);

            echo '<br>';
        }
    }
    mysql_free_result($query);
}

Result:

Berries: blueberry, raspberry, strawberry,
Citrus: grapefruit, lime,
Pear

Database structure SELECT * FROM fruits:

id   | group | name
-------------------------
03E7 |       | Berries
0618 | 03E7  | blueberry
051B | 03E7  | raspberry
02AA | 03E7  | strawberry
035F |       | Citrus
07A5 | 035F  | grapefruit
0633 | 035F  | lime
05E1 |       | 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-07T05:56:22+00:00Added an answer on June 7, 2026 at 5:56 am

    Full working code:

    <?php
    
    $mysqli = new mysqli('localhost', 'root', '', 'test');
    
    $query1 = 'SELECT id, name FROM fruits WHERE `group` = \'\'';// AND id = \'03E7\'
    $query2 = 'SELECT name FROM fruits WHERE `group` = ?';
    
    if ($stmt1 = $mysqli->prepare($query1)) {
        $stmt1->execute();
        $stmt1->store_result();
        $stmt1->bind_result($id, $name);
    
        while ($stmt1->fetch()) {
            echo $name;
    
            if ($stmt2 = $mysqli->prepare($query2)) {
                $stmt2->bind_param('s', $id);
                $stmt2->execute();
                $stmt2->store_result();
                $stmt2->bind_result($name);
    
                $count = $stmt2->num_rows;
    
                if ($count > 0) {
                    echo ':';
    
                    $i = 1;
                    while ($stmt2->fetch()) {
                        echo ' ' . $name;
    
                        if($i < $count) {
                           echo ',';
                        } else {
                           echo '.';
                        }
                        $i++;
                    }
                    $stmt2->free_result();
                    $stmt2->close();
                }
                echo '<br>';
            }
        }
        $stmt1->free_result();
        $stmt1->close();
    }
    
    ?>
    

    Improved result:

    Berries: blueberry, raspberry, strawberry.
    Citrus: grapefruit, lime.
    Pear
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert this SQL query to L2S.. SELECT TOP 5 vt.* FROM
Let's say I have the following simple query SELECT TOP 1 name FROM months
can any one know the, convert mysql query in to an php array: this
This is so simple I'm embarrassed to ask, but how do you convert a
Based on this code below I use for regular mysql, how could I convert
Is anyone aware of a simple way to convert mysql datetime values to coldfusion
I am getting this error while I am trying to execute a simple SELECT
this is my simple query written in ms sql server... now i need to
actually I need to write just a simple program in JAVA to convert MySQL
I think this is a simple question. We have a MySQL database with a

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.