This article mainly introduces the method of php PDO to determine whether the connection is available. Interested friends can refer to it. I hope it will be helpful to everyone.
mysql_ping() Checks whether the connection to the server is normal. Returns true if a connection to the server is available, false otherwise.
But PDO does not support the mysql_ping() method, so you need to write your own code to simulate the mysql_ping() method to check whether the connection is available.
The code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Code demonstration:
1. Create a test data table
1 2 3 4 5 |
|
2. Insert test data
1 2 3 4 5 6 7 8 9 10 |
|
3. Demo file
db.php
##
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
test.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
|
4. Execution
Execute test.php in php cli mode, and then immediately execute mysql.server stop and mysql.server start to simulate a flash interruption1 2 3 4 5 6 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
Use Composer to automatically load the php namespace
PHP 3 ways to connect to the database
Complete list of redis commands under php
The above is the detailed content of PDO implements a method to determine whether the connection is available. For more information, please follow other related articles on the PHP Chinese website!