PHP provides mysqli_connect() function to open a database connection. This function takes five parameters and returns a MySQLi link identifier on success or FALSE on failure.
//connection
mysqli_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 MySQLi user name |
password | The password to log in with |
database_name | Optional. The database to be used when performing queries |
<?php
// Create connection
$db=mysqli_connect("localhost","root","","my_db") or die(mysqli_error());
?>
<?php
// Create connection
$db=mysqli_connect("localhost","root","","myDatabaseName") or die(mysqli_error());
//code to be executed...
// Close connection
mysqli_close($db);
?>
Note: After work with the database is completed we have to close the connection using mysqli_close() function in which the connection to the database is passed.
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning