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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:26:17+00:00 2026-06-15T20:26:17+00:00

I am trying to learn how to use prepared statements with MySQLi to insert

  • 0

I am trying to learn how to use prepared statements with MySQLi to insert data.

Even though prepared statements are lauded for their ability to efficiently execute similar statements repeatedly, I can’t seem to find examples of executing multiple statements in a loop using MySQLi. I’m especially confused about the following:

  • whether to call bind_param before my loop or inside my loop
  • whether to assign values to my variables before or after the call to bind_param

Most tutorials on prepared statements use PDO. With PDO, an array of parameter values can be passed to execute, eliminating the need to call bindParam. This is not the case with MySQLi.

The PHP manual mysqli_prepare entry has an example that shows the following order of operations:

  1. assign values to variables
  2. prepare statement
  3. bind variables
  4. execute
  5. close

Judging by the above, I had assumed that I would need to call the bind statement within my loop.

However, the PHP manual mysqli_stmt_execute entry has an example that shows the following order of operations:

  1. prepare statement
  2. bind variables
  3. assign values to variables
  4. execute
  5. assign values to variables
  6. execute
  7. close

Note that one thing this example doesn’t show is where the variables are first declared. I thought passing undeclared variables to bind_param would generate a notice. I finally figured out that I can pass undefined variables because they are passed by reference.

Question:
Is one order of operations preferred over the other? Or does it just depend on what you are doing? Are there any gotchas with one or the other?

Note: The code already uses MySQLi and I don’t want to switch to PDO (switching now is outside the scope of this project).

  • 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-15T20:26:18+00:00Added an answer on June 15, 2026 at 8:26 pm

    Because the parameters are passed by reference, you can assign the values to the variables before or after the call to bind_param. Either way will work.

    If the variables were passed by value, you would need to bind them each time you changed their value. But since they are passed by reference, you only need to bind them once.

    In the case of a single execute, the order of operation doesn’t really matter and may depend on where the values are coming from. In the case of a loop, you should definitely call bind_param before the loop.

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

Sidebar

Related Questions

I'm trying to learn how to use CURL and Json to access data sent
I'm trying to learn to use PDO instead of MySQLi for database access and
Trying to learn something new - specifically trying to choose wether to use MySQLi
I'm trying to learn to use Yelp's Python API for MapReduce, MRJob. Their simple
I am trying learn the use of lambda expressions and hence still struggling to
I am trying to learn and use an SDK for a vendor's product. Unfortunately,
I am trying to learn to use buffer overflow attack in Ubuntu. Unfortunately, I
I am trying to learn to use the wx packages to make GUI programs
I am relatively new to programming, and I am trying to learn to use
I'm trying to learn how to use AJAX in my rails apps so i've

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.