想要:获取MySQL表作为PHP变量
错误:Fatal error: Call to a member function execute() on a non-object in /home/ubuntu/workspace/List Machines/functions.php on line 90
在浏览器中呈现页面时会出现此错误。
第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)
我还尝试向访客用户授予完全权限。
我所能说的是,此时似乎有些事情失败了:$stmt=$connection->prepare($query);
但我不确定失败到底是什么。 SQL检出并且连接似乎有效。
答案 0 :(得分:1)
你根据它看起来错字。
变化:
$database = "c9";
$connection = new mysqli($servername,$username,$password,$c9);
为:
$database = "c9";
$connection = new mysqli($servername,$username,$password,$database);
($c9
已使用$database
代替$connection
答案 1 :(得分:0)
琐碎的问题。错误地命名变量。
“看起来你在新的mysqli中使用了$ c9而不是$ database(...声明 - shortchng”