如何将MYSQL与PHP连接

时间:2018-01-30 10:47:03

标签: php mysql

我写完所有代码后卡住了,网站根本就没有连接mysql!

2 个答案:

答案 0 :(得分:2)

我认为这段代码会有所帮助

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

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

?>

将此代码写入html文件并将其另存为.php 我希望它有效

答案 1 :(得分:-1)

在phpmyadmin中创建一个数据库,然后在文件中记下下面的代码并用.php扩展名保存

<?php
    $db_username = "db_username";  // Your database login username 
    $db_password = "db_password";  // Your database login password
    $db_name = "db_name";      // The name of the database you wish to use
    $db_host = "db_host";   // The address of the database. Often this is localhost, but may be for example db.yoursite.com
    $conn = new mysqli($db_host, $db_username, $db_password, $db_name);
    if (mysqli_connect_errno())
      {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
?>

根据您的数据库详细信息设置这4个变量。如果连接无法建立,mysqli_connect_error()会给您错误