Basically in pseudo code I'm looking for something like
if (connected_to_any_database()) { // do nothing } else { mysql_connect(...) }
How do I implement
connected_to_any_database()
Firstly, use the mysqli_xxx() functions instead of the old obsolete mysql_xx() functions. With the mysqli library, you get a variable that contains your DB connection, which you can examine at any point. $mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
To test the connection to your database, run the telnet hostname port on your Looker server. For example, if you are running MySQL on the default port and your database name is mydb, the command would be telnet mydb 3306 .
We check the status with the systemctl status mysql command. We use the mysqladmin tool to check if MySQL server is running. The -u option specifies the user which pings the server. The -p option is a password for the user.
Open phpMyAdmin and Click on database which you want to you want to upload to the server. then click on Export option and then choose SQL and press go. It will create the . sql file which contains all the queries which will create exact copy of your database on server.
Have you tried mysql_ping()?
Update: From PHP 5.5 onwards, use mysqli_ping() instead.
Pings a server connection, or tries to reconnect if the connection has gone down.
if ($mysqli->ping()) { printf ("Our connection is ok!\n"); } else { printf ("Error: %s\n", $mysqli->error); }
Alternatively, a second (less reliable) approach would be:
$link = mysql_connect('localhost','username','password'); //(...) if($link == false){ //try to reconnect }
Try using PHP's mysql_ping function:
echo @mysql_ping() ? 'true' : 'false';
You will need to prepend the "@" to suppose the MySQL Warnings you'll get for running this function without being connected to a database.
There are other ways as well, but it depends on the code that you're using.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With