Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script for a quick PHP MySQL DB connection test.
<?php
# Fill our vars and run on cli
# $ php -f db-connect-test.php
$dbname = 'name';
$dbuser = 'user';
$dbpass = 'pass';
$dbhost = 'host';
$link = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysqli_select_db($link, $dbname) or die("Could not open the db '$dbname'");
$test_query = "SHOW TABLES FROM $dbname";
$result = mysqli_query($link, $test_query);
$tblCnt = 0;
while($tbl = mysqli_fetch_array($result)) {
$tblCnt++;
#echo $tbl[0]."<br />\n";
}
if (!$tblCnt) {
echo "There are no tables<br />\n";
} else {
echo "There are $tblCnt tables<br />\n";
}
@abdulsalam1447

This comment has been minimized.

Copy link

commented Mar 23, 2017

works fine thank you

@abdulsalam1447

This comment has been minimized.

Copy link

commented Mar 26, 2017

Back again.

@fizerkhan

This comment has been minimized.

Copy link

commented May 5, 2017

Thanks. 👍

@thpweb4

This comment has been minimized.

Copy link

commented Aug 10, 2017

Hi! Having an issue with a script (stopped working for some reason after many years of running just fine) and trying to test database and server connection. I uploaded the script and it appears to be working, except, it is saying there are no tables, when in fact there are many tables in the database. Does anyone know what the issue may be or able to point me in the right direction?

@rickp2006

This comment has been minimized.

Copy link

commented Feb 8, 2018

Thank you for the updated version. This is very handy indeed.

@gvhconnect

This comment has been minimized.

Copy link

commented Sep 2, 2018

Having trouble understanding how to run the script.
I first of all moved it as "index.php" into the root apache directory... ...not sure if there's an ownership issue there?
Online the browser failed to respond, even though it's checked out with the basic script.

I then took the command as you'd hashed out in line 3, and as per below tried to run as root in the var/www directory:

root@ip-172-31-33-139:/home/ubuntu# cd /var/www
root@ip-172-31-33-139:/var/www# ls -l
total 12
-rw-rw-r-- 1 ubuntu ubuntu 678 Sep 2 19:20 db-connect-test.php
drwxrwxr-x 2 www-data www-data 4096 Sep 2 16:35 html
-rw-rw-r-- 1 ubuntu ubuntu 662 Sep 2 19:13 index.php
root@ip-172-31-33-139:/var/www# php -f db-connect-test.php
PHP Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /var/www/db-connect-test.php:9
Stack trace:
#0 {main}
thrown in /var/www/db-connect-test.php on line 9
root@ip-172-31-33-139:/var/www# root@ip-172-31-33-139:/var/www# php -f db-connect-test.php
bash: root@ip-172-31-33-139:/var/www#: No such file or directory
root@ip-172-31-33-139:/var/www# PHP Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /var/www/db-connect-test.php:9
bash: syntax error near unexpected token `('

I have set the variables with the correct RDS host and login credentials, which checkout from this machine using the remote mysql access, which works fine, logging in and returning the schema structure with the show databases; mysql command.

In your second line hashed out statement, you're suggesting running this under CLI. Could you please provide the syntax?

Also, as it's my goal here to understand the general rules / syntax for evaluating php scripts from this micro staging server I"m using, please do comment if what I'm trying to do is possible (ie invoking a php script in the root apache directory as "index.php" to get a browser response linked to the server's url).

Any guidance appreciated.

Thanks,

@Jacuro

This comment has been minimized.

Copy link

commented Sep 13, 2018

Hi there.

Are you running this script in /var/www/ ??
Save it as db-connect-test.php in /var/www/html and try there

Regards J

@mobilewebguru

This comment has been minimized.

Copy link

commented Apr 16, 2019

wonderfull, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.