我正在写一个简单的数据库来练习,但我一直收到这个错误:
致命错误:在第43行的/Applications/XAMPP/xamppfiles/htdocs/index.php中调用boolean上的成员函数query()
查询行是第43,但我不明白为什么它是一个布尔值既不$conn
或者$db_handle
是false
,这是为什么?
<?php
$server = "localhost";
$database = "motocross_db";
$username = "root";
$password = "";
$conn = new mysqli($server, $username, $password);
$db_handle = $conn->select_db($database);
if ($db_handle)
{
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
// upload
}
$enrollments_sql = "SELECT
users.first_name,
users.last_name,
registrations.attendance_date,
registrations.attendance_location,
bikes.bike_brand,
bikes.engine_size,
bikes.bike_type
FROM users
INNER JOIN registrations
ON users.user_id = registrations.user_id
INNER JOIN bikes
ON registrations.bike_id = bikes.bike_id";
$enrollments_result = $db_handle->query($enrollments_sql);
?>
Run Code Online (Sandbox Code Playgroud)
更改
$db_handle->query($enrollments_sql);
Run Code Online (Sandbox Code Playgroud)
至
$conn->query($enrollments_sql);// use $conn here
Run Code Online (Sandbox Code Playgroud)
因为mysqli_query()
需要第一个参数作为数据库连接
阅读http://php.net/manual/en/mysqli.query.php