TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

PHP

Test MySQL Connection in PHP

Introduction

In this post, we will learn how to test MySQL connection in PHP using sample test code. In order to work with the MySQL database, we need to enable the extensions in the PHP configuration file.

MySQL Extensions

Enable MySQL extensions in the php.ini file.

https://www.testingdocs.com/enabling-mysql-extensions-in-php/

PHP INFO

echo phpinfo();

The above statement would display the PHP environment and configuration 
parameters.

Test PHP Code

Add a Sample PHP file under the Apache server htdocs directory.

MySQLDBConTest.php

<?php
/*************************************************************************
* This is to test PHP MySQL Connection
*
* www.TestingDocs.com
*************************************************************************/
$MySQLDb = new mysqli("localhost", "root", "<password>", "world");
if($MySQLDb->connect_errno)
{
 echo $MySQLDb>connect_error;
}
else
{
 echo "MySQL connection succeeded.";
}
?>

 

Screenshot

 

Verify

Once you added the test code file. Restart the Apache server. Browse the URL in the browser. For Example:

http://localhost/MySQLDBConTest.php

The output should be displayed as  “MySQL connection succeeded.”

 

That’s it. We have successfully tested MySQL database connection.

 

—

PHP Tutorials

PHP Tutorials on this website:

https://www.testingdocs.com/php-tutorials/

More Information on PHP

https://www.php.net/

Related Posts

Compoer Command line command

PHP /

Install Slim Framework on Windows

php download website

PHP /

Download and Install PHP on Windows

Composer Install Windows

PHP /

Composer Download on Windows

Apache conf file

PHP /

PHP 7 Configuration in Apache Server

testing php linux

PHP /

Testing PHP with Apache Server on Linux

‹ XAMPP Control Panel› Popular Open Source PHP Frameworks

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version