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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:22:25+00:00 2026-06-08T15:22:25+00:00

I am familiar with PHP and MYSQLi connections and have read the PHP documentation

  • 0

I am familiar with PHP and MYSQLi connections and have read the PHP documentation including http://www.php.net/manual/en/mysqli.persistconns.php , but am wondering about the proper way (performance wise) to establish and close connections in MYSQLi using PHP.

What is the most efficient way of starting and closing mysqli connections?

What is the proper way to handle if the mysqli connection is lost during a query?

Here is my current setup:

  1. Opening a connection at the beginning of the php page
  2. Making all the necessary queries through other classes
  3. Closing the connection at the end of page

/*
public exampleQuery ($conn, $p1, $p2) {
  $stmt = $conn->prepare("SELECT id FROM tbl_name WHERE c1=? AND c2=?");
  $stmt->bind_param("ss", $p1, $p2);
  $stmt->execute();
  // etc..
}
*/


$mysqli_connection = $connectionClass->connectToMYSQLi();
//Execute queries using prepared statements
$queryClass->exampleQuery($mysqli_connection,$param1, $param2);
$queryClass->exampleQueryTwo($mysqli_connection,$param1, $param2);
//etc...
$connectionClass_>closeMYSQLiConnection($mysqli_connection);
  • 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-08T15:22:26+00:00Added an answer on June 8, 2026 at 3:22 pm

    How does PHP handle the connections once the page is reloaded or accessed by a new user simultaneously?

    PHP will also make sure the some variable you use in your script isn’t used by a different user who happens to access the same page.
    The same goes for the database connection.

    Accessing the database using some credentials (username/password) doesn’t mean it can ‘collide’ with somebody else using the same credentials.
    The database is perfectly fine with running multiple connections simultaneously, even if they are all using the same credentials.

    But this shouldn’t be your problem/concern (as end-user of the functionality, being the developer), but it boils down to PHP forking processes or using a threadsafe module to serve each request. Then each process associated with each request starts a new session (or recycles one) to the database.

    prepared statements:

    Prepared statements are good. They offer the database an opportunity to use an executionplan that is stored on the database. When you use complex queries, this can give some speed advantages.
    They also protect you against SQL-injection.

    So keep using the prepared statements.

    How you structure them inside your code is up to you. The database doesn’t care at all if you call the statements from some class or not.
    If you use the same prepared statements from a Java application, they would still be prepared statements (if the database supports them, that is.). The database would still have a executionplan ready for them.

    So do it the way you like best (=most clear/concise to you).

    There is a lot more to say about the subject, and I explained some concepts only partially correct, but I hope this brushes the big picture.

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

Sidebar

Related Questions

I've done plenty of ASP.NET and PHP development, but I'm less familiar with how
I'm not that familiar with PHP so far but already succeeded in registring a
I am somewhat familiar with how to set up seamless authentication in PHP/ASP.net applications
I am a PHP developer but now I am learning asp.net with C# as
I am familiar with using PHP to perform mySQL queries. However, I have been
I'm familiar with MySQL but not with Firebird. I want to change my PHP
Disclaimer: I'm familiar with PHP, MySQL, jQuery, AJAX, but am by no means an
I'm familiar with php's function bin2hex() for converting binary data to its hexadecimal representation.
we are all familiar with embedding PHP scripts in HTML pages to do tasks
I am coming from a PHP background and I am familiar with OOP concepts,

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.