Fatal Error when trying to view PHP data as JSON w

2019-08-24 04:27发布

问题:

The following error is present when trying to view api.php using localhost:

Fatal error: Assignments can only happen to writable values in /opt/lampp/htdocs/MyApi/api.php on line 27

I had a few errors in my code that I thought would have been contributing to this error (one of which was on line 27), however, still no joy. When I comment out line 27, the error moves on to line 28, and so forth.

I have also tried to change the name of my array to 'job' as it was originally the same name as my table, but didn't seem to fix the issue.

Very inexperienced with PHP (this is actually my first major stab at it), so any help would be much appreciated!

PHP

<?php

define ('DB_HOST', 'localhost');
define ('DB_USER', 'root'); 
define ('DB_PASS', '');
define ('DB_NAME', 'homeflow_database');

$conn = new mysqli (DB_HOST, DB_USER, DB_PASS, DB_NAME);

if (mysqli_connect_errno()) {

    die('Unable to connect to database '. mysqli_connect_error ());
}

$stmt = $conn->prepare ("SELECT maintenance_id, type, property,
more_info, date FROM maintenance;");

$stmt->execute();

$stmt->bind_result($maintenance_id, $type, $property, $more_info, $date);

$job = array();

while($stmt->fetch()) {

$temp = array ();
$temp = ['maintenance_id'] = $maintenance_id;
$temp = ['type'] = $type;
$temp = ['property'] = $property;
$temp = ['more_info'] = $more_info;
$temp = ['date'] = $date;

array_push ($job, $temp);

}

echo json_encode ($job);

Thanks

回答1:

You are not assigning data on an array like you seems to want.

Your ['maintenance_id'] is not a writable value, it is an array.

Try this :

$temp['maintenance_id'] = $maintenance_id;
$temp['type'] = $type;
$temp['property'] = $property;
$temp['more_info'] = $more_info;
$temp['date'] = $date;


回答2:

This code is tested and working fine:

header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');

define ('DB_HOST', 'localhost');
define ('DB_USER', 'user'); 
define ('DB_PASS', 'password');
define ('DB_NAME', 'homeflow_database');

$conn = new mysqli (DB_HOST, DB_USER, DB_PASS, DB_NAME);

if (mysqli_connect_errno()) {
    die('Unable to connect to database '. mysqli_connect_error ());
}

$stmt = $conn->prepare ("SELECT maintenance_id, type, property, more_info, date FROM maintenance;");

$stmt->execute();

$stmt->bind_result($maintenance_id, $type, $property, $more_info, $date);

$job = array();

$i = 0;
while($stmt->fetch()) {
    $job[$i]['maintenance_id'] = $maintenance_id;
    $job[$i]['type'] = $type;
    $job[$i]['property'] = $property;
    $job[$i]['more_info'] = $more_info;
    $job[$i]['date'] = $date;

    $i++;
}

echo json_encode ($job);

Sample table I used:

CREATE TABLE `maintenance` (
  `maintenance_id` int(5) NOT NULL,
  `type` varchar(55) NOT NULL,
  `property` varchar(55) NOT NULL,
  `more_info` varchar(55) NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Final Result: