Wednesday, February 13, 2013

Connect Mysql Database Using Php Script

You can access data in a database, you must create a connection to the database.
In PHP, this is done with the mysql_connect() function.

Syntax :
mysql_connect(servername,username,password);  

Parameter Description
servername Optional. Specifies the server to connect to. Default value is "localhost:3306"
username Optional. Specifies the username to log in with. Default value is the name of the user that owns the server process
password Optional. Specifies the password to log in with. Default is ""



The mysql_select_db() function sets the active MySQL database.
This function returns TRUE on success, or FALSE on failure.

Syntax :
mysql_select_db(database,connection)  

Parameter Description
database Required. Specifies the database to select.
connection Optional. Specifies the MySQL connection. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used.


Final Example :

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

$db_selected = mysql_select_db("test", $con);

if (!$db_selected)
  {
  die ("Can\'t use test_db : " . mysql_error());
  }
?>  

1 comment:

Thanks....