dqz13288 2016-12-30 09:17
浏览 58
已采纳

试图从网址获取价值并在数据库中搜索,以json格式提供

enter image description herei am new to php webservice and JSON. Now i am trying to get the keyword from url and search whether is it in Database. keywords can be one or more. deliver the result in jSON format. the result will be some url that connected with the value. url will be in database.

<?php
include_once('confi.php');
error_reporting(E_ALL);

//Get the variables here

$key_word = isset($_GET['key_word']) ? mysqli_real_escape_string($conn, $_GET['key_word']) :  "";
$result = explode(',', $key_word);

print_r($result);
$sql = 'SELECT * FROM `kw` WHERE key_word="'.$key_word.'"';
$Obj='';
$query = mysqli_query($conn,$sql) or trigger_error(mysqli_error()." ".$sql);

while($row = mysqli_fetch_array($query))
{

    $key_word = $row['key_word'];
    $Url = $row['Url'];
}
}
/* Output header */
header('Content-type: application/json');
$Obj->key_word = "$key_word";
$Obj->Url = "$Url";
$JSON = json_encode($Obj);
echo $JSON;


?>

i am getting null value for this code. please help me to do it.

i am getting the result like this. it sholud me display separaly and those two will have separate urls.

  • 写回答

2条回答 默认 最新

  • dua27031 2016-12-30 09:36
    关注

    Try like this... Steps:

    1.Explode the kewords into array based on commas

    2.Foreach key find URL

    3.Make the separate array of keys

    4.Make the separate array of urls

    5.Finally use array_combine() to make an associative array of keys and urls

    6.json_encode() your array get the result in json format.

        <?php
    include_once('confi.php');
    error_reporting(E_ALL);
    
    //Get the variables here
    
    $key_word = isset($_GET['key_word']) ? mysqli_real_escape_string($conn, $_GET['key_word']) :  "";
    $result = explode(',', $key_word);
    
     // print_r($result);
    if (count($result>0)) {
    $key_word=array();
    $Url=array();
    
    foreach ($result as $res) {
    $sql = "SELECT * FROM `kw` WHERE key_word='$res'";
    $query = mysqli_query($conn,$sql);
    $row = mysqli_fetch_array($query);
    array_push($key_word, $row['key_word']);
    array_push($Url,$row['Url']);
    
    }
    $return = array_combine($key_word, $Url);
    print_r($return); // here is your final array
    echo json_encode($return); //in json format
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么