douqi1212 2017-10-23 22:28
浏览 42
已采纳

MySQL Select查询在终端中有效。 在php准备好的声明中失败

Want: Get MySQL table as PHP variable

Error: Fatal error: Call to a member function execute() on a non-object in /home/ubuntu/workspace/List Machines/functions.php on line 90

This error appears upon rendering the page in browser.

Line 90 (as referenced in the error) is this one: $stmt->execute();

Relevant code (condensed slightly for your reading convenience):

<?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)

At this time, the SQL table machines is populated with 3 rows of dummy data. The SQL statement described in the variable $query followed by a ; produces these 3 rows as expected in the terminal.

I've tried various PHP methods to detect possible errors in the $connection and $stmt variables at all points in the code prior to and immediately after the error. All turn up blank.

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

I have also tried granting full permissions to the guest user.

All I can tell is that something seems to be failing at this point: $stmt=$connection->prepare($query); but I am not sure what exactly the failure is. The SQL checks out and the connection seems to be valid.

  • 写回答

2条回答 默认 最新

  • dongpang2029 2017-10-23 22:52
    关注

    You made a typo by the looks of it.

    Change:

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

    to:

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

    (You have used $c9 instead of $database for $connection)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误