简体   繁体   English

PHP的MYSQL数据库连接

[英]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. 我将我的网站数据库与php mysql_connect一起使用,但是我的托管服务提供商说我需要使用mysqli_connect。

When I edit connection file and update change method its not working with mysqli. 当我编辑连接文件并更新更改方法时,它不适用于mysqli。

Currently using this code: 当前正在使用此代码:

MSQL Method: MSQL方法:

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

and for mysqli I am using this code but fail 对于mysqli,我正在使用此代码,但失败

MYSQLI Method: MYSQLI方法:

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

Please help. 请帮忙。 Why is mysqli not working? 为什么mysqli不起作用? Is my method wrong? 我的方法错了吗?

Try this: 尝试这个:

Example (MySQLi Object-Oriented) 示例(MySQLi面向对象)

 <?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) 示例(MySQLi程序)

<?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";
?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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