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