PHP provides mysql_connect() function to open a database connection. This function takes five parameters and returns a MySQL link identifier on success or FALSE on failure.
//connection
mysql_connect(server_name,user_name,password,database_name);
Parameter | Description |
---|---|
host(Server name) | Either a hostname(server name) or an IP address (If you are working on local system then use localhost or 127.0.0.1 ) |
username | The MySQL user name |
password | The password to log in with |
database_name | Optional. The database to be used when performing queries |
<?php
// Create connection
$db=mysql_connect("localhost","root","","my_db") or die(mysql_error());
?>
<?php
// Create connection
$db=mysql_connect("localhost","root","","myDatabaseName") or die(mysql_error());
//code to be executed...
// Close connection
mysql_close($db);
?>
Note: After work with the database is completed we have to close the connection using mysql_close() function in which the connection to the database is passed.
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning