Connect To MySQL in Php?

How Can You Connect To Your MySql Database In Php? Thats Easy as Turning Water into Wine

Before Connecting Your Database To Your Application Makes Sure Its Working/Accessible By Outside Environment Such as Mysql Workbench Download From Here

After Successfully Testing Your Database You Are Ready To Connect To Your Web Application

Lets Start With Php Code To Connect To Mysql

<?php  
//You Know What this is Right? its Php Starting Tag and this // are syntax

$host = "freedb.tech";
$username = "freedbtech_youareawesome";
$password = "dontgiveup";
$databasename = "freedbtech_icandoanything";

/* 
//these are the /* php multiline comments syntax */ 
well these are the credentials to connect to your database you can get these
from here and in above codewe are just assigning a variable to our details thats all don't get too confused and yes assigning variable is up to you its just makes it better to understand
*/

$connection = new mysqli($host, $username, $password, $databasename);
//We Are Just Starting our Mysql Connection With Our Database Details


if ($connection->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
echo "Connection Successful";

/*
well now we are checking if our connection failed if it failed it will display the error and break the code otherwise if it gets succeed without any error  its gonna print "Connection Successful"
So folks Thats all you need to connect to your Mysql Database what now ?
you can start accessing your database in your php code 
How can i do that ?
there are lots of tutorials on internet you can find we just wanted to let you know at freedb we are trying to cover everything related to mysql soon.
*/
?>

Lets just cleanup the unusual comments and present you php code:

<?php  
$host = "freedb.tech";
$username = "freedbtech_youareawesome";
$password = "dontgiveup";
$databasename = "freedbtech_icandoanything";


$connection = new mysqli($host, $username, $password, $databasename);


if ($connection->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

echo "Connection Successful";
?>

Leave a Reply

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