PHP无法连接到MySQL数据库

时间:2015-10-01 15:59:29

标签: php mysql mysqli xampp

我是网站开发的新手,我在OSX上使用XAMPP。 Apache和MySQL服务器都在运行。但是,我的PHP代码无法连接到数据库。我使用这些凭据和正确的密码:

$host= "localhost"
$user= "root";
$password = "";
$database = "";

$cxn = mysqli_connect($host,$user,$password,$database) or die("Query died: connect");

错误是:

Parse error: syntax error, unexpected 'died' (T_STRING) in /Applications/XAMPP/xamppfiles/htdocs/web/login_reg.php on line 7

Error line : include("dbstuff.inc");

2 个答案:

答案 0 :(得分:1)

您的错误发生在or die("Query died: connect");

请改为尝试:

$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}
mysqli_connect

http://php.net/manual/en/function.mysqli-connect.php

答案 1 :(得分:0)

如果你想使用你使用的风格,那么使用它," name"应该是您创建的数据库名称。

<?php
$dbConnect = array(
    'server' => 'localhost',
    'user' => 'root',
    'pass' => '',
    'name' => ''
);
$db = new mysqli(
    $dbConnect['server'],
    $dbConnect['user'],
    $dbConnect['pass'],
    $dbConnect['name']
);
if ($db->connect_errno>0) {
    echo "Database connection error" . $db->connect_error;
    exit;
}
?>