Skip to content

Instantly share code, notes, and snippets.

@chales
Last active March 29, 2025 09:10
Show Gist options
  • Select an option

  • Save chales/11359952 to your computer and use it in GitHub Desktop.

Select an option

Save chales/11359952 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';
$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
Copy link
Copy Markdown

Says no tables when there are some.

@JakubNei
Copy link
Copy Markdown

thank you

@joaoBeno
Copy link
Copy Markdown

Thanks!

@changlee2
Copy link
Copy Markdown

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

@lexblagus
Copy link
Copy Markdown

Great! Obrigado!

@abhinov2
Copy link
Copy Markdown

thanks

@ron4stoppable
Copy link
Copy Markdown

great for fast implementation

@BretMishler
Copy link
Copy Markdown

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
Copy link
Copy Markdown

thank you! works great.

@omalave
Copy link
Copy Markdown

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
Copy link
Copy Markdown

gives me 403 error

@M165437
Copy link
Copy Markdown

M165437 commented Sep 2, 2016

@ultrablue
Copy link
Copy Markdown

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

@abdulsalam1447
Copy link
Copy Markdown

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
Copy link
Copy Markdown

500 error. what missing

@Shivani-Band
Copy link
Copy Markdown

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

@MikkelsWorld
Copy link
Copy Markdown

@BestaTechnology
Copy link
Copy Markdown

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

Copy link
Copy Markdown

ghost commented Oct 10, 2017

works perfectly for a quick test. Thanks a lot.

@FGDATA
Copy link
Copy Markdown

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

@Jogai
Copy link
Copy Markdown

Jogai commented Jul 3, 2018

@hargi12
Copy link
Copy Markdown

hargi12 commented Aug 6, 2018

Well detailed script. Thanks

@davegog
Copy link
Copy Markdown

davegog commented Oct 9, 2018

Hi Chris,

Many thanks for your script, I've updated it with an html form section, if anyone needs to test the same via a browser. The name of the *.php files corresponds to the name depicted in the form section (form action="index.php”)' currently, this can be changed to match the name of the php file. I've tested it with RDS MySQL.

<?php

if(isset($_POST['dbname']) && isset($_POST['dbuser']) && isset($_POST['dbpass']) && isset($_POST['dbhost']))

{

        $dbname = $_POST['dbname'];
        $dbuser = $_POST['dbuser'];
        $dbpass = $_POST['dbpass'];
        $dbhost = $_POST['dbhost'];


        $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";
                }

                }
?>


<form action="index.php" method="post">
        Enter Database_name: <input type="text" name="dbname" />
        Enter Database_username: <input type="text" name="dbuser" />
        Enter Database_password: <input type="password" name="dbpass" />
        Enter Database_host: <input type="text" name="dbhost" />
<input type="submit" value="Submit" />
</form>


@waseemmir1
Copy link
Copy Markdown

what is the error I am getting when I do php.script.php

Fatal error: Uncaught Error: Call to undefined function mysql_connect()

@jonasrafael
Copy link
Copy Markdown

thanks!

@HATIMEDIA
Copy link
Copy Markdown

PHP Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\inetpub\wwwroot\db-connect-test.php:10
Stack trace:
#0 {main}
thrown in C:\inetpub\wwwroot\db-connect-test.php on line 10

@cambmon
Copy link
Copy Markdown

cambmon commented Jan 3, 2020

Using on MariaDB Implemented on RaspberryPi
I found this code very helpful i’m new to php and this code was just the job.
Many thanks
Phil

@dinushkarukshan
Copy link
Copy Markdown

Thank you!

@rauhmaru
Copy link
Copy Markdown

Thanks bro!

@franciscoeymardneto
Copy link
Copy Markdown

Thanks man!

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