我正在编写一个php文件,以打印mysql数据库中的所有用户。我的问题是在prepare($ sql)和$ statement-> execute之后,语句始终为空。这是我的代码:
public function selectAllUser(){
$response = array();
$sql = "SELECT * FROM Tata.users";
$statement = $this->conn->prepare($sql);
if(!$statement){
throw new Exception($statement->error);
}
//echo json_encode($statement);
$statement->execute();
try {
throw new Exception($statement->error);
} catch (Exception $e){}
$result = $statement->get_result();
//echo json_encode($result);
while ($row = $result->fetch_assoc()){
//echo json_encode($row);
$response[] = $row;
}
return $response;
}
这是我得到的结果:echo $ statement;
{"affected_rows":null,"insert_id":null,"num_rows":null,"param_count":null,"field_count":null,"errno":null,"error":null,"error_list":null,"sqlstate":null,"id":null}
这是我使用此功能的代码:
$file = parse_ini_file("../../../Tata.ini");
$dbhost = trim($file["dbhost"]);
$dbuser = trim($file["dbuser"]);
$dbpass = trim($file["dbpassword"]);
$dbname = trim($file["dbname"]);
require ("secure/access.php");
$access = new access($dbhost,$dbuser,$dbpass,$dbname);
$access->connect();
$users = $access->selectAllUser();
$access->disconnect();
echo json_encode($users);