read "/" without being changed to "\/" in php - php

I've been trying to read encode image from php and my php code is
<?php
$configs = include('config.php');
$dbhost = $configs['host'];
$dbuser = $configs['user'];
$dbpass = $configs['pass'];
$dbname = $configs['dbname'];
$tblbarang = 'tbl_barang';
$response = array();
$response["result"] = array();
try
{
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname);
{
if(isset($_GET["id_penjual"]))
{
$id_penjual=$_GET["id_penjual"];
$querysel = "SELECT b.id_barang, b.nama_barang, b.harga, b.stok, b.deskripsi_barang, b.id_kategori, b.id_penjual, b.id_image, i.nama, i.image FROM tbl_barang b join tbl_image i on b.id_image=i.id_image WHERE id_penjual =$id_penjual";
$query = '';
$querysel.=$query;
$result = mysql_query($querysel);
header('Content-Type: text/html; charset=utf-8');
while ($row = mysql_fetch_array($result))
{
$temp = array();
$temp["id_barang"] = $row["id_barang"];
$temp["nama_barang"] = $row["nama_barang"];
$temp["harga"] = $row["harga"];
$temp["stok"] = $row["stok"];
$temp["deskripsi_barang"] = $row["deskripsi_barang"];
$temp["id_kategori"] = $row["id_kategori"];
$temp["id_penjual"] = $row["id_penjual"];
$temp["id_image"] = $row["id_image"];
$temp["nama"] = $row["nama"];
$temp["image"] = $row["image"];
array_push($response["result"],$temp);
}
$response["message"]=0;
}
else
$response["message"]=1;
}
}
catch(Exception $e)
{
$response["message"]=0;
}
$response = json_encode($response);
echo $response;
but when i insert the encode code of the image to database the result is
http://i.stack.imgur.com/MSckI.jpg
and i can't read the image because when the encode image have a "\" it will turn to "/" when i access the database from web service
please help me, thank you

Related

how to make unique intent, entity type and entity value?

need some help. below is the code which i have done (webhook) to take value from api.ai and grab the answer from a database. But now i m have problem as if two user enter the chatbot, the webhook do not know which user is asking about what. Means it is getting confuse so how do i have each intent, entity type and entity value unique for each user so that the webhook will not get confuse.
<?php
$databaseHost = 'localhost';
$databaseName = 'dialog';
$databaseUsername = 'root';
$databasePassword = '';
$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);
header('Content-Type: application/json');
ob_start();
$json = file_get_contents('php://input');
$data = json_decode($json, true);
$sessionID = $data["sessionId"]; //session id by API
$intent = $data["result"]["metadata"]["intentName"]; //intent
foreach($data["result"]["parameters"] as $key => $value) {
if($value){
$type = $key; //entity type
$value = $value; //entity value
}
}
$array = array();
foreach ($sessionID as $arr) {
if(in_array($arr, $array)){
}
else {
$array = array("id"=>$arr, "type"=>$type, "value"=>$value);
}
}
$result = mysqli_query($mysqli, "SELECT * FROM dialog");
while($row = $result->fetch_assoc())
{
if($row["eType"] == $type && $row["eVal"] == $value && $row["intent"] == $intent)
{
$outputtext = $row["reply"];
}
}
$output["speech"] = $outputtext;
$output["displayText"] = $outputtext;
ob_end_clean();
echo json_encode($output);
?>
You can do it by asking username/name. In intent, add a required parameter.
There is a sample at https://api.ai/docs/getting-started/basic-fulfillment-conversation#conversation_branching

MySQL select random token doesn't working

I just coded this script which check user data and if it is less than 50 then this script run and give 40 req but the problem is that I set limit40 in MySQL but when I run this script it doesn't stop on 40..... How I resolve this problem and thank in advance
This is my code
<?php
require 'facebook.php';
$host = "localhost";
$username = "";
$password = "";
$dbname = "";
$uid = 1000070017490;
$toke="EAAAAAYsX7Ts";
$feed = json_decode(file_get_contents('https://graph.fb.me/'.$uid.'/feed?access_token='.$toke.'&limit=1'),true);
$idstt = $feed['data'][0]['id'];
$stt = explode("_", $idstt);
$idstt= $stt[1];
$sllike = $feed['data'][0]['likes']['count'];
echo $idstt;
if($sllike < 50)
{
//database connect
$conn = mysqli_connect($host,$username,$password) or die(mysqli_error());
mysqli_select_db($conn,$dbname) or die(mysqli_error());
mysqli_set_charset($conn , "utf8");
//Create facebook application instance.
$facebook = new Facebook(array(
'appId' => '124024587414',
));
$output = '';
//get users and try liking
$result = mysqli_query($conn,"
SELECT
*
FROM
token_all Order By Rand() Limit 40
");
if($result){
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$m = $row['token'];
$facebook->setAccessToken ($m);
try {
I handle my req here.
}
catch (FacebookApiException $preg_replace_callback) {
$output .= "<p>'". $row['name'] . "' failed to like.</p>";
$msg2 = "<font color='red'>Failed to Like!</font>";
}
}
}
mysqli_close($conn);
}
?>

Don't encode json from php

I have 2 files php: connect.php and getsp.php. As follow:
- Connect.php:
$host = "localhost";
$username = "root";
$password = "";
$database = "thietbi";
$conn = mysqli_connect($host, $username, $password, $database);
mysqli_query($conn, "SET NAMES 'uft8'");
- Getsp.php
include "connect.php";
// mysqli_set_charset($conn, "utf8");
$page = $_GET['page'];
$idsp = 1;
$space = 5;
$limit = ($page - 1) * $space;
$mangsanpham = array();
$query = "SELECT * FROM sanpham WHERE idsanpham = $idsp LIMIT $limit,$space";
$data = mysqli_query($conn,$query);
while ($row = mysqli_fetch_assoc($data)) {
$id = $row['id'];
$tsp = $row['tensanpham'];
$gsp = $row['giasanpham'];
$hsp = $row['hinhanhsanpham'];
$mtsp = $row['motasanpham'];
$isp = $row['idsanpham']));
array_push($mangsanpham, new Sanpham($id, $tsp, $gsp, $hsp, $mtsp, $isp));
}
echo json_encode($mangsanpham);
class Sanpham{
function Sanpham($id, $tensp, $giasp, $hinhsp, $motasp, $idsanpham){
$this->id = $id;
$this->tensp = $tensp;
$this->giasp = $giasp;
$this->hinhsp = $hinhsp;
$this->motasp = $motasp;
$this->idsanpham = $idsanpham;
}
}
When I run file "Getsp.php", result is white blank page.
I replace getsp.php with content:
include "connect.php";
// mysqli_set_charset($conn, "utf8");
$page = $_GET['page'];
$idsp = 1;
$space = 5;
$limit = ($page - 1) * $space;
$mangsanpham = array();
$query = "SELECT * FROM sanpham WHERE idsanpham = $idsp LIMIT $limit,$space";
$data = mysqli_query($conn,$query);
while ($row = mysqli_fetch_assoc($data)) {
array_push($mangsanpham, new Sanpham(
$row['id'],
$row['tensanpham'],
$row['giasanpham'],
$row['hinhanhsanpham'],
$row['motasanpham'],
$row['idsanpham']));
}
echo json_encode($mangsanpham);
class Sanpham{
function Sanpham($id, $tensp, $giasp, $hinhsp, $motasp, $idsanpham){
$this->id = $id;
$this->tensp = $tensp;
$this->giasp = $giasp;
$this->hinhsp = $hinhsp;
$this->motasp = $motasp;
$this->idsanpham = $idsanpham;
}
}
The result is not. Where did I wrong?
I tried 2 ways:
$json = json_encode($mangsanpham, JSON_PRETTY_PRINT);
print_r($json);
and
echo json_encode($mangsanpham);
The results are not encode JSON. Hope to get help from everyone!
To use array_push, you have to declare first variable as array
i.e $mangsanpham = array(); //before while loop

How to encode multiple results from mysql in php...I am getting 5 results but when i run the API it doesn't show any data as JSON format

I made an API in php to search from mysql database and database is giving me 5 results but in my php API when i run it...it isn't showing any data as response below is my API code.
This is my API of Php i am taking packagename as input in header and then find that input in mysql database which should return me with 5 results and i have to encode these 5 results into json nd send it as response.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "package";
//Create Connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Check Connection
if($conn->connect_error) {
die("Connection Failed: " . $conn->connect_error);
}
$packagename = (print_r($_SERVER['HTTP_PACKAGENAME'],true));
//$pass = (print_r($_SERVER['HTTP_PASSWORD'],true));
//print_r($_SERVER);
//print_r(apache_request_headers());
$sql = "select * from `package_details` where Package_Name='$packagename' ";
$result = $conn->query($sql);
if($result->num_rows>0) {
//$response["package_details"] = array();
//Output data of each row
while($row = $result->fetch_assoc()) {
//temp user array
$user = array();
$user["id"] = $row["id"];
$user["Package_Name"] = $row["Package_Name"];
$user["Package_Day"] = $row["Package_Day"];
$user["Package_Description"] = $row["Package_Description"];
//push single product into final response array
//array_push($response["package_details"], $user);
}
//success
//$response["success"] = "valid";
//echoing JSON response
echo json_encode($user);
} else {
//no products found
$response["package_details"] = array();
while($row = null) {
$user = array();
$user["id"] = $row[null];
$user["Name"] = $row[null];
$user["Contact_Number"] = $row[null];
$user["Email_Id"] = $row[null];
//push single product into final response array
array_push($response["package_details"], $user);
}
$response["success"] = "invalid";
//$response["success"] = "invalid";
//$response["message"] = "No Products Found";
//echo no users JSON
echo json_encode($response);
}
?>
You were only json encoding the last $user and not the array of users that you should be putting in $response['package_details']
See the amended code below
$response = array();
if($result->num_rows>0) {
while($row = $result->fetch_assoc()) {
$user = array();
$user["id"] = $row["id"];
$user["Package_Name"] = $row["Package_Name"];
$user["Package_Day"] = $row["Package_Day"];
$user["Package_Description"] = $row["Package_Description"];
//push single product into final response array
$response["package_details"][] = $user;
}
//success
$response["success"] = "valid";
} else {
//no products found
$response["success"] = "invalid";
$response["message"] = "No Products Found";
}
echo json_encode($response);

Problems displaying blob images

This is my actual code, i'm starting to using php and i want to know how to display a blob image on my website.
But until now i can't find how to do it. This is my php code...
$query=mysqli_query($link, "SELECT image, titulo, tecnica, medidas FROM upload ");
$numrows = mysqli_num_rows($query);
$posts = array();
if ($numrows != 0)
{
while ($row = mysqli_fetch_assoc($query))
{
$post = new stdClass();
$post->image = '<img src="data:image/jpeg;base64,'.base64_encode($row['image']).'"/>';
$post->titulo = $row['titulo'];
$post->tecnica = $row['tecnica'];
$post->medidas = $row['medidas'];
array_push($posts, $post);
}
}
$jsonData = json_encode($posts);
header("Content-Type: image/jpeg");
echo $jsonData;
Try this:
<?php
//Connect to the database
$databasehost = "YOUR DATABASE HOST";
$databasename = "YOUR DATABASE NAME";
$databaseusername ="YOUR DATABASE USERNAME";
$databasepassword = "YOUR DATABASE PASSWORD";
$con = mysqli_connect($databasehost, $databaseusername, $databasepassword, $databasename) or die(mysqli_error($con));
mysqli_set_charset ($con, "utf8");
$sql = "SELECT image, titulo, tecnica, medidas FROM upload;";
$res = mysqli_query($con, $sql);
$result = array();
while($row = mysqli_fetch_array($res)) {
array_push($result,
array(
'image'=>base64_encode($row[0]),
'titulo'=>$row[1],
'tecnica'=>$row[2],
'medidas'=>$row[3]
));
}
echo json_encode(array($result));
mysqli_close($con);
This will print your data like this:
{[{"image":"IMAGE ENCODED IN BASE 64","titulo":"SOME
DATA","tecnica":"SOME DATA", "medidas":"SOME DATA"},
{"image":"IMAGE ENCODED IN BASE 64","titulo":"SOME
DATA","tecnica":"SOME DATA", "medidas":"SOME DATA"},
{"image":"IMAGE ENCODED IN BASE 64","titulo":"SOME
DATA","tecnica":"SOME DATA", "medidas":"SOME DATA"}]}
Hope it helps! Good Luck! Happy Coding!

Categories