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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:36:38+00:00 2026-05-20T10:36:38+00:00

I have basic question about mysqli prepared statements. For example, I want to execute

  • 0

I have basic question about mysqli prepared statements. For example, I want to execute a SELECT query, Should I do it like:

<?
$city = "Amersfoort";

if ($stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?")) {
    $stmt->bind_param("s", $city);
    $stmt->execute();
    $stmt->bind_result($district);
    $stmt->close();
}
$mysqli->close();
?>

In the above code, is bind_result also required? What exactly it does?

Also, do I need need to close mysqli connection after each query?
Thanks.

  • 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-20T10:36:39+00:00Added an answer on May 20, 2026 at 10:36 am

    bind_result makes it so that when you iterate over the results of the query, the columns from the result set are automatically mapped to local variables.

    For example, suppose you execute a query that returns a result set with three columns like this:

    $query = "SELECT Name, CountryCode, District FROM myCity";
    

    You want to execute the query and do something with the results, let’s say print them:

    if ($result = $mysqli->query($query)) {
        while ($row = $result->fetch_row()) {
            printf("%s (%s,%s)\n", $row[0], $row[1], $row[2]);
        }
    }
    

    The “problem” with the above code is that $row[0] is not very descriptive. An alternative way is to use bind_result, which goes like this:

    $query = "SELECT Name, CountryCode, District FROM myCity";
    
    if ($stmt = $mysqli->prepare($query)) {
        $stmt->bind_result($name, $countryCode, $district);
    
        while ($stmt->fetch()) {
            printf("%s (%s,%s)\n", $name, $countryCode, $district);
        }
    }
    

    As you see, when using bind_result each time you call fetch the variables $name, $countryCode, $district are automatically populated with the values from the current result row. There are some details that you must ensure, read the documentation for more info.

    To answer your other question: you do not need to, and indeed you must not close the connection after each query (unless you know very well what you are doing).

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

Sidebar

Related Questions

I have a basic question about JSF and their ManagedBeans. Imagine we have set
Very basic question about iPhone memory management: Say I have a viewController with several
I have a question about when you distribute your app with an existing DB.
I have a general question about WPF performance. We have a relatively simple forms
Sorry for this basic question, but I've been looking over all the info about
New to rails and I have what I think is a basic question. In
I want to ask a question about the tomcat and the Linux terminal. I
I'm trying to tweak my MySQL server to fit my needs... and I have
I'm about to begin development of an iPhone app. The app itself is fairly
Hey Im working on a folder tree and need to be able to limit

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.