Skip to content

Instantly share code, notes, and snippets.

@M165437
Forked from chales/db-connect-test.php
Last active May 26, 2023 05:34
Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save M165437/421cd2d23e53a111541a483971f7368b to your computer and use it in GitHub Desktop.
Save M165437/421cd2d23e53a111541a483971f7368b to your computer and use it in GitHub Desktop.
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";
}
@HATIMEDIA
Copy link

PHP Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: H�te inconnu. in C:\inetpub\wwwroot\db-connect-test.php on line 10
PHP Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: H�te inconnu. in C:\inetpub\wwwroot\db-connect-test.php on line 10

@chatelao
Copy link

chatelao commented Jan 3, 2020

If you send me the working fix as pull-request, I could include it into the repository and publish an updated version on SF:

@dinushkarukshan
Copy link

Thank you!

@ModeratorPS
Copy link

I have tested the script and the connection works well.
But, I got following error: There are no tables

Any idea why?

I have only one table, but this one should be shown.
Bildschirmfoto 2022-04-11 um 20 57 18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment