简体   繁体   English

PHP JSON网络服务

[英]PHP JSON Web Services

The database does not answer. 数据库不应答。 What's the problem? 有什么问题? I'm doing with Android. 我正在使用Android。 Similar to that in other web services. 与其他Web服务中的类似。 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);
        }
    //}
    ?>

MySQL functions are deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. MySQL功能在PHP 5.5.0中已弃用,在PHP 7.0.0中已将其删除。 Instead, the MySQLi or PDO_MySQL extension should be used. 相反,应使用MySQLi或PDO_MySQL扩展。

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$gelenid ,因此您将NULL传递给查询。 $id is not defined. $id未定义。 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. 从您的评论看来,您似乎想在MySQLi中看到一个示例。 Be aware that there are tons of examples at php.net and I will show you one here: 请注意,php.net上有大量示例,在这里我将向您展示一个示例:

<?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();
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM