How to connect to MySQL using PHP

How to connect to MySQL using PHP

How to connect to MySQL using PHP, in this blog post i will step through the code  on how to get it done.

Prior to that you need to complete  the items listed Below

  1. Mamp or xampp with phpMyAdmin install into your Computer , check out here
  2.  A Sample Database created Check out Here
  3.  New User Created . Check out Here

If all 3 item Checked , you can proceed the below

Step 1

Start your My SQL and Apache Server

 

mysql and Apache Initialised Success

 

Step 2

Create a New PHP Script to connect MySQL DB

Connecting MySQL using PDO ( PHP DATA OBJECT)


<?php

/* Declare User connecting Credential*/

$hostName ="localhost";
$userName ="jane";
$userPassword ="jane";
$dataBaseName = "mydata";

/* return connection status through $dbConnectionStatus Boolean Variable*/

$dbConnectionStatus = new PDO("mysql:host=$hostName;dbname=$dataBaseName", $userName, $userPassword);

/* Check whether the User Succesfully connect to localhost and mydata Database*/

if ($dbConnectionStatus){

echo "User Succesfully connected to MySQL Localhost and mydata Database<br/> ";

}else{

echo "User failed to connect to MySQL Localhost and mydata Database <br/> ";
}

?>

Connecting MySQL using mysqli


<?php

/* Declare User connecting Credential*/

$hostName ="localhost";
$userName ="jane";
$userPassword ="jane";
$dataBaseName = "mydata";

/* return connection status through $dbConnectionStatus Boolean Variable*/

/* using mysqli*/

$dbConnectionStatus = mysqli_connect($hostName, $userName, $userPassword,$dataBaseName);

/* Check whether the User Succesfully connect to localhost and mydata Database*/

if ($dbConnectionStatus){

echo "User Succesfully connected to MySQL Localhost and mydata Database<br/> ";

}else{

echo "User failed to connect to MySQL Localhost and mydata Database <br/> ";
}

?>
<h2></h2>

Connecting MySQL using mysqli Object

</pre>
<?php

/* Declare User connecting Credential*/

$hostName ="localhost";
$userName ="jane";
$userPassword ="jane";
$dataBaseName = "mydata";

/* return connection status through $dbConnectionStatus Boolean Variable*/

/* using mysqli Object*/

$dbConnectionStatus = new mysqli($hostName, $userName, $userPassword,$dataBaseName);

/* Check whether the User Succesfully connect to localhost and mydata Database*/
/*    -> Access the instance "$dbConnectionStatus" variable property "connect_error " boolean */

if ($dbConnectionStatus->connect_error){

die("Connection failed: " . $dbConnectionStatus->connect_error);

}else{

echo " Database Connected";
}

?>
<pre>

 

 

 

Step 3

Save this File  as “.php” extension  in

C:\xampp\htdocs

your drive location might vary from mine .

regardless of your location save that file into Xampp   htdocs

 

Or you can change the Document root by following the steps below

(1) at your Xampp Control

(2) Click Config -> Apache  htttpd.conf -> open the File using Notepad

(3) Search for  DocumentRoot

(4)  Change the root Location

Change Document root
Change Document root
Change Document root
Change Document root

 

 

Change root Location
Change root Location

 

Step 4 

Test it

(1)Click Admin

(2) Click the  PHP File you have just created  in this example i name it  ” connectDB.php”

(3) If successful you will see this Message print out ” User Succesfully connected to MySQL Localhost and mydata Database ”

test connection
test connection
Click the PHP File you have just created
Click the PHP File you have just created
connection Successful
connection Successful

 

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

9 + six =