PHP编码出错了

时间:2017-12-27 07:43:20

标签: php

 <?php
$conn=mysql_connect(`localhost'," root","") or die("Could not connect");
mysql_select_db("bng_nov",$conn) or die("could not connect database");
?>

解析错误:语法错误,意外结束文件,期待&#39;`&#39;在第4行的C:\ xampp \ htdocs \ display \ db.php

2 个答案:

答案 0 :(得分:1)

此行替换(`)与(&#39;)单引号出错。也删除root之前的空格

$conn=mysql_connect('localhost',"root","") or die("Could not connect");

并且最好的解决方案是使用像这样的双引号

$conn=mysql_connect("localhost","root","") or die("Could not connect");

我也提到不要使用mysql_*。你应该使用mysqli_*

我添加了一些数据库连接代码。使用此代码

<?php
$servername = "localhost";
$username = "root";
$password = "";

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

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

答案 1 :(得分:0)

错误在下面一行

$conn=mysql_connect(`localhost'," root","")

请尝试如下

<?php
if (!$link = mysql_connect('localhost', 'root', '')) {
    echo 'Could not connect to mysql';
    exit;
}

if (!mysql_select_db('bng_nov', $link)) {
    echo 'Could not select database';
    exit;

}
?>