简体   繁体   English

尝试使用XAMPP将PHP数据查看为JSON时发生致命错误

[英]Fatal Error when trying to view PHP data as JSON with XAMPP

The following error is present when trying to view api.php using localhost: 尝试使用本地主机查看api.php时出现以下错误:

Fatal error: Assignments can only happen to writable values in /opt/lampp/htdocs/MyApi/api.php on line 27 致命错误:第27行的/opt/lampp/htdocs/MyApi/api.php中的可写值只能发生赋值

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. 我的代码中有一些错误,我认为是造成该错误的原因之一(其中一个在第27行),但是仍然不高兴。 When I comment out line 27, the error moves on to line 28, and so forth. 当我注释掉第27行时,错误继续前进到第28行,依此类推。

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. 我还尝试将数组的名称更改为“ job”,因为它最初与表的名称相同,但似乎无法解决该问题。

Very inexperienced with PHP (this is actually my first major stab at it), so any help would be much appreciated! PHP经验不足(这实际上是我的第一个主要攻略),因此,任何帮助将不胜感激!

PHP 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 谢谢

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. 您的['maintenance_id']不是可写的值,它是一个数组。

Try this : 尝试这个 :

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

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: 最后结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM