2016-10-08 11:44
浏览 22

PHP为MySQL Query调用另一个PHP页面(返回JSON数据)

I would like to find out how a PHP page calls another PHP page, which will return JSON data.

I am working with PHP (UsersView.php) files to display my contents of a website. However, I have separated the MySQL Queries in another PHP (Get_Users.php) file.

In the Get_Users.php, I will have a MySQL statement to query the database for data. It will then encode in JSON and be echo-ed out.

In the UsersView.php, I will call the Get_Users.php in order to retrieve the Users JSON data. The data will then be used to populate a "Users Table".

The thing is, I do not know how to call the "Get_Users.php" from the "UsersView.php" in order to get the data.

Part of UserView.php

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

I am trying to call the file which is in the same directory, but this does not seem to work.

Whole of Get_Users.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);
        echo json_encode($arr);
        $arr=array('success'=>0,'message'=>"Invalid User ID!");
        echo json_encode($arr);


图片转代码服务由CSDN问答提供 功能建议


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

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

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

问题是,我不知道如何从“UsersView.php”中调用“Get_Users.php” 为了获得数据。


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

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


 $ connection = mysqli_connect(“localhost  “,”root“,”“,”bluesky“); 
 die(”数据库连接失败:“。mysqli_connect_error()。”(“  .mysqli_connect_errno()。“)”。
“&lt; br&gt;请重试您的上一个操作。请重试您的上一个操作。”。
“&lt; br&gt;如果问题仍然存在,请严格按照说明手册重新启动 系统。“); 
 $ valid = true; 
if(!isset($ _ GET ['id'])){
 $ valid = false; 
 $ arr = array(  'success'=&gt; 0,'message'=&gt;“没有用户ID!”); 
 echo json_encode($ arr); 
 $ id = $ _GET ['id']; \  n 
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'=&gt; 1,'type'=&gt; $ row ['type'],'user_id  '=&GT; $行[' ID”  ], '电子邮件'=&GT; $行[ '电子邮件'], '名称'=&GT; $行[ '名称'], '电话'=&GT; $行[ '电话'], '通知'=&GT;  $ row ['notification']); 
 echo json_encode($ arr); 
} else {
 $ arr = array('success'=&gt; 0,'message'=&gt;“无效的用户ID!”  ); 
 echo json_encode($ arr); 
mysqli_close($ connection); 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanji4449 2016-10-08 12:24

    You have a couple of different ways to accomplish this:

    • You should be able to first set the actual id and then include the Get_Users.php file like this. Notice that you should not echo out the output from Get_Users.php, instead only return the encoded json data using return json_encode($arr);:

    // set the id in $_GET super global
    $_GET['id'] = 1;
    // include the file and catch the response
    $result = include_once('Get_Users.php');
    • You can also create a function that can be called from UserView.php:

    // Get_Users.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
    $result = get_user(1);
    • You could also use file_get_contents(). Notice that you need to make sure so that allow_url_fopen is enabled in your php.ini file for this to work:

    $result = file_get_contents('');

    To enable allow_url_fopen you need to open up your loaded configuration file and set allow_url_fopen=1 and finally restart your webserver.

    • You could also use curl to achieve the same result:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, '');
    $result = curl_exec($ch);

    • An ajax request could also be made to get the result. This example uses jQuery:

    $(document).ready(function() {
        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`
    打赏 评论
  • drfqfuhej48511519 2016-10-08 12:05

    Change UsersView.php to like this

    $actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['CONTEXT_PREFIX'];
    $url =  "get_users.php?id=" . $id;
    $url = $actual_link.$url;
    $json = file_get_contents($url);
    $result = json_decode($json, true);

    This will work fine.

    打赏 评论

相关推荐 更多相似问题