PHP MYSQL Connection

PHP MYSQL Connection






Today i will tell you how to make connection between php and mysql. 


1. Connecting Mysql Database

Use Given Example For make connection with Mysql.





<?php
$mysqli = new mysqli("127.0.0.1", "user", "password", "database");
if ($mysqli->connect_errno) {
    echo "Failed to connect to your MySQL Databas: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info;
?>







2. Select Mysql Database

<?php
// 1. Create a database connection
$connection = mysqli_connect("127.0.0.1", "user", "password");
if (!$connection) {
    die("Connection Problem: " . mysqli_error());
}

$db_select_db = mysqli_select_db($connection, your database name);
if (!$db_select) {
    die("Database selection failed: " . mysqli_error());
}
?>

3. Close Mysql Database


<?php
// Database connection
$connection = mysqli_connect("127.0.0.1", "user", "password");
if (!$connection) {
    die("Connection Problem: " . mysqli_error());
}
// select database with connection
$db_select_db = mysqli_select_db($connection, your database name);
if (!$db_select) {
    die("Database selection failed: " . mysqli_error());
}
//close mysql database connection
mysqli_close($connection);

?>






No comments:

Post a Comment