我的MySQL数据库有一个连接函数,如下所示:
function connect() {
$config = parse_ini_file('../../config.ini');
// Try and connect to the database
self::$conn = mysqli_connect('localhost',$config['username'],$config['password'],$config['dbname']);
//Check if both are bools and FALSE === checks type and equality
if(self::$conn === false) {
header("HTTP/1.1 500 Internal Server Error");
header("Content-Type: application/json");
$response = array("Response"=>"Failed to connect to the database");
echo "json_encode($response)";
die();
}
}
当我执行json_encode(用于失败连接)时遇到问题。我收到错误: 注意: /var/www/html/somesite.co/public_html/API/index.php 中的数组到字符串转换< b> 47
json_encode(数组)
真的不知道我为什么会收到这个错误。我之前编码和echo'd和数组都没有问题。有人可以给我一些指示我可能做错的事吗?
答案 0 :(得分:5)
您在尝试echo
array
时收到此错误消息。并且您不用引号括起函数调用。
替换
echo "json_encode($response)";
带
echo json_encode($response);
答案 1 :(得分:0)
对于Laravel,如果您尝试使用dd(),例如
,也会出现此错误S2: ['4', '1', '1']
S1: ['1', '2', '2']
S3: ['3', '2', '3']
S4: ['1', '6', '5']
NAME: ['A', 'B', 'C']