- MCF / sqlsrv_test.php
- How to Test PHP MySQL Database Connection Using Script
- Setup LAMP Stack on Linux Systems
- Setup LEMP Stack on Linux Systems
- Quick MySQL Database Connection Test Using PHP Script
- Тестовое подключение к MySQL из PHP mysqli
- Testing SQLServer and PHP locally in 2021
- Docker to the rescue
- Getting SQLS erver
MCF / sqlsrv_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
$ serverName = » sqlserver.example.com «; |
$ database = » myDbName «; |
$ uid = ‘sqlserver_username’ ; |
$ pwd = ‘password’ ; |
try |
$ conn = new PDO ( |
» sqlsrv:server= $ serverName ;Database= $ database «, |
$ uid , |
$ pwd , |
array ( |
//PDO::ATTR_PERSISTENT => true, |
PDO :: ATTR_ERRMODE => PDO :: ERRMODE_EXCEPTION |
) |
); |
> |
catch ( PDOException $ e ) |
die(» Error connecting to SQL Server: » . $ e -> getMessage ()); |
> |
echo » Connected to SQL Server \n»; |
echo » PDO::ATTR_PERSISTENT value: \n»; |
echo » "; |
echo var_export( $ conn -> getAttribute ( PDO :: ATTR_PERSISTENT ), true ); |
echo " «; |
echo » PDO::ATTR_DRIVER_NAME value: \n»; |
echo » "; |
echo var_export( $ conn -> getAttribute ( PDO :: ATTR_DRIVER_NAME ), true ); |
echo " «; |
echo » PDO::ATTR_CLIENT_VERSION value: \n»; |
echo » "; |
echo var_export( $ conn -> getAttribute ( PDO :: ATTR_CLIENT_VERSION ), true ); |
echo " «; |
$ query = ‘select top 5 * from tbl_users’ ; |
$ stmt = $ conn -> query ( $ query ); |
echo » "; |
while ( $ row = $ stmt -> fetch ( PDO :: FETCH_ASSOC )) |
print_r( $ row ); |
> |
echo " «; |
// Free statement and connection resources. |
$ stmt = null ; |
$ conn = null ; |
?> |
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 .
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.
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.
Тестовое подключение к MySQL из PHP mysqli
Иногда необходимо протестировать работу базы данных, если не удается настроить подключение через сайт или CMS, для этого можно использовать простой PHP скрипт с расширением mysqli.
Скрипт достаточно загрузить в папку вашего сайта и подставить свои значения в файле вместо USER, PASSWORD, BASENAME.
Данный фрагмент кода поможет проверить подключение к базе данных. А также может служить основой для работы с базой данных и выполнения SQL запросов.
$mysqli = new mysqli('localhost', 'USER', 'PASSWORD', 'BASENAME'); if ($mysqli->connect_error) { die('Ошибка подключения (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error); } echo 'Соединение установлено. '
. $mysqli->host_info . ""; $result = $mysqli->query("SHOW TABLES;"); while ($row = $result->fetch_row()) { echo "Таблица: "
; } echo 'Версия MYSQL сервера: ' . $mysqli->server_info . "\n"; $mysqli->close(); ?>
Пригодился скрипт для тестирования хостинга Namecheap.
Testing SQLServer and PHP locally in 2021
I’ve recently been working on overhauling the test database fixture system in CakePHP . My goals are to separate fixture schema management from fixture data management. By doing this applications will more easily be able to use their existing schema management to generate their test database. This project has entailed fixing many differences between the database servers that CakePHP supports. While I can easily install SQL ite, MySQL and Postgres locally via apt SQLS erver is a different animal.
Docker to the rescue
Thankfully we’re in 2021 and Microsoft has invested the time to make SQLS erver functional on Linux and Docker makes running SQLS erver much easier than ever before. The pdo_sqlsrv extension is complicated to install on linux, but thankfully Docker can make that easier too. So how does all this magic work? I wasn’t able to find a single reference guide after searching through google, stackoverflow and countless abandoned GitHub projects. From those sources, I was able to cobble together a solution. That I would like to share so other have a less confusing journey.
Getting SQLS erver
First thing we need to do is get the SQLS erver image. We can use docker pull to get it:
Before we run this container, we need to setup a docker ‘network’. This is required so that our database and PHP containers can talk to each other. While you could do this with docker-compose I didn’t want to use more tools:
Now we can run our container to get SQLS erver running in the background.