显示解析错误:" @"在dbc connect上。我怎么解决它?

时间:2015-07-02 07:24:06

标签: php mysqli

  

错误 - "解析错误:语法错误,意外' @'在   /home/vol2_8/byethost13.com/b13_16347614/digiquiz.my-style.in/htdocs/SimplePHPQuiz-master/includes/db_conn.php   在第7行"

这是我的代码:

<?php 
// Set the database access information as constants
DEFINE ('DB_USER', 'XXXXXXXXX');
DEFINE ('DB_PASSWORD', 'XXXXXXX');
DEFINE ('DB_HOST', 'XXXXXXXXXXX');
DEFINE ('DB_NAME', 'XXXXXXXXXXX');

@ $dbc = new mysqli(XXXXXXXXXX, XXXXXXXX, XXXXXXXXX, XXXXXXXX); // details were added as constants not define's

if (mysqli_connect_error()){
    echo "Could not connect to MySql. Please try again";
    exit();
}
?>

3 个答案:

答案 0 :(得分:3)

为什么不使用这样的定义:

<?php 
//Set the database access information as constants
DEFINE ('DB_USER', 'xxx');
DEFINE ('DB_PASSWORD', 'xxx');
DEFINE ('DB_HOST', 'xxx');
DEFINE ('DB_NAME', 'xxx');
@ $dbc = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (mysqli_connect_error()){
    echo "Could not connect to MySql. Please try again";
    exit();
}

删除$ dbc旁边的@(语法无效)。

@ $dbc = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

使用以下内容检查错误,因为您使用mysqli类的变量:

if ($dbc->connect_errno) {
    printf("Connect failed: %s\n", $dbc->connect_error);
    exit();
}

所有这些都应该是这样的:

<?php 
//Set the database access information as constants
define('DB_USER', 'xxx');
define('DB_PASSWORD', 'xxx');
define('DB_HOST', 'xxx');
define('DB_NAME', 'xxx');
$dbc = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($dbc->connect_errno) {
    printf("Connect failed: %s\n", $dbc->connect_error);
    exit();
}

答案 1 :(得分:2)

<?php 
DEFINE ('DB_USER', 'xxx');
DEFINE ('DB_PASSWORD', 'xxx');
DEFINE ('DB_HOST', 'xxx');
DEFINE ('DB_NAME', 'xxx');
$dbc = new mysqli(DB_HOST,DB_USER, DB_PASSWORD, DB_NAME);
// use constants, you have defined it already
if (mysqli_connect_error()){
echo "Could not connect to MySql. Please try again";
exit();
}
?>

答案 2 :(得分:1)

使用它非常简单;

<?php 
$host = "hostname";
$db_user = "dbusername";
$bd_pass= "password";
$db_name ="dbname";


$dbc = new mysqli($host,$db_user,$bd_pass,$db_name);
if (mysqli_connect_error()){
echo "Could not connect to MySql. Please try again";
    echo mysqli_error();
exit();
}
?>