gpt4 book ai didi

Php 和 AngularJs - 获取数据

转载 作者:行者123 更新时间:2023-11-29 07:37:09 24 4
gpt4 key购买 nike

我正在为从 MySQL 服务器获取带有标题等的“消息”创建简单请求。

所以,我的 AngularJS 中有这样的东西:

        $http({
url: 'http://localhost/webpack/downloadMessage.php',
method: 'GET',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(response){
console.log("CHECKED");
console.log(response.data);
}, function(response) {
alert('something wrong');
})
})
}

只是一个数据请求。但是我对我的 php 代码感到困惑,因为我是初学者,你能帮我看看哪里出了问题吗?我只想要整张表,其中 section = 3。

<?php 

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

if(!isset($_POST)) die();

session_start();

$response = [];

$con = mysqli_connect('localhost', 'root', '', 'projekt');

$query = "SELECT * FROM messages WHERE section='3'";

$result = mysqli_query($con, $query);

echo json_encode($result);

它没有错误,但在我的控制台中我得到了这个:

CHECKED {current_field: null, field_count: null, lengths: null,num_rows: null, type: null}

最佳答案

根据 manual如果查询失败,mysqli_query 的结果是 mysql_resultfalse

要从 mysqli_result 获取数据,您需要获取它,例如 fetch_assoc :

$result = mysqli_query($con, $query);
$row = mysqli_fetch_assoc($result);

echo json_encode($row);

此外,在某些情况下,您没有结果($row 将为空)或者您的查询失败( $result 将为 false)。应在您的脚本中检查这些情况,并返回适当的响应。

关于Php 和 AngularJs - 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48249979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com