[英]Can't return array from php function to ajax request
I have a javascript file in which I am doing an ajax request to foo.php
in order to get an array of mysql results. 我有一个javascript文件,其中我正在对
foo.php
执行ajax请求以获取mysql结果数组。 Here is my javascript file. 这是我的javascript文件。
//foo.js
$(document).ready(function(){
$.ajax({
url:"foo.php",
type:"POST",
data:{
action:"test",
remail:"foo"
},
success:function(data){
var messages = data;
console.log("Ext req");
try{
console.log(JSON.parse(data));
}catch(e){
console.log(data);
}
},
failure:function(){
}
})
});
In order to receive my array of results from php I do the following: 为了从php接收我的结果数组,我执行以下操作:
//foo.php
<?php
if(isset($_POST)){
$action = $_POST['action'];
if(empty($action)){
return;
}
switch($action){
case "test":
$query = "select * from message where seen";
$ar = [];
$res = mysqli_query($con,$query);
while($row = mysqli_fetch_array($res)){
$ar[] = $row;
}
echo json_encode($ar);
break;
}
}
?>
This returns an array of objects to my ajax request which then I can handle according to my needs. 这会将一个对象数组返回给我的ajax请求,然后根据我的需要处理。 However if I try to move the php code inside the switch statement into a function and return the encoded result of the function I only get an empty array as response.
但是,如果我尝试将switch语句中的php代码移动到函数中并返回函数的编码结果,我只得到一个空数组作为响应。 Here is how I am trying to do it:
以下是我尝试这样做的方法:
<?php
function test(){
$query = "select * from message where seen";
$ar = [];
$res = mysqli_query($con,$query);
while($row = mysqli_fetch_array($res)){
$ar[] = $row;
}
return $ar;
}
if(isset($_POST)){
$action = $_POST['action'];
if(empty($action)){
return;
}
switch($action){
case "test":
$result = test();
echo json_encode($result);
break;
}
}
?>
Any ideas why this happening? 任何想法为什么会这样?
$con
is a variable that comes from another file which I include
$con
是来自我include
另一个文件的变量
When you moved your query logic into a function, $con
, MySQL's connection object is not available. 当您将查询逻辑移动到函数
$con
,MySQL的连接对象不可用。 Use GLOBAL $con;
使用
GLOBAL $con;
inside your function. 在你的功能里面。
Read this to understand Variable Scope 阅读本文以了解可变范围
Method 1 Using GLOBAL
keyword 方法1使用
GLOBAL
关键字
function test(){
GLOBAL $con;
$query = "select * from message where seen";
$ar = [];
$res = mysqli_query($con,$query);
while($row = mysqli_fetch_array($res)){
$ar[] = $row;
}
return $ar;
}
Method 2 Pass an argument to a function 方法2将参数传递给函数
function test($con){
$query = "select * from message where seen";
$ar = [];
$res = mysqli_query($con,$query);
while($row = mysqli_fetch_array($res)){
$ar[] = $row;
}
return $ar;
}
Call it like this:
test($con);
Global variables if not used carefully can make problems harder to find as other solution suggested. 如果不仔细使用全局变量可能会使问题更难以找到其他解决方案建议。 Pass
$con
as argument to your function: 将
$con
作为参数传递给您的函数:
function test($con){
$query = "select * from message where seen";
$ar = [];
$res = mysqli_query($con,$query);
while($row = mysqli_fetch_array($res)){
$ar[] = $row;
}
return $ar;
}
Let us talk about the problems you have: 让我们谈谈你遇到的问题:
You pass a failure
function to $.ajax
. 您将
failure
函数传递给$.ajax
。 You surely wanted to use error
instead of failure
. 你肯定想用
error
而不是failure
。 See here . 看到这里 。
You have a messages
variable initialized, but unused inside success
. 您已初始化
messages
变量,但未success
。 Get rid of it. 摆脱它。
You check for isset($_POST)
, but that will always be true
. 你检查
isset($_POST)
,但这将永远是true
。 You wanted to check isset($_POST["action"])
instead, or you wanted to check whether it is a POST request . 您想要检查
isset($_POST["action"])
,或者您想检查它是否是POST请求 。
$con
is not available inside the function
. function
内部没有$con
。 You will need to either initialize it inside the function
or pass it to it and use it as a parameter. 您需要在
function
内部初始化它或将其传递给它并将其用作参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.