简体   繁体   中英

MYSQL Database connection for php

I am using my website database with php mysql_connect but my hosting provider is saying I need to use mysqli_connect.

When I edit connection file and update change method its not working with mysqli.

Currently using this code:

MSQL Method:

$con=mysql_connect("localhost","username","password");
mysql_select_db("databasename"); 

and for mysqli I am using this code but fail

MYSQLI Method:

$con = mysqli_connect("localhost","my_user","my_password","my_db");

Please help. Why is mysqli not working? Is my method wrong?

Try this:

Example (MySQLi Object-Oriented)

 <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
`   $dbname = "myDB";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";
    ?>  

Example (MySQLi Procedural)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password,  $dbname);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM