为 .getJSON 定义特定函数
Defining a specific function for .getJSON
我正在尝试使用 Json 通过调用 function.php 查询数据库来从数据库中获取数据。我 运行 遇到了一个问题,即如何在 function.php 文件中指定特定函数来完成这项工作。
下面是我的 Jquery 和 function.php 的目标是让 Json 使用 function.php 的 getlist()。
请帮助我一段时间以来一直在努力理解这一点。
Jquery 函数:
$.getJSON( "public/includes/functions.php", function( data ) {
var items = [];
$.each( data, function( key, val ) {
console.log('data:' + key + ' And ' + val);
});
});
PHP函数:
function getlist(){
require "dbconn.php";
$result = $stdb->get_results("SELECT id, name FROM supplements");
$stdb->show_errors();
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
} else {
echo "0 results";
}
$js_array = json_encode($array);
echo $js_array;
mysqli_close($conn);
}
function getlist(){
require "dbconn.php";
$result = $stdb->get_results("SELECT id, name FROM supplements");
$stdb->show_errors();
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
} else {
echo "0 results";
}
$js_array = json_encode($array);
echo $js_array;
mysqli_close($conn);
}
//You need to call your function
getlist();
$.getJSON( "public/includes/functions.php?myfun=1", function( data ) {
var items = [];
$.each( data, function( key, val ) {
console.log('data:' + key + ' And ' + val);
});
});
<?php
if(isset($_GET)){
if(isset($_GET['myfun'])){
getlist();exit;
}
}
function getlist(){
require "dbconn.php";
$result = $stdb->get_results("SELECT id, name FROM supplements");
$stdb->show_errors();
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
} else {
echo "0 results";
}
$js_array = json_encode($array);
echo $js_array;
mysqli_close($conn);
}
?>
我正在尝试使用 Json 通过调用 function.php 查询数据库来从数据库中获取数据。我 运行 遇到了一个问题,即如何在 function.php 文件中指定特定函数来完成这项工作。
下面是我的 Jquery 和 function.php 的目标是让 Json 使用 function.php 的 getlist()。
请帮助我一段时间以来一直在努力理解这一点。
Jquery 函数:
$.getJSON( "public/includes/functions.php", function( data ) {
var items = [];
$.each( data, function( key, val ) {
console.log('data:' + key + ' And ' + val);
});
});
PHP函数:
function getlist(){
require "dbconn.php";
$result = $stdb->get_results("SELECT id, name FROM supplements");
$stdb->show_errors();
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
} else {
echo "0 results";
}
$js_array = json_encode($array);
echo $js_array;
mysqli_close($conn);
}
function getlist(){
require "dbconn.php";
$result = $stdb->get_results("SELECT id, name FROM supplements");
$stdb->show_errors();
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
} else {
echo "0 results";
}
$js_array = json_encode($array);
echo $js_array;
mysqli_close($conn);
}
//You need to call your function
getlist();
$.getJSON( "public/includes/functions.php?myfun=1", function( data ) {
var items = [];
$.each( data, function( key, val ) {
console.log('data:' + key + ' And ' + val);
});
});
<?php
if(isset($_GET)){
if(isset($_GET['myfun'])){
getlist();exit;
}
}
function getlist(){
require "dbconn.php";
$result = $stdb->get_results("SELECT id, name FROM supplements");
$stdb->show_errors();
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$array[] = $row;
}
} else {
echo "0 results";
}
$js_array = json_encode($array);
echo $js_array;
mysqli_close($conn);
}
?>