gpt4 book ai didi

php - MySQL Select 查询在终端中运行。 php 准备语句失败

转载 作者:行者123 更新时间:2023-11-29 10:33:43 24 4
gpt4 key购买 nike

想要:获取 MySQL 表作为 PHP 变量

错误: fatal error :在第 90 行/home/ubuntu/workspace/List Machines/functions.php 中的非对象上调用成员函数execute()

在浏览器中渲染页面时会出现此错误。

第 90 行(如错误中所引用)是:$stmt->execute();

相关代码(为方便阅读而稍微精简):

<?php
$servername = "0.0.0.0";
$username = "guest";
$password = "password";
$database = "c9";
$connection = new mysqli($servername,$username,$password,$c9);
// Check connection
if ($connection->connect_error)
{
die("Connection failed: " . $connection->connect_error);
echo "Problem connecting.";
}

function retrieveMachineList(){
global $connection;

$query = "SELECT
machine_id,
manufacturer,
model,
model_year,
type,
warranty_type,
warranty_end_date,
vendor,
purchase_date,
verified_date,
retired_date,
serial
FROM machines";

$stmt=$connection->prepare($query);

$stmt->execute();

$stmt->bind_result($machine_id,
$manufacturer,
$model,
$model_year,
$type,
$warranty_type,
$warranty_type,
$vendor,
$purchase_date,
$verified_date,
$retired_date,
$serial);

$i=0;

while ($stmt->fetch())
{
$rows[$i]['machine_id'] = $machine_id;
$rows[$i]['manufacturer'] = $manufacturer;
$rows[$i]['model'] = $model;
$rows[$i]['model_year'] = $model_year;
$rows[$i]['type'] = $type;
$rows[$i]['warranty_type'] = $warranty_type;
$rows[$i]['warranty_end_date'] = $warranty_end_date;
$rows[$i]['vendor'] = $vendor;
$rows[$i]['purchase_date'] = $purchase_date;
$rows[$i]['verified_date'] = $verified_date;
$rows[$i]['retired_date'] = $retired_date;
$rows[$i]['serial'] = $serial;
$i++;
}
$stmt->close();
return $rows;
}

$rows = array(retrieveMachineList());
?>

SQL:

mysql> describe machines;
+-------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+-------------+------+-----+---------+----------------+
| machine_id | int(11) | NO | PRI | NULL | auto_increment |
| manufacturer | varchar(64) | YES | | NULL | |
| model | varchar(64) | YES | | NULL | |
| model_year | int(11) | YES | | NULL | |
| type | varchar(32) | YES | | NULL | |
| warranty_type | varchar(32) | YES | | NULL | |
| warranty_end_date | int(11) | YES | | NULL | |
| vendor | varchar(32) | YES | | NULL | |
| purchase_date | int(11) | YES | | NULL | |
| verified_date | int(11) | YES | | NULL | |
| retired_date | int(11) | YES | | 0 | |
| serial | varchar(64) | YES | | NULL | |
+-------------------+-------------+------+-----+---------+----------------+
12 rows in set (0.00 sec)

此时,SQL 表 machines 填充了 3 行虚拟数据。变量 $query 中描述的 SQL 语句后跟 ; 会按照终端中的预期生成这 3 行。

我尝试了各种 PHP 方法来检测代码中在错误发生之前和之后的所有点上 $connection 和 $stmt 变量中可能存在的错误。全部显示为空白。

echo var_dump($stmt); 的结果:/home/ubuntu/workspace/List Machines/functions.php:90: bool(false)

我还尝试向 guest 用户授予完全权限。

我所能告诉的是,此时似乎有些东西失败了:$stmt=$connection->prepare($query);但我不确定失败到底是什么。 SQL 检查出来并且连接似乎是有效的。

最佳答案

从外观上看,您打错了字。

更改:

$database = "c9";
$connection = new mysqli($servername,$username,$password,$c9);

至:

$database = "c9";
$connection = new mysqli($servername,$username,$password,$database);

(您已使用 $c9 而不是 $database 作为 $connection)

关于php - MySQL Select 查询在终端中运行。 php 准备语句失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46899474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com