gpt4 book ai didi

PHP 调用另一个 PHP 页面进行 MySQL 查询(返回 JSON 数据)

转载 作者:行者123 更新时间:2023-12-01 00:38:44 25 4
gpt4 key购买 nike

我想了解一个 PHP 页面如何调用另一个 PHP 页面,该页面将返回 JSON 数据。

我正在使用 PHP (UsersView.php) 文件来显示我的网站内容。但是,我已将 MySQL 查询分离到另一个 PHP (Get_Users.php) 文件中。

在 Get_Users.php 中,我将有一个 MySQL 语句来查询数据库中的数据。然后它将以 JSON 编码并被回显。

在 UsersView.php 中,我将调用 Get_Users.php 以检索用户 JSON 数据。然后,数据将用于填充“用户表”。

问题是,我不知道如何从“UsersView.php”调用“Get_Users.php”来获取数据。

UserView.php 的一部分

$url =  "get_user.php?id=" . $id;
$json = file_get_contents($url);
$result = json_decode($json, true);

我试图调用同一目录中的文件,但这似乎不起作用。

整个 Get_Users.php

<?php
$connection = mysqli_connect("localhost", "root", "", "bluesky");

// Test if connection succeeded
if(mysqli_connect_errno()) {
die("Database connection failed: " . mysqli_connect_error() . " (" . mysqli_connect_errno() . ") " .
"<br>Please retry your last action. Please retry your last action. " .
"<br>If problem persist, please follow strictly to the instruction manual and restart the system.");
}

$valid = true;

if (!isset($_GET['id'])) {
$valid = false;
$arr=array('success'=>0,'message'=>"No User ID!");
echo json_encode($arr);
}

$id = $_GET['id'];

if($valid == true){
$query = "SELECT * FROM user WHERE id = '$id'";
$result = mysqli_query($connection, $query);
if(mysqli_num_rows($result) == 1){
$row = mysqli_fetch_assoc($result);
$arr=array('success'=>1,'type'=>$row['type'],'user_id'=>$row['id'],'email'=>$row['email'],'name'=>$row['name'],'phone'=>$row['phone'],'notification'=>$row['notification']);
echo json_encode($arr);
}else{
$arr=array('success'=>0,'message'=>"Invalid User ID!");
echo json_encode($arr);
}
}

mysqli_close($connection);
?>

最佳答案

你有几种不同的方法来完成这个:

  • 您应该能够首先设置实际的 id,然后像这样包含 Get_Users.php 文件。请注意,您不应回显 Get_Users.php 的输出,而应仅使用 return json_encode($arr); 返回编码后的 json 数据:

// set the id in $_GET super global
$_GET['id'] = 1;
// include the file and catch the response
$result = include_once('Get_Users.php');
  • 您还可以创建一个可以从 UserView.php 调用的函数:

// Get_Users.php
<?php
function get_user($id) {
// connect to and query database here
// then return the result as json
return json_encode($arr);
}
?>

// In UserView.php you first include the above file and call the function
include_once('Get_Users.php');
$result = get_user(1);
  • 您也可以使用 file_get_contents()。请注意,您需要确保在 php.ini 文件中启用了 allow_url_fopen 才能正常工作:

$result = file_get_contents('http://example.com/Get_Users.php?id=1');

要启用allow_url_fopen,您需要打开加载的配置文件并设置allow_url_fopen=1,最后重启您的网络服务器。


  • 您也可以使用 curl 来获得相同的结果:

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'http://example.com/Get_Users.php?id=1');
$result = curl_exec($ch);
curl_close($ch);

  • 也可以发出 ajax 请求来获取结果。此示例使用 jQuery:

$(document).ready(function() {
$.get({
url: 'Get_Users.php',
data: 'id=1',
success: function(response) {
// response contains your json encoded data
// in this case you **must** use echo to transfer the data from `Get_Users.php`
}
});
});

关于PHP 调用另一个 PHP 页面进行 MySQL 查询(返回 JSON 数据),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39931940/

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