By 05/02/2014 8 Comments

How to Connect Database in PHP Using MySQL Database

Using Php we can connect our application with databases. The most popular database system used with PHP is MySQL . It is an open source and lightweight DBMS system.

What is MySQL?

  • MySQL is a software tool for managing the RDBMS (Relational database management system)
  • MySQL is fine for small and large applications
  • MySQL is lightweight, fast and easy to use
  • MySQL is an open source which means free

What is RDBMS?

  • It stands for Relational database management system.
  • It is a DBMS which is based on relational models
  • Relational models are more then one database tables which is referenced by each other through some rules
  • RDBMS is a modern database approach which is used by many web applications now days
  • With RDBMS you can retrieve and manipulate data from database easily
  • In RDBMS you breaks the large database tables into small  pieces

What is a database table?

  • All RDBMS data is being stored in database tables also called tables
  • Tables are also called objects or database entities
  • A table is a set of columns and rows
  • each column in a table is called attributes
  • each row in a table called database record
  • Any data which is exists inside column is called a database item

Connect an application with MySQL database in php.

<?php

if(basename(__FILE__) == basename($_SERVER['PHP_SELF'])) send_404();

$dbHost = "localhost"; //Location Of Database usually it can be localhost

$dbUser = "xxxx"; //Database User Name or your localhost user name

$dbPass = "xxxx"; //Database Password or your localhost password

$dbDatabase = "xxxx"; //Database Name

$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database.");//Make a live link or connection with MySQL host, pass 3 arguments which sets the host name, user name and password (depends on your configuration settings)

mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");// Select of database name and pass 2 arguments which sets database name and seconds argument sets the connection string [second argument is an optional]

// now we write basic query to database as bellow:
$query = "SELECT * FROM table_name"; // Standard sql query to database which select all data from database table

$result = mysql_query($query); // Process our Sql query and assign it in $result variable, pass 1 argument ($query) which has a standard sql query 

// now fetch the data from database and display it on webpage
while($rows = mysql_fetch_array($result)) //Loop through each records from db and display. while loop has ($rows = mysql_fetch_array($result)) function as a boolean value, this code read the query ($result) and convert it in an array and then assign that array into $rows variable, finally echo result and display it on webpage
{ 
echo $rows['database_column_name']; // you can pass your own database column name in $rows array
} 

mysql_close($db);// close the current database connection, pass 1 argument ($db) of connection string

?>

This function will send an imitation 404 page if the user types in this file’s file name into the address bar. only files connecting with in the same directory as this file will be able to use it as well.

<?php

function send_404()

{

header('HTTP/1.x 404 Not Found');

print ''."n".

''."n".

''."n".

''."n".

'
Not Found

'."n".

'
The requested URL '.

str_replace(strstr($_SERVER['REQUEST_URI'], '?'), '', $_SERVER['REQUEST_URI']).

' was not found on this server.

'."n".

''."n";

exit;

}

?>

In any file you want to connect to the database, and in this case we will name this file db.php just add this line of php code (without the pound sign): include”db.php”;







Posted in: PHP

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .