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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:32:20+00:00 2026-06-05T05:32:20+00:00

I’m making a PHP frontend for a MySQL database. I’ve got a table called

  • 0

I’m making a PHP frontend for a MySQL database. I’ve got a table called “Regions” which contains RegionID (int), Name (string), and Country (string). I’m trying to put a <select> list onto a form containing all the regions as options, using <optgroup>’s to group regions by country. I’ve already done it successfully by nested plain queries, and now I’m trying to get my head around prepared statements.

What I’ve got so far is:

// $DB is database object, already connected
// Error checking will be worked into this once I've worked out the basics...

$stmt_country = $DB->stmt_init();
$stmt_country->prepare("SELECT DISTINCT Country FROM Regions ORDER BY Country ASC");
$stmt_country->execute();
$stmt_country->bind_result($CountryName);

$stmt_region = $DB->stmt_init();
$stmt_region->prepare("SELECT RegionID,Name FROM REGIONS WHERE Country=? ORDER BY Name ASC");
$stmt_region->bind_param("s",$CountryName);

echo "<select name=\"RegionID\">\n";

while ($stmt_country->fetch()) {
    // Start 'country' option group

    echo "<optgroup label=\"$CountryName\">\n";

    // Execute the 'regions' query using the 'country' parameter

    $stmt_region->execute();
    $stmt_region->bind_result($RegionID,$RegionName);

    while ($stmt_region->fetch()) {
        // Output the regions matching that country 

        echo "<option value=\"$RegionID\">$RegionName</option>\n";
        }

    echo "</optgroup>\n";
    }

echo "</select>\n";

However when I run this I’m getting:

Warning: mysqli_stmt::bind_param(): invalid object or resource

on the $stmt_region->bind_param("s",$CountryName) line.

With some commenting out of code, I’ve got just the $stmt_country half of it working (i.e. outputs just the optgroups for each country), and then bodging in a value for $CountryName, the $stmt_region half of it works by itself, but I can’t get both halves working together. Can someone please advise where I’m going wrong?

  • 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-05T05:32:22+00:00Added an answer on June 5, 2026 at 5:32 am

    Im pretty sure you cant have 2 staments bound on the same connection resource until after you have gone through the entire result set. Because of that your second stmt_init() isnt actually returning a statement.

    It looks like all the data is on the same table so there is no need for 2 queries.

    If the data were on two tables then there would still be no need for 2 queries as oyu could use a join:

    What you can do, and should do even if this is not the case is use a join to get everything at once:

    SELECT DISTINCT r.RegionID, r.Country 
    FROM Regions r, Country c
    WHERE c.NAME = r.Country 
    ORDER BY r.Country ASC
    

    In either case you can get a reult set looking like:

    RegionID    Country    Name
    ----------------------------------
    1           US         Ohio
    2           US         California
    3           US         New York
    4           CN         Guan Dong
    5           CN         Hunan
    6           CN         Szechuan
    

    Because of the ordering you can use a single loop comparing the previous Country to the current to determine when to close/open your opt group tags.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words

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.