Create a gist now

Instantly share code, notes, and snippets.

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';
$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysql_select_db($dbname) or die("Could not open the db '$dbname'");
$test_query = "SHOW TABLES FROM $dbname";
$result = mysql_query($test_query);
$tblCnt = 0;
while($tbl = mysql_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";
}

Says no tables when there are some.

aeroson commented Sep 13, 2015

thank you

Thanks!

That really worked perfect and it is fantastic! I tried so many scripts, and that worked excellent. Many thanks.

Great! Obrigado!

thanks

great for fast implementation

I forked this to make some edits to deal with deprecated functions but I'm not sure how to push it back to yours for approval if you so choose to commit the changes.

thank you! works great.

omalave commented Jun 13, 2016

mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

gives me 403 error

Nice. Simple, useful, direct and to the point. Thanks!

Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /var/www/html/car/db-connect-test.php:10 Stack trace: #0 {main} thrown in /var/www/html/car/db-connect-test.php on line 10

500 error. what missing

i am getting uncaught error for call to undefined function mysql_connect
please provide me the solution fast

Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\xampp\htdocs\sample\mycoonect.php:8 Stack trace: #0 {main} thrown in C:\xampp\htdocs\sample\mycoonect.php on line 8

showwing this error i have tried so many times .. with your code and with my own code but showing the same error

Nordinz commented Oct 10, 2017

works perfectly for a quick test. Thanks a lot.

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