i 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.
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
}
Don't you get some errors or warnings ?
The $Obj var is not a object but a string
change
$Obj='';
by
$Obj = new stdClass();
and don't use quote around variable.