【问题标题】:get request returning empty json with valid parameter获取返回具有有效参数的空 json 的请求
【发布时间】:2019-02-08 16:29:00
【问题描述】:

所以我在网站的 phpmyadmin 中有一个附属公司数据库,我制作了一个 get api 来获取给定 ID 号的特定附属公司,现在我可以连接并拨打电话,但每次我只得到一个空的 json例子

["","","","","",""]

身份证号码是有效的,无效的甚至是字母都没有关系,这就是我每次得到的

现在这是我的连接代码

 <?php 
 header( 'Content-Type: text/html;charset=utf-8' );


 function ejecutarSQLCommand($commando){

 $mysqli = new mysqli("ip", "user", "pass","database");

 /* check connection */
 if ($mysqli->connect_errno) {
 printf("Connect failed: %s\n", $mysqli->connect_error);
 exit();
 }

 if ( $mysqli->multi_query($commando)) {
    if ($resultset = $mysqli->store_result()) {
       while ($row = $resultset->fetch_array(MYSQLI_BOTH)) {

       }
        $resultset->free();
     }


}



$mysqli->close();
}

function getSQLResultSet($commando){


  $mysqli = new mysqli("ip", "user", "pass", "database");
  /* check connection */
  if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
  }

if ( $mysqli->multi_query($commando)) {
    return $mysqli->store_result();




}



$mysqli->close();
}


?>

这是请求文件

<?php
include('function.php');
$id=$_GET["ID"];


if($resultset=getSQLResultSet("SELECT * FROM `table` WHERE ID='$id'")){
    while ($row = $resultset->fetch_array(MYSQLI_NUM)){
        echo json_encode($row);
    }
}

?>

【问题讨论】:

  • 我会 var_dump() $row 以确保你有东西。每行将是一个单独的 json 对象然后使用准备好的语句来保护您的数据库 bobby-tables.com
  • @JasonK 我只加入附属公司(一次一行)而且我似乎没有得到任何东西 array(5) { [0]=> string(0) "" [1]=> string(0) "" [2]=> string(0) "" [3]=> string(0) "" [4]=> string(0) "" } null 知道我能做些什么吗?

标签: php json rest get


【解决方案1】:

我编写了更真实的代码。下一步就是把它变成一个类。

准备而不是查询将防止 SQL 注入以及其中包含引号的数据。

index.php

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

include('function.php');

$id=$_GET["ID"];

dbConnect();
$rows = getSQLResultSet($id);
echo json_encode($rows);

dbClos​​e();

function.php

<?php
//
// not sure if this is needed for returning json
//
header( 'Content-Type: text/html;charset=utf-8' );

//////////////////////////////////////////////////////////////////////////////////////////////////////  
function  dbConnect(){
    //
    // You should be gettng the db connect info from a source out side the webservers reach.
    //
    // read a ini file or imclude a file
    //

    $ip     = '127.0.0.1';
    $user = 'test';
    $pass = 'test12';
    $db     = 'test';

    $GLOBALS['mysqlI'] = new mysqli($ip, $user, $pass, $db);

    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_errno());
        exit();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
function dbClose(){
    $GLOBALS['mysqlI']->close();
}
/////////////////////////////////////////////////////////////////////////////////////////////////
function getSQLResultSet($id){
    $stmt = $GLOBALS['mysqlI']->prepare('SELECT * FROM test WHERE ID = ?');
    $stmt->bind_param('i', $id);
    $stmt->execute();
    $result = $stmt->get_result();
    while($row = $result->fetch_object()){
        $ret[] = $row;
    }
    return $ret;
}

【讨论】:

  • 现在我刚刚收到一些错误通知,您知道,DNI 是我的 ID 的名称 注意:未定义索引:第 8 行 location/query.php 中的 DNI 注意:使用未定义常量 DNI - 在第 35 行的 location/function.php 中假定为“DNI” 致命错误:无法通过第 35 行的 location/function.php 中的引用传递参数 2
猜你喜欢
  • 2011-01-27
  • 2019-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-15
  • 2021-04-29
  • 1970-01-01
相关资源
最近更新 更多