Monday 5 May 2014

Connecting to a server and Database By Php

php mysql
I will Demonstrate how we can connect with mysql database. Read Articles Creating Database which we had named a_database. We also seen Privileges. I had explain we can get our username and everything. Home -> Privileges Screenshot1. We can add new user for but now i am going to use root on localhost with no password. So i will show you how we can connect to our database and then select database which we had named a_database.

phpmyadmin Privileges
Screenshot1

We will connect to a server and database from php Script. 

Code for Connection to a server and database by php


<?php

$mysql_host = 'localhost';

$mysql_user = 'root';

$mysql_pass = '';



mysql_connect($mysql_host, $mysql_user, $mysql_pass);

?>

Output (Screenshot2)

connecting to a server and database by php script
Screenshot2

Now First thing we do that to call mysql_connect function Now this takes three arguments. The First arguments is the hostname So it will be localhost. second argument is the username root and password i don't had the password so i am going to leave this blank but we had to specify it. How we can make this bit better bit more easy to specify everything. I am actually be going to use variables. So, i am going to say $mysql_host equal to localhost , $mysql_user equal to root, and $mysql_pass equal to nothing. then i will substitute the values inside the mysql_connect function mysql_connect($mysql_host, $mysql_user, $mysql_pass). Output of the script will be blank so we can assume that everything has been connected. 

mysql_connect function
Screenshot3
Now For example we had had incorrect information like in script we had replace root with alex and connect with database it will show us warning:mysql_connect()[function.mysql-connect]:Access denied for user 'alex'@'localhost' (using password: NO) in C:\xampp\htdocs\series\databases\index.php on line 6 (Screenshot3) Because we does not had alex user in our Privileges. Now let say we are running live website and this error return to the user this kind of error that we don't want to show user if we had incorrectly specify the user name or password inside of our code. 

Code for Connection to a server and database by php


<?php

$conn_error = 'could not connect.'



$mysql_host = 'localhost';

$mysql_user = 'alex';

$mysql_pass = '';



@mysql_connect($mysql_host, $mysql_user, $mysql_pass) or die('$conn_error');

?>

Output (Screenshot4)

connecting database by php
Screenshot4
I Don't want to show this kind of error to user if we had incorrectly write username or password in our code. So at the end of the mysql_connect function i will use logical operator in php (or) after that i am going to use die function with my specified message error. which is could not connect. Now what it will do if our username and password is correctly return it will give use blank it assumes it is connected. Otherwise it die page and show my specified error message could not connect. Still it will show the warning now you can hide this warning by error reporting in php or just put @ in mysql_connect function. We had to specify @ symbol before mysql_connect function to remove warning message. @ symbol will  remove the default error reporting from php just on this line. 

Code for connecting to a server and selecting database by php

<?php

$conn_error = 'could not connect.'



$mysql_host = 'localhost';

$mysql_user = 'root';

$mysql_pass = '';



$mysql_db = 'a_database';



@mysql_connect($mysql_host, $mysql_user, $mysql_pass) or die($conn_error);

mysql_select_db('$mysql_db') or die($conn_error);



echo 'Connected!';

?>

Output (Screenshot5)

mysql_select_db function
Screenshot6
Code for connecting to a server and selecting database by php script. Firstly here we had connected to our server by mysql_connect function with passing parameters Such as $mysql_host corresponding to localhost which is our host name of the server. $mysql_user corresponding to root which is our privileges of database and $mysql_pass corresponding to the blank which is our password of the user to connect. Then we had used die function to show our specified message to the screen if the database is not connected it will kill the page and show our specified message. 

connecting database in php
Screenshot6
Now for Selecting database in php mysql_select_db function is used which had one argument the database name. Here we had the database name a_database which is corresponding to the variable $mysql_db. After mysql_select_db function i had call die function if the database name is wrong it will show our specified error meassage for example in script code if i change the database name which does not exists in database You can see screenshot6.

Now Let see Connection to database in php with lot better way to code for the connection to the database. 

Code for Connecting to a server and database Better way.


<?php

$conn_error = 'could not connect.';



$mysql_host ='localhost';

$mysql_user = 'root';

$mysql_pass = '';



$mysql_db = 'a_database;



if(!@mysql_connect($mysql_host, $mysql_user, $mysql_pass) || !@mysql_select_db($mysql_db) {

die($conn_error);

}

?>

Output (Screenshot7)

connected to database by php
Screenshot7

Every-time user connected to server and database we don't want to show them message connected. If Its not connected we want to show our specified message. So, In this script i had NOT operators in If_If else Statement in php. We are checking condition for server and database with @mysql_connect function and @mysql_selected_db function. Remember here @ is used to remove the error reporting in php. Here i had Say if not mysql_connect or if not mysql_select_db found show specified error with die function in php. Here die function will kill the page and show error message which is specified in variable $conn_error for example could not connect. If condition of if statement is not true then die function will executed and will kill the page. As you can see in screenshot7 it does not show any error message which means it is successfully connected to server and database. 

Code for Connecting to a server and database Better way.
Screenshot8
Now let's Change and see how it will show the error message above connection script if i change the $mysql_user variable root to alex you can see the output screenshot8. Or Else if i change the database name a_database to a_database1 you can see the output Screenshot8. It will show the error message which we had specified. So In this article We had learn how to connect to a server and database and how to handle errors related to connection to a server and database. 




No comments:

Post a Comment

Thanks For Comment Will get you Soon..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates