当连接正确时,无法从数据库中获取数据

时间:2017-11-19 17:05:04

标签: php mysqli

我有这段代码:

include "config.php";
if (isset($connect)) {
    echo "connected";
} else {
    echo "not connected";
}

这是config.php文件:

$localhost = "localhost";
$db_username = "*****";
$db_password = "*****";
$db_name = "*****"; 
If($connect = mysqli_connect("$localhost ","$db_username ","$db_password ","$db_name ")){}or die{}  

我不知道为什么,但是当我尝试做一些查询时,我没有得到任何东西

$sql="SELECT * FROM table_name ORDER BY column_name ASC";
$query=($connect,$sql);
While($row = mysqli_fetch_object($query)) {
    Echo $row>column;
}

错误是:

 Parse error: syntax error, unexpected '}' in /path/index.php on line 6

1 个答案:

答案 0 :(得分:2)

我在您的代码中看到了多个问题,

首先来自config.php

If($connect = mysqli_connect("$localhost ","$db_username ","$db_password ","$db_name ")){}or die{}  

这不是正确的代码,{}or die{}中的错误必须是{} else {die();}

但我建议你改用这个,

$connect = mysqli_connect("$localhost", "$db_username", "$db_password", "$db_name") or die(mysqli_connect_error());

关于第三个代码有一些问题,首先是

$query=($connect,$sql);

你忘了使用mysqli_query

$query=mysqli_query($connect, $sql);

在打印数据时,您必须使用->而不是>

Echo $row->column;

我希望这会有所帮助。