- PHP mysqli connect() Function
- Syntax
- Object oriented style:
- Procedural style:
- Parameter Values
- Technical Details
- Example — Procedural style
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- mysql_ping
- Description
- Parameters
- Return Values
- Examples
- See Also
- User Contributed Notes 7 notes
- Simple PHP script to check the MySQL database connection from web-browser
- PHP Code:
- Example:
- Post navigation
- Ann
- 4 thoughts on “ Simple PHP script to check the MySQL database connection from web-browser ”
PHP mysqli connect() Function
The connect() / mysqli_connect() function opens a new connection to the MySQL server.
Syntax
Object oriented style:
Procedural style:
Parameter Values
Parameter | Description |
---|---|
host | Optional. Specifies a host name or an IP address |
username | Optional. Specifies the MySQL username |
password | Optional. Specifies the MySQL password |
dbname | Optional. Specifies the default database to be used |
port | Optional. Specifies the port number to attempt to connect to the MySQL server |
socket | Optional. Specifies the socket or named pipe to be used |
Technical Details
Example — Procedural style
Open a new connection to the MySQL server:
// Check connection
if (mysqli_connect_errno()) echo «Failed to connect to MySQL: » . mysqli_connect_error();
exit();
>
?>
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
mysql_ping
This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide. Alternatives to this function include:
Description
Checks whether or not the connection to the server is working. If it has gone down, an automatic reconnection is attempted. This function can be used by scripts that remain idle for a long while, to check whether or not the server has closed the connection and reconnect if necessary.
Note:
Automatic reconnection is disabled by default in versions of MySQL >= 5.0.3.
Parameters
The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() had been called with no arguments. If no connection is found or established, an E_WARNING level error is generated.
Return Values
Returns true if the connection to the server MySQL server is working, otherwise false .
Examples
Example #1 A mysql_ping() example
$conn = mysql_connect ( ‘localhost’ , ‘mysqluser’ , ‘mypass’ );
$db = mysql_select_db ( ‘mydb’ );
/* Assuming this query will take a long time */
$result = mysql_query ( $sql );
if (! $result ) echo ‘Query #1 failed, exiting.’ ;
exit;
>
/* Make sure the connection is still alive, if not, try to reconnect */
if (! mysql_ping ( $conn )) echo ‘Lost connection, exiting after query #1’ ;
exit;
>
mysql_free_result ( $result );
/* So the connection is still alive, let’s run another query */
$result2 = mysql_query ( $sql2 );
?>
See Also
User Contributed Notes 7 notes
It should be noted that mysql_ping() seems to reset the error message on the server.
I used it to check whether the connection was still alive before reading the error message via mysql_error() and it always returned an empty string. Upon removing the connection check everything worked.
mysql_ping() is really helpful when you have this annoying error:
MYSQL Error 2006 Server has gone away
For CI users:
In 1.7.2 version of codeigniter, there is a function
that uses mysql_ping() to reestablish the timed out connection.
This function is specially useful when developing social media sites that uses hundreds of connections to the db such asinserting or selecting.
When using the mysql_ping command under php 5.1.2 and mysql 5.0, I was having problems with the auto-reconnect «feature», mainly that when the connection was severed, a mysql_ping would not automatically re-establish the connection to the database.
The connection to the DB is dropped when the time without a query excedes the wait_timeout value in my.cnf. You can check your wait_timeout by running the query «SHOW VARIABLES;»
If you’re having problems auto-reconnecting when the connection is dropped, use this code:
$conn = mysql_connect ( ‘localhost’ , ‘user’ , ‘pass’ );
mysql_select_db ( ‘db’ , $conn );
if (! mysql_ping ( $conn )) //here is the major trick, you have to close the connection (even though its not currently working) for it to recreate properly.
mysql_close ( $conn );
$conn = mysql_connect ( ‘localhost’ , ‘user’ , ‘pass’ );
mysql_select_db ( ‘db’ , $conn );
>
//run queries knowing that your connection is alive.
When checking if a $resource works.
be prepared that mysql_ping returns NULL as long as $resource is no correct mysql resource.
$resource = NULL ;
var_dump = @ mysql_ping ( $resource );
# showing NULL
?>
This could be used to decide of a current $resource is a mysql or a mysqli connection when nothing else is available to do that.
This function *does not* attempt to reconnect at this time. It only tells you whether or not you currently *are* connected.
To actually reconnect, you will have to implement this yourself in a wrapper class.
Is important to remember that if your first connection to mysql don’t works, mysql_ping will always return true! So, if you want to check if mysql is connected, first of all you must check if mysql_connect do not returns false and then you can begin to check mysql_ping.
If you get ‘error 2006: MySQL server has gone away’ messages when running (really) long scripts, mysql_ping will help detecting the loss of the db-connection. This can happen, when ‘wait timeout’ is reached (MySQL default is 8 hours).
- MySQL Functions
- mysql_affected_rows
- mysql_client_encoding
- mysql_close
- mysql_connect
- mysql_create_db
- mysql_data_seek
- mysql_db_name
- mysql_db_query
- mysql_drop_db
- mysql_errno
- mysql_error
- mysql_escape_string
- mysql_fetch_array
- mysql_fetch_assoc
- mysql_fetch_field
- mysql_fetch_lengths
- mysql_fetch_object
- mysql_fetch_row
- mysql_field_flags
- mysql_field_len
- mysql_field_name
- mysql_field_seek
- mysql_field_table
- mysql_field_type
- mysql_free_result
- mysql_get_client_info
- mysql_get_host_info
- mysql_get_proto_info
- mysql_get_server_info
- mysql_info
- mysql_insert_id
- mysql_list_dbs
- mysql_list_fields
- mysql_list_processes
- mysql_list_tables
- mysql_num_fields
- mysql_num_rows
- mysql_pconnect
- mysql_ping
- mysql_query
- mysql_real_escape_string
- mysql_result
- mysql_select_db
- mysql_set_charset
- mysql_stat
- mysql_tablename
- mysql_thread_id
- mysql_unbuffered_query
Simple PHP script to check the MySQL database connection from web-browser
Want to verify the DB connectivity from the web browser?
This can be done from by adding a simple PHP script. See how!!
It’s very easy and interesting to check your MySQL database connectivity from your account without login as a root user. Here is a script to do the same.
To check the database connectivity, you must know the database hostname, normally it is “localhost” then database-name, username and password.
If the DB is hosted on a remote server [remote DB server], change the host to that server’s hostname. Also the connectivity from this machine to DB machine should be enabled.
PHP Code:
It’s very simple concept, first the “mysql_connect” argument will check the database hostname, username and password.
If the first argument is true, then PHP take the second line to execute else the script will die with an output given in the Die section. Similarly, mysql_select_db check the database on the server.
If both of the arguments are true then you will get an output given after the echo command.
Example:
1, If all of the input data(db_name, username, password, hostname) are right, then the output is like;
Database Connected successfully
2, If any of the entry in ‘mysql_connect’ secion is wrong, then the output is;
Could not connect the database : Username or password incorrect
3, If the database entry is wrong;
No database found
Post navigation
Ann
I’m Ann, a freelancer in web-designing and also I have interest in blogging. I’m glad to be an editor/author at CryBit.com. Here, I will post some interesting web hosting + design related topics. Post your suggestions as comment.
4 thoughts on “ Simple PHP script to check the MySQL database connection from web-browser ”
Hi, you say adding this simple php text to where?
I try to imagine, it is a document in the web site directory let’s say named test.php having this little script, is that correct?
Can you be more accurate on it?, please.
Thanks.