尝试使用XAMPP将PHP数据查看为JSON时发生致命错误 [英] Fatal Error when trying to view PHP data as JSON with XAMPP

查看:443
本文介绍了尝试使用XAMPP将PHP数据查看为JSON时发生致命错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用localhost查看api.php时出现以下错误:

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

致命错误:赋值只能发生在 /opt/lampp/htdocs/MyApi/api.php,第27行

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

我的代码中有一些错误,我认为是导致该错误的原因之一(其中一个在第27行),但是仍然不高兴.当我注释掉第27行时,错误继续前进到第28行,依此类推.

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.

我还试图将数组的名称更改为"job",因为它最初与表的名称相同,但似乎无法解决问题.

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.

对PHP经验不足(这实际上是我的第一个主要工具),所以对您的帮助将不胜感激!

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

谢谢

推荐答案

您不是像您想要的那样在数组上分配数据.

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

您的['maintenance_id']不是可写的值,它是一个数组.

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

尝试一下:

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

这篇关于尝试使用XAMPP将PHP数据查看为JSON时发生致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆