Php mysql test page

mattstein / db-test.php

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

/**
* Database Connection Tester
* A quick-and-dirty standalone script for checking PHP’s connection to a
* MySQL (or MariaDB) or PostgreSQL database.
*
* To use, enter the settings below and run the following from your terminal:
* «`
* php -f db-test.php
* «`
*/
$ driver = ‘mysql’ ; // ‘mysql’ or ‘pgsql’
$ database = » ;
$ username = » ;
$ password = » ;
$ host = » ;
$ port = $ driver === ‘mysql’ ? 3306 : 5432 ;
/**
* Ignore the stuff below this line.
*/
$ numTables = 0 ;
echo » ———————————————— \n»;
echo » Database Connection Test \n»;
echo » PHP «. PHP_VERSION .»\n»;
echo » DB driver: $ driver \n»;
echo » ———————————————— \n»;
if ( $ driver === ‘mysql’ )
$ connection = mysqli_connect( $ host , $ username , $ password , null , $ port )
or die(» 🚫 Unable to Connect to ‘ $ host ‘. \n\n»);
mysqli_select_db( $ connection , $ database )
or die(» 🚫 Connected but could not open db ‘ $ database ‘. \n\n»);
$ result = mysqli_query( $ connection , » SHOW TABLES FROM ` $ database ` «);
if ( $ result === false )
die(» 🚫 Couldn’t query ‘ $ database ‘. \n\n»);
>
while ( $ table = mysqli_fetch_array( $ result ))
$ numTables ++;
//echo $table[0].»\n»;
>
> elseif ( $ driver === ‘pgsql’ )
$ connection = pg_connect(» host= $ host dbname= $ database user= $ username password= $ password port= $ port «)
or die(» 🚫 Unable to Connect to ‘ $ host ‘. \n\n»);
$ result = pg_query(
$ connection ,
» SELECT table_schema || ‘.’ || table_name
FROM information_schema.tables
WHERE table_type = ‘BASE TABLE’
AND table_schema NOT IN (‘pg_catalog’, ‘information_schema’);
«
);
if ( $ result === false )
die(» 🚫 Couldn’t query ‘ $ database ‘. \n\n»);
>
while ( $ table = pg_fetch_array( $ result ))
$ numTables ++;
//echo $table[0].»\n»;
>
> else
die(» ⛔ Invalid driver ` $ driver `; must be `mysql` or `pgsql`. \n\n»);
>
if (! $ numTables )
echo » 🤷‍️ Connected but no tables found. \n\n»;
> else
echo » 👍 Connected and found $ numTables tables. \n\n»;
>

Источник

M165437 / db-connect-test.php

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

# Fill our vars and run on cli
# $ php -f db-connect-test.php
$ dbname = ‘name’ ;
$ dbuser = ‘user’ ;
$ dbpass = ‘pass’ ;
$ dbhost = ‘host’ ;
$ 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].»
\n»;
>
if (! $ tblCnt )
echo » There are no tables
\n»;
> else
echo » There are $ tblCnt tables
\n»;
>

Hi! Having an issue with a script (stopped working for some reason after many years of running just fine) and trying to test database and server connection. I uploaded the script and it appears to be working, except, it is saying there are no tables, when in fact there are many tables in the database. Does anyone know what the issue may be or able to point me in the right direction?

Thank you for the updated version. This is very handy indeed.

Having trouble understanding how to run the script.
I first of all moved it as «index.php» into the root apache directory. . not sure if there’s an ownership issue there?
Online the browser failed to respond, even though it’s checked out with the basic script.

I then took the command as you’d hashed out in line 3, and as per below tried to run as root in the var/www directory:

root@ip-172-31-33-139:/home/ubuntu# cd /var/www
root@ip-172-31-33-139:/var/www# ls -l
total 12
-rw-rw-r— 1 ubuntu ubuntu 678 Sep 2 19:20 db-connect-test.php
drwxrwxr-x 2 www-data www-data 4096 Sep 2 16:35 html
-rw-rw-r— 1 ubuntu ubuntu 662 Sep 2 19:13 index.php
root@ip-172-31-33-139:/var/www# php -f db-connect-test.php
PHP Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /var/www/db-connect-test.php:9
Stack trace:
#0
thrown in /var/www/db-connect-test.php on line 9
root@ip-172-31-33-139:/var/www# root@ip-172-31-33-139:/var/www# php -f db-connect-test.php
bash: root@ip-172-31-33-139:/var/www#: No such file or directory
root@ip-172-31-33-139:/var/www# PHP Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /var/www/db-connect-test.php:9
bash: syntax error near unexpected token `(‘

I have set the variables with the correct RDS host and login credentials, which checkout from this machine using the remote mysql access, which works fine, logging in and returning the schema structure with the show databases; mysql command.

In your second line hashed out statement, you’re suggesting running this under CLI. Could you please provide the syntax?

Also, as it’s my goal here to understand the general rules / syntax for evaluating php scripts from this micro staging server I»m using, please do comment if what I’m trying to do is possible (ie invoking a php script in the root apache directory as «index.php» to get a browser response linked to the server’s url).

Источник

How to Test PHP MySQL Database Connection Using Script

MySQL is a popular database management system while PHP is a server-side scripting language suitable for web development; together with Apache or Nginx HTTP servers, are the different components of the LAMP (Linux Apache MySQL/MariaDB PHP) or LEMP (Linux Nginx MySQL/MariaDB PHP) stack receptively.

If you are a web developer then you might have installed these software packages or used them to setup a local web server on your system. In order for your website or web application to store data, it needs a database such as MySQL/MariaDB.

For the web application users to interact with the information stored in the database, there must be a program running on the server to pick requests from client and pass to the server.

In this guide, we will explain how to test a MySQL database connection using a PHP file. Before moving further, make sure you must have LAMP or LEMP installed on the system, if not follow these tutorials to setup.

Setup LAMP Stack on Linux Systems

Setup LEMP Stack on Linux Systems

Quick MySQL Database Connection Test Using PHP Script

To do a quick PHP MySQL DB connection test, we will use a following handy script as file db-connect-test.php .

Script to Test PHP MySQL DB Connection

Now change the database name, database user and user password as well as the host to your local values.

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

Save and close the file. Now run it as follows; it should print the total number of tables in the specified database.

MySQL DB Connection Test

You can cross check manually by connecting to the database server and listing the total number of tables in the particular database.

You may also like to check out these following related articles.

Do you have any other way or script to test a MySQL DB connection? If yes, then use the feedback form below to do that.

Источник

Simple PHP Mysql Connection Test Script Tutorial Guide

As a php programmer, sometimes you need to create scripts with an install for the installation of the script. and you want to test the mysql database connection before you install it. today i was writing a script and i wanted to take the user through the installation process and one of the steps was to test the mysql database connections, if the connections fails, then the user is prompted to enter the database details again, if its successful, then it continues with the next step in the installation process. so you can use this:

OPTION 1 — IF you dont want to create the files yourself, Webune has provided the full script, you can download the .zip file by going to this post: How To Test Mysql Database Connection

OPTION 2 — You can follow the steps below to create the script yourself:

1. first step is to copy and paste the following code into your texteditor. if you are using windows, you can use notepad.

 else < $page_title = 'Test MySQL step '.$step; >############## BEGIN FUNCTIONS ############################## # FUNCTION TO TEST USERNAME AND PASSWORD IN MYSQL HOST function db_connect($server, $username, $password, $link = 'db_link') < global $$link, $db_error; $db_error = false; if (!$server) < $db_error = 'No Server selected.'; return false; >$$link = @mysql_connect($server, $username, $password) or $db_error = mysql_error(); return $$link; > # FUNCTION TO SELECT DATABASE ACCESS function db_select_db($database) < echo mysql_error(); return mysql_select_db($database); ># FUNCTION TO TEST DATABASE ACCESS function db_test_create_db_permission($database) < global $db_error; $db_created = false; $db_error = false; if (!$database) < $db_error = 'No Database selected.'; return false; >if ($db_error) < return false; >else < if ([email protected]_select_db($database)) < $db_error = mysql_error(); return false; >else < return true; >return true; > > function step1 ($error) < echo '

'.$error.'


'; ?>
mysql hostname:
"> (usually "localhost" or enter IP Address of MySQL Server)
mysql username:
">
mysql username password:
">
mysql database name:
">
?> Webune MYSQL TEST -

> else < $error = $db_error; >if ($db_error != false) < $error = "failed"; echo step1($db_error); >else < echo '

Congratulations!

Connected Successful to database Continue >> '; > > else < $error = "ERROR: please provide a hostname"; echo step1($error); >break; default: echo step1('Step 1'); break; > ?>
Script Courtesy of Webune PHP/Mysql Hosting

2. save the file as testmysql.php and upload to your website. NOTE: you must have php and mysql for this sample script to work

3. now open testmysql.php with your browser ( http://www.example.com/testmysql.php ), you will see if it fails or if it is able to connect successful

UPDATE: 12/19/2012 Thanks for your comments. I see that some of you have had trouble with the code. I have tested it again and it works ok on my apache server using XAMPP on my windows 7 laptop. So I created a short video to show you as proof that it works.

Источник

Читайте также:  Выравнивание
Оцените статью