Skip to content

Instantly share code, notes, and snippets.

@chales
Last active June 3, 2023 01:01
Show Gist options
  • Star 87 You must be signed in to star a gist
  • Fork 67 You must be signed in to fork a gist
  • Save chales/11359952 to your computer and use it in GitHub Desktop.
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";
}
@davegog
Copy link

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

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

thanks!

@HATIMEDIA
Copy link

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

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

Thank you!

@rauhmaru
Copy link

Thanks bro!

@franciscoeymardneto
Copy link

Thanks man!

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