我正在尝试将SQL数据库链接到一个应用程序,我正在关注的教程中包含了这个SQL代码。当我替换占位符并运行它时,它给了我上面的错误。这是代码(数据库名称,用户名和表名已被替换,相关部分除外)。真实密码也以感叹号结尾。
<?php
// Create connection
$con=mysqli_connect(“localhost”, ”databasename”, ”password!”, ”tablename”);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This SQL statement selects ALL from the table ‘Recipes’
$sql = "SELECT * FROM Recipes";
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row = $result->fetch_object())
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
// Close connections
mysqli_close($result);
mysqli_close($con);
?>
完整错误是:
解析错误:语法错误,意外'!'在第4行的/home/content/93/9076293/html/babyfood/service.php
编辑:以下答案有效,但它打开了一个新错误:
Warning: mysqli_connect() [function.mysqli-connect]: (28000/1045): Access denied for user 'databasename'@'184.168.46.96' (using password: YES) in /home/content/93/9076293/html/babyfood/service.php on line 4
Failed to connect to MySQL: Access denied for user 'databasename'@'184.168.46.96' (using password: YES)
Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /home/content/93/9076293/html/babyfood/service.php on line 16
Warning: mysqli_close() expects parameter 1 to be mysqli, null given in /home/content/93/9076293/html/babyfood/service.php on line 36
Warning: mysqli_close() expects parameter 1 to be mysqli, boolean given in /home/content/93/9076293/html/babyfood/service.php on line 37
答案 0 :(得分:3)
您在连接部分使用智能/引号“ ”
而不是标准引号"
。
应该如下:
$con=mysqli_connect("localhost", "databasename", "password!", "tablename");