Pages

Tuesday 3 July 2012

PHP + DATABASE

PHP will work with virtually all database software, including Oracle and Sybase but most commonly used is freely available MySQL database.
For this we need to have mysql database.But don't worry we have a mysql database already in our XAMPP installed. We just need to open our XAMPP Control Panel and click on admin button placed along the side of stop button.This will open up your phpmyAdmin page on your WebBrowser.After this on window we will see option providing database creation.Set a Suitable name for your database and after this press the create button after that option of creating tables will appear .From now we need to give a name to our table,specify the number of columns in our table as well there datatypes and other attributes as required.This was all one need to know before beginning. 

Creating a Connection to a MySQL Database:-

The syntax for establishing a MySQL connection is a below. mysql_connect(servername,username,password);

Here mysql_connect is the inbuilt function and all other fields are the optional values for example servername  Specifies the server to connect to default value is "localhost", username Specifies the username to log in with. Default value is the name of the user that owns the server process and password Specifies the password to log in with. Default is "".

Let us now take a simple example:-

Example

In the following example we store the connection in a variable ($con) for later use in the script. The "die" part will be executed if the connection fails:

<?php
$con = mysql_connect("localhost","bajaj","bajaj123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }


// some code
?> 

The output of above program is Could not connect only if no such username exists or the password is incorrect else a blank screen appears.

Now we study creating a database:-

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$sql = 'CREATE Database test_db';
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not create database: ' . mysql_error());
}
echo "Database test_db created successfully\n";
mysql_close($conn);
?>
If the output of above code is Database test_db created successfully
then it is sure that our database is created otherwise something is going
wrong and we need to check output error and solve it.
 
Selecting a Database:-
<?php
$dbhost = 'localhost';
$dbuser = 'guest';
$dbpass = 'guest123';
$conn = mysql_connect($dbhost, $dbuser, $dbpass); //conn is the connection name
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db( 'test_db' ); //test_db is the database name
mysql_close($conn);
?> 

Creating tables:-
 
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$sql = 'CREATE TABLE employee( '.
       'emp_id INT NOT NULL AUTO_INCREMENT, './/proving column names and its attributes
       'emp_name VARCHAR(20) NOT NULL, '.     //proving column names and its attributes

       'emp_address  VARCHAR(20) NOT NULL, '. //proving column names and its attributes

       'emp_salary   INT NOT NULL, '.         //proving column names and its attributes

       'join_date    timestamp(14) NOT NULL, './/proving column names and its attributes

       'primary key ( emp_id ))';

mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not create table: ' . mysql_error());
}
echo "Table employee created successfully\n";
mysql_close($conn);
?>

 

No comments:

Post a Comment