duanchangnie7996 2015-11-02 15:00
浏览 105
已采纳

PHP JSON Web服务

The database does not answer. What's the problem? I'm doing with Android. Similar to that in other web services. But they are accurate

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

    if(isset($_POST['id']))
            $gelenid = $_POST['id'];
    else
            $gelenid = $_GET['id'];
    //{
        //$gelenid=$_POST["id"];
    //  $gelenid="36";

        $sql_query="select * from places where ADDED_BY='$id'"; 

        if(!mysql_query($sql_query))
        {
            //  
        }
        else
        {
            $sonucDizisi['basliklar'] = array();

            while($oku=mysql_fetch_array(mysql_query($sql_query)))
            {
                $temp['ilan'] = $oku['TITLE'];
                array_push($sonucDizisi['basliklar'], $temp);
            }

            echo json_encode($sonucDizisi);
        }
    //}
    ?>
  • 写回答

1条回答 默认 最新

  • dousi1906 2015-11-02 17:19
    关注

    MySQL functions are deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

    Since you have nothing to notify you or your app that the query failed, you're getting a blank page.

    You also call $id versus $gelenid, so you're passing a NULL to your query. $id is not defined. Here is an example of your code with a Error catch:

    <?php
    header('Content-Type: text/html; charset=utf-8');
    require_once("connect.php");
    
    if(isset($_POST['id'])){
        $gelenid = $_POST['id'];
    } else {
        $gelenid = $_GET['id'];
    }
    $sql_query = sprintf("select * from places where ADDED_BY='%d'", $gelenid); 
    
    if(!mysql_query($sql_query)){
        $error = array("error" => mysql_errno($link) . ": " . mysql_error($link));
        echo json_encode($error)
    } else {
        $sonucDizisi['basliklar'] = array();
        while($oku=mysql_fetch_array(mysql_query($sql_query))){
            $temp['ilan'] = $oku['TITLE'];
            array_push($sonucDizisi['basliklar'], $temp);
        }
        echo json_encode($sonucDizisi);
    }
    ?>
    

    ** EDIT**

    From your comments, it sounds like you would like to see an example in MySQLi. Be aware that there are tons of examples at php.net and I will show you one here:

    <?php
    header('Content-Type: text/html; charset=utf-8');
    if(isset($_POST['id']))
        $gelenid = $_POST['id'];
    else
        $gelenid = $_GET['id'];
    }
    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
    
    /* check connection */
    if (mysqli_connect_errno()) {
        $error = array("Error" => "Connect failed: " . mysqli_connect_error();
        echo json_encode($error);
        exit();
    }
    
    /* create a prepared statement */
    if ($stmt = $mysqli->prepare("SELECT * FROM places WHERE ADDED_BY=?")) {
        /* bind parameters for markers */
        $stmt->bind_param("i", $gelenid);
    
        /* execute query */
        if(!$stmt->execute()){
            $error = array("error" => $stmt->errno . ": " . $stmt->error);
            echo json_encode($error);
            $stmt->close();
            $mysqli->close();
            exit();
        }
    
        $result = $stmt->get_result();
        $sonucDizisi['basliklar'] = array();
    
        while ($oku = $result->fetch_assoc()) {
            $temp['ilan'] = $oku['TITLE'];
            array_push($sonucDizisi['basliklar'], $temp);
        }
        echo json_encode($sonucDizisi);
    
        /* close statement */
        $stmt->close();
    }
    
    /* close connection */
    $mysqli->close();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP