Skip to content

Instantly share code, notes, and snippets.

@ridvanaltun
Created August 14, 2021 20:34
Show Gist options
  • Save ridvanaltun/dbbbcb25adf576209c5510e54e687afb to your computer and use it in GitHub Desktop.
Save ridvanaltun/dbbbcb25adf576209c5510e54e687afb to your computer and use it in GitHub Desktop.
DB test in PHP

before start

  • open your php.ini file
  • remove semicolon from ;extension=mysqli like extension=mysqli

code

<?php
# 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";
}

run

# run the test
php -f db-connect-test.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment