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";
}
@chrisdrogaris

This comment has been minimized.

Show comment Hide comment
@chrisdrogaris

chrisdrogaris Mar 30, 2015

Says no tables when there are some.

Says no tables when there are some.

@aeroson

This comment has been minimized.

Show comment Hide comment
@aeroson

aeroson Sep 13, 2015

thank you

aeroson commented Sep 13, 2015

thank you

@joaoBeno

This comment has been minimized.

Show comment Hide comment
@joaoBeno

joaoBeno Sep 29, 2015

Thanks!

Thanks!

@changlee2

This comment has been minimized.

Show comment Hide comment
@changlee2

changlee2 Dec 27, 2015

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

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

@lexblagus

This comment has been minimized.

Show comment Hide comment
@lexblagus

lexblagus Mar 31, 2016

Great! Obrigado!

Great! Obrigado!

@abhinov2

This comment has been minimized.

Show comment Hide comment
@abhinov2

abhinov2 Apr 14, 2016

thanks

thanks

@ron4stoppable

This comment has been minimized.

Show comment Hide comment
@ron4stoppable

ron4stoppable Apr 28, 2016

great for fast implementation

great for fast implementation

@BretMishler

This comment has been minimized.

Show comment Hide comment
@BretMishler

BretMishler May 3, 2016

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.

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.

@stephangriesel

This comment has been minimized.

Show comment Hide comment
@stephangriesel

stephangriesel Jun 10, 2016

thank you! works great.

thank you! works great.

@omalave

This comment has been minimized.

Show comment Hide comment
@omalave

omalave Jun 13, 2016

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

omalave commented Jun 13, 2016

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

@ChimpGamer

This comment has been minimized.

Show comment Hide comment
@ChimpGamer

ChimpGamer Aug 4, 2016

gives me 403 error

gives me 403 error

@M165437

This comment has been minimized.

Show comment Hide comment
@ultrablue

This comment has been minimized.

Show comment Hide comment
@ultrablue

ultrablue Oct 13, 2016

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

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

@abdulsalam1447

This comment has been minimized.

Show comment Hide comment
@abdulsalam1447

abdulsalam1447 Mar 23, 2017

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

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

@abdulsalam1447

This comment has been minimized.

Show comment Hide comment
@abdulsalam1447

abdulsalam1447 Mar 27, 2017

500 error. what missing

500 error. what missing

@Shivani-Band

This comment has been minimized.

Show comment Hide comment
@Shivani-Band

Shivani-Band Apr 7, 2017

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

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

@del-sys32

This comment has been minimized.

Show comment Hide comment
@del-sys32

del-sys32 Apr 29, 2017

@gurwinderbytegigs

This comment has been minimized.

Show comment Hide comment
@gurwinderbytegigs

gurwinderbytegigs Sep 28, 2017

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

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

This comment has been minimized.

Show comment Hide comment
@Nordinz

Nordinz Oct 10, 2017

works perfectly for a quick test. Thanks a lot.

Nordinz commented Oct 10, 2017

works perfectly for a quick test. Thanks a lot.

@FGDATA

This comment has been minimized.

Show comment Hide comment
@FGDATA

FGDATA Feb 17, 2018

Hi @ALL

Very useful script. Thanks a lot.
Using arch-linux and maria-db the mysql do not work, so the code was changed as listed below -- and then executed succesfully.

I hope this helps,
https://gist.github.com/FGDATA/37e8c0d4ca6b15ac7046ad7c28e9ef42
https://gist.github.com/FGDATA/37e8c0d4ca6b15ac7046ad7c28e9ef42/revisions

# Fill our vars and run on cli
# $ php -f db-connect-test.php

$dbname = 'name';
$dbuser = 'user';
$dbpass = 'pass';
$dbhost = 'host';

$connect = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysqli_select_db($connect, $dbname) or die("Could not open the db '$dbname'");

$test_query = "SHOW TABLES FROM $dbname";
$result = mysqli_query($connect, $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";
}

FGDATA commented Feb 17, 2018

Hi @ALL

Very useful script. Thanks a lot.
Using arch-linux and maria-db the mysql do not work, so the code was changed as listed below -- and then executed succesfully.

I hope this helps,
https://gist.github.com/FGDATA/37e8c0d4ca6b15ac7046ad7c28e9ef42
https://gist.github.com/FGDATA/37e8c0d4ca6b15ac7046ad7c28e9ef42/revisions

# Fill our vars and run on cli
# $ php -f db-connect-test.php

$dbname = 'name';
$dbuser = 'user';
$dbpass = 'pass';
$dbhost = 'host';

$connect = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysqli_select_db($connect, $dbname) or die("Could not open the db '$dbname'");

$test_query = "SHOW TABLES FROM $dbname";
$result = mysqli_query($connect, $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";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment