PHP/MYSQL - Multiple connections to the same database? - php

I have two files:
config.php:
$con = mysqli_connect("$dbhost","$dbuser","$dbpass","$dbname");
ads.php (config.php require_once):
$query1 = mysqli_query($con,"SELECT * From XXXX where id = 'XXXX'");
$query2 = mysqli_query($con,"SELECT * FROM XXXX2 WHERE id = 'XXXX2'");
I have more than 40 different queries mysqli_query() using $con. Please keep in mind that all my logic is procedural style, not object oriented.
My two questions are:
am I connecting to the DB using a different connection on every query? Or just once when the first one is executed? Is there a better way to do this?
I understand that it is not mandatory to close a mysql connection since it closes itself, however since we have millions of consults sometimes a few can hang and stay for more than 10 seconds after the user left the PHP file. If I wanted to close a connection, should I put mysqli_close($con) at the end of the file (after the 40 consults) or at the end of each consult (40 times)?
Any help would be appreciated. Thanks

You are reusing the connection initiated in mysqli_connect.
A better way could be to use or create a DataHandler and implement PDOs. It could allow you to perform timing analytics on queries, automate binding parameters, destroy the connection once its finished with etc.
Call mysqli_close($con) at the end of the file. Also null $con to mark for garbage collection.
Once finished with the information from each step you could also call;
mysqli_free_result($query1) to free memory associated to the statement handle

Related

mysqli: connection and queries and efficiency

I'm a fairly new php programmer. I've been learning for several weeks and over those weeks i've tinkered from hello world scripts to taking other scripts online and installing them and modifying them. Now i'm writing my own and in the course of writing it, I had a bit of an epiphany.. because the scripts ive been modifying and toying with were done a certain way so i started that way as well. But now im thinking it could be inefficient coding or my idea is just wrong for some reason however..
my script is say index.php it's an interactive site and has users. i have 4 different databases that store some form of data. the first database is focused on users and their data. the second on something else, and so on.
The way I have been writing it is that anytime i need to do a query to the database, at the beginning of the script i have my database connection variables all defined such as
$servername = "127.0.0.1";
$dbusername = "dbusername";
$dbpassword = "passwordhere";
$dbname = "database1";
$roomdb = "database2";
etc
etc
so at all sorts of different points in my script there are needs to run database queries so i've been opening a connection like so
$link = mysqli_connect("$servername", "$dbusername", "$dbpassword", "$roomdb");
then running the query
then closing the database connection.
But then i was having a shower and thought suddenly.. why can't i just at the top of my script where i define the database connection info, just open ONE connection to each of the databases. then run the entire script, and just use queries where i need to use them.. and just name the queries differently if i need to have multiple queries to one database. like $query1 , $query2 etc where a query would be like
$query = "INSERT INTO `$room` (room, message, color) VALUES ('$room', '$randomvariable', '$randomvariable')";
does this make sense? it just seems to me that if i almost assuredly have to make multiple connections to each of these databases each time the script is run, and even if theres a chance i dont need one of the connections once in a while, that it would be more efficient to just connect it once at the beginning, have it available then use unique query names for each different query, and then close the connections at the end of the script?
any insights as to why i might be wrong or why it might be better to open the full connection right where you need it then close it again would be appreciated. i want to know how it functions best so i can make the best coding practice as possible....
but lets pretend i even have one database. the same logic applies. is it more efficient to just open one db connection at the top of my script, keep it open the whole run of the script and just use different $querynames to execute queries to the same db rather than open it, run query, close it, and do that 10 different times for all the queries. it seems pointless to me to open and close the connection so many times and adding overhead. did the examples i learn from just code bad or is there a reason for that
We need to save each connection handler in different variable
$link1 = mysqli_connect("$servername", "$dbusername", "$dbpassword", "$roomdb");
$link2 = mysqli_connect("$servername", "$dbusername", "$dbpassword", "$dbname");
Then use the $link_identifier separately for each, so that we know what are connecting to
mysqli_query($link1,"INSERT INTO `$room` (room, message, color) VALUES ('$room', '$randomvariable', '$randomvariable'");
mysqli_close($link1);
Note : Opening a connection for too long is not good coding standard. We would only connect when we are needed to query anything in that database, and close the connection immediately after the transaction is over.

mysqli multi_query followed by query [duplicate]

This question already has answers here:
"Commands out of sync; you can't run this command now" - Caused by mysqli::multi_query
(3 answers)
Closed 7 months ago.
I am currently doing the following:
$mysqli = new mysqli($server, $username, $password, $database);
$mysqli->multi_query($multiUpdates);
while ($mysqli->next_result()) {;} // Flushing results of multi_queries
$mysqli->query($sqlInserts);
Is there a faster way to dump the results?
I do not need them and just want to run the next query however I get the error:
Commands out of sync; you can't run this command now
Problem is the while ($mysqli->next_result()) {;} takes about 2 seconds which is a waste for something I don't want.
Any better solutions out there?
Found a faster solution which saves about 2-3 seconds when updating 500 records and inserting 500 records.
function newSQL() {
global $server, $username, $password, $database;
$con = new mysqli($server, $username, $password, $database);
return $con;
}
$mysqli = newSQL();
$mysqli->multi_query($multiUpdates);
$mysqli->close();
$mysqli = newSQL();
$mysqli->query($sqlInserts);
$mysqli->close();
Not sure how practical it is but works well for speed.
If closing and reopening the connection works for you, then you might be better off changing the order:
$mysqli = newSQL();
$mysqli->query($sqlInserts);
$mysqli->multi_query($multiUpdates);
$mysqli->close();
If you don't care which runs first, the query runs more predictably. As soon as it finishes, MySQL will return the results of the insert statement to the PHP client (probably mysqlnd). The connection will then be clear and can accept the next request. No need to close and reopen after a query. So you save the time it takes to close and reopen the connection with this order.
The multi_query is more complicated. It returns the results of the first update before the PHP code continues. At this point, we don't know if the later updates have run or not. The database won't accept any more queries on this connection until it has finished with the multi_query, including passing the results back with next_result. So what happens when you close the query?
One possibility is that it blocks until the multi_query is finished but does not require the results. So closing the connection essentially skips the part where the database server returns the results but still has to wait for them to be generated. Another possibility is that the connection closes immediately and the database continues with the query (this is definitely what happens if the connection is simply dropped without formally closing it, as the database server won't know that the connection is broken until it finishes or times out, see here or here).
You'll sometimes see the claim that query and multi_query take the same time. This is not true. Under some circumstances, multi_query can be slower. Note that with a normal query (using the default MYSQLI_STORE_RESULT), the database can simply return the result as soon as it finishes. But with multi_query (or with MYSQLI_USE_RESULT), it has to retain the result on the database server. If the database server stores the result, it may have to page it out of memory or it may deliberately store the result on disk. Either way, it frees up the memory but puts the result in a state where it takes more time to access (because disk is slower than memory).
NOTE for other readers: multi_query is harder to use safely than query. If you don't really know what you are doing, you are probably better off using PDO than mysqli (because PDO does more of the work for you) and you are almost certainly better off doing your queries one at a time. You should only use multi_query if you understand why it increases the risk of SQL injections and are avoiding it. Further, one usually doesn't need it.
The only real advantage to multi_query is it allows you to do your queries in one block. If you already have queries in a block (e.g. from a database backup), this might make sense. But it generally doesn't make sense to aggregate separate queries into a block so as to use multi_query. It might make more sense to use INSERT ON DUPLICATE KEY UPDATE to update multiple rows in one statement. Of course, that trick won't work unless your updates have a unique key. But if you do, you might be able to combine both the inserts and the updates into a single statement that you can run via query.
If you really need more speed, consider using something other than PHP. PHP produces HTML in response to web requests. But if you don't need HTML/web requests and just want to manipulate a database, any shell language will likely be more performant. And certainly multithreaded languages with connection pools will give you more options.

PHP & PDO: One Connection vs More-than-one Connections

In my PHP program I need to hit the database between 0 and 3 times on any given webpage request. I am using PDO to interact with MySQL. First I create a database connection using something like this:
$dbh = new PDO("mysql:host=$hostname;dbname=animals", $username, $password);
Then I do what I need to do and close the connection like this:
$dbh = null;
I open and close a connection 0-3 times right now, the same number of times I need to interact with MySQL.
My question is, should I be re-using this connection instead? My queries are not one after another, they are scattered throughout my program and I don't really think it would be easy to run them one right after another.
So is it better to create a new database connection and close it for each interaction (query) or to just leave the connection open and reuse it? Does it make any difference?
Thanks!
For a typical website page, you should be reusing the same connection for all queries.
It's not worth it to spend time disconnecting and reconnecting.
Unless your pages take a huge amount of time to run (huge being relative), then there's no point in relinquishing a connection. You'll end up wasting more cycles on connectiong/disconnecting than you do actually executing queries. MySQL's pretty lightweight as far as connections go, but it still adds up.
The site says: "You must have 50 reputations to comment". My reputation is pretty bad so I can't leave a comment, but I want to add a notice on:
For a typical website page, you should be reusing the same connection
for all queries.
Absolutely right, but in case of need atypical script (e.g. compare tables between a remote database and local database) one can't use the same connection. Must close first connection and establish new one or have 2 separate connections (using different names) at the same time.
I prefer the second option in this case because of closing connection using "$dbh = null;" it can be a bit difficult in some cases. (PDO closing connection)
Here is explanation [from documentation: https://www.php.net/manual/en/pdo.connections.php ] why should be careful with "$dbh = null;" (it should be closed all instances too):
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
// use the connection here
$sth = $dbh->query('SELECT * FROM foo');
// and now we're done; close it
$sth = null;
$dbh = null;
?>
So, only $dbh = null; (without nullifying instances) is not a good idea.

The best efficient method to connect MySQL from PHP?

It is very common that using PHP to connect MySQL. The most common method is like this:
$sqlcon=mysql_connect("localhost","user","pw");
mysql_select_db('database');
$sqlcomm=mysql_query("SELECT id FROM bd");
while($row=mysql_fetch_row($sqlcomm))
{
//do something
}
mysql_close($sqlcon);
I think this is the fastest direct way to connect MySQL. But in project, there will have too many MySQL connections in php script, we should use "mysql_connect("localhost","user","pw")" code to connect MySQL in every php script. So you will like to build a MySQL class or function in a file to connect MySQL:
function connect( $query )
{
$sqlcon=mysql_connect("localhost","user","pw");
mysql_select_db('database');
$sqlcomm=mysql_query($query);
while($row=mysql_fetch_row($sqlcomm))
{
//do something.
}
mysql_close($sqlcon);
}
and then include into your project using include() for connection.
include('connect.php');
$data = connect('SELECT id from db');
OK, in this way, the code is look better. But using include() function will let PHP to read and execute other php script files, a I/O operation on harddisk again, it will also slow down the performance.
If the webpage is 100PV/s, php will read and execute a one php script 100 times/s in first method, but read and execute php script 200 times/s in this method!
I here show a simple example for only one query. Try image a high network multi-query environment.
Dose any one have other better way to make MySQL connection more easier and more efficient?
You don't really need to open that many connections. You just open 1 connection at the start of your script (before <body> gets generated, let's say), and then close it at the end of your script (after </body> is generated, let's say). That leaves you with only 1 connection. In between, you can execute as many queries as you need.
Have you looked at using PDO? it does connection pooling and what not andnot limited to mysql...
Have a look at Dibi.
You use a class that opens a MySQL connection (username / password / db is inherited from some sort of configuration file) and when you query the db - it establishes a connection.
That leads you on to using a framework that uses certain programing paradigms and so forth.
Also, you shouldn't worry about performance decrease because you're including a file. That should be the least of your worries. OS is doing many IOs, not just with the hard disk, your 1 file include won't be noticeable.
If you're asking whether there's more efficient way of connecting to a MySQL db without using mysql_, mysqli_, odbc or PDO - no, there isn't.
performance lack would be insignificant. you must be concerned more about correct approach to the structure of your code than performance.
you can move your host/user/password into constants into separate files and include wherever you need them, more over you can use some design patterns for database object. like Singleton or Factory. they will provide more flexibility to your system.
But in project, there are too many MySQL connections, we should type Username and Password code each time
There are lots of things wrong with this statement - even if you don't count the grammar.
If you mean that you have multiple servers with different datasets on them, then you should definitely consider consolidating them or using the federated engine to provide a single point of access.
Opening a new connection and closing it each time you run a query is very inneficient if you need to execute more than one query per script.
Realy you need to spend a lot of time thinking about why you need multiple database connections and eliminate them, but in the meantime, bearing in mind that connections are closed automatically when a script finishes.....
class connection_pool {
var $conxns=array(
'db1.example.com'=>
array ('host'=>'db1.example.com', 'user'=>'fred', 'password'=>'secret'),
'db2.example.com'=>
array ('host'=>'db1.example.com', 'user'=>'admin', 'password'=>'xxx4'),
....
);
function get_handle($db)
{
if (!array_key_exists($db, $this->conxns)) {
return false;
}
if (!#is_resource($this->conxns[$db]['handle'])) {
$this->conxns[$db]['handle']=mysql_connect(
$this->conxns[$db]['host'],
$this->conxns[$db]['user'],
$this->conxns[$db]['password']
);
}
return $this->conxns[$db]['handle'];
}
}
(NB never use 'USE database' if you have multiple databases on a single mysql instance - always explicitly state the database name in queries)

proper way to solve mysql max user connection error

I'm using PHP with MYSQL database as both are open source and easy to use.
I'm getting problem when I execute insert and/or update of millions of row one after another
while this operation perform I got the MYSQL error that:
'max_user_connections' active connections
which is the best way to solve this problem.
I don't want to use another database or language other then PHP.
connect_db();
$query = "insert into table(mobno,status,description,date,send_deltime,sms_id,msg,send_type) values('".$to."','".$status."','".$report."','','".$timewsha1."','".$smsID."','','".$type."')";
$result = mysql_query($query) or ("Query failed : " . mysql_error());
this query will execute thousand of times.
and then server give connection error.
First of all, try to know from your hosting server administrator about the max consecutive active connections available to the MySQL database. This is the most basic & primary information to have knowledge about.
If your page(s) load in a decent amount of time and release the connection once the page is loaded, it should be fine. The problem occurs when your script takes some long time to retrieve information from the database or maintains the connections.
Since you are executing INSERT and / or UPDATE operations of millions of rows, so you may have some problem.
Additionally, if you fail to close connections in your script(s), it is possible that someone will load a page and instead of closing the connection when the page is loaded, it is left open. No one else can then use that connection. So please make sure that at the end of execution of all the MySQL / SQL queries, the database connection is closed. Also please make sure that your server provides more than 250 connections, since 100 connections is available in almost all the servers generally.
Also make sure that you are not using the persistent connections (which is available when using the built-in function "mysql_pconnect()"), since this will lock up the user until the connection is manually closed.
Hope it helps.
//this loop is for preparing the subquery for mutiple records
for(// this loop for getting data for mutiple records){
$sub_query[] = "('".$to."','".$status."','".$report."','','".$timewsha1."','".$smsID."','','".$type."')";
}
$query = "insert into table(mobno,status,description,date,send_deltime,sms_id,msg,send_type) values ";
$query .= implode(',',$sub_query);
mysql_query($query );
So, a remote app calls into this script, sends it a list of values, and then this query is executed once, right? It's not in a foreach or for or while loop? When you say it will be executed millions of times, you don't mean in one sitting I mean. If it is in a loop, then move the db connect outside of the loop, otherwise it will attempt to connect again each time the loop iterates, and also, remember to call mysql_close at the end of the script, just in case.
mysql_pconnect() would create a persistent connection and that is the way to go if you don't want to exhaust your server's connection pool.

Categories