Problems displaying blob images - php

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!

Related

React Native fetch data in MYSQL database

I'm using React Native and extracting data from MYSQL. But I can't get the direct result of my data. So the output that is now: "[{" STT_TIP ":" Taxi "}]", I would say that the output: "Taxi" get. So just give me the result. He's drawing his name in the painting I'm taking now. I just want to draw the result.
<?php
include 'DBConfig.php';
$conn = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
mysqli_query($conn, "SET CHARACTER SET 'utf8'");
mysqli_query($conn, "SET SESSION collation_connection ='utf8_turkish_ci'");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$arr = array();
$json = file_get_contents('php://input');
$obj = json_decode($json, true);
$email = $obj['email'];
$password = $obj['password'];
$Sql_Query = "select STT_TIP from ...... where .. = '$email' and . = '$password' ";
$result = $conn->query($Sql_Query);
if ($result->num_rows > 0) {
while ($row[] = $result->fetch_assoc()) {
$tem = $row;
$json = json_encode($tem);
}
} else {
}
echo $json;
$conn->close();
?>
Since you said this only returnn 1 result. Don't bother with the loop
use
$row = $result->fetch_assoc(); // same as mysqli_fetch_assoc($result)
and
$json = json_encode($row['STT_TIP');
Will return the same as plain text(no brace, nor object)
Be carefull, your code is vulnerbale to SQL injection.

How to display the database name in json

Am fetching values from mysql to json. All my data's are showing properly now i want to display database name inside json. any help can be appreciated.
json.php code :
<?php
$connect = mysqli_connect("localhost", "root", "", "recruiter");
$sql = "SELECT * FROM recruiter";
$result = mysqli_query($connect, $sql);
$json_array = array();
while($row = mysqli_fetch_assoc($result))
{
$json_array[] = $row;
}
echo (json_encode($json_array));
?>
Output:
{
"here i want db name":[
{
"job_id":"1",
"job_title":"Java developer",
"job_description":"Java description",
"job_details":"details",
"job_skills":"java",
"job_min_exp":"0 Yr",
"job_max_exp":"2 Yrs",
"company_name":"",
"job_location":"",
"industry":"",
"department":"",
"job_role":"",
"owner_name":"",
"owner_mobile":"",
"owner_email":"",
"owner_description":" "
}
]
}
As per your output, Understand is database name is array under the multidimensional array. To show database name inside the JSON.
Try this code
<?php
$database_name = "recruiter";
$connect = mysqli_connect("localhost", "root", "", $database_name);
$sql = "SELECT * FROM recruiter";
$result = mysqli_query($connect, $sql);
$json_array = array();
while($row = mysqli_fetch_assoc($result))
{
$json_array[] = $row;
}
echo json_encode(array($database_name => array($json_array)));
?>

JSON encoding not working

Here is my code to encode data in JSON format, but it doesn't work. The result is []. Where is my mistake?
<?php
$conn = new mysqli('localhost','root','','project');
$data =array();
if(!empty($_GET['masp'])){
$masp =$_GET['masp'];
$sql ="SELECT *FROM sanpham WHERE masp='$masp'";
$result = mysqli_query($conn,$sql);
if($result){
while($r = mysqli_fetch_assoc($result)){
$r['masp'] =$data['masp'];
$r['loai'] =$data['loai'];
$r['hangsx']=$data['hangsx'];
$r['tensp']=$data['tensp'];
$r['img']=$data['img'];
$r['gia']=$data['gia'];
$r['nx']=$data['nx'];
}
}
}
print json_encode($data);
?>
You are setting your variables wrong.
In every while cycle you get a new $r variable that you want to add to your $data variable.
$conn = new mysqli('localhost', 'root', '', 'project');
$data = array();
if (!empty($_GET['masp'])) {
$masp = $_GET['masp'];
$sql = "SELECT *FROM sanpham WHERE masp='$masp'";
$result = mysqli_query($conn, $sql);
$i = 0;
if ($result) {
while ($r = mysqli_fetch_assoc($result)) {
$data[$i]['masp'] = $r['masp'];
$data[$i]['loai'] = $r['loai'];
$data[$i]['hangsx'] = $r['hangsx']];
$data[$i]['tensp'] = $r['tensp'];
$data[$i]['img'] = $r['img'];
$data[$i]['gia'] = $r['gia'];
$data[$i]['nx'] = $r['nx'];
$i += 1;
}
}
}
print json_encode($data);
You make mistake. You should swap variable data with r inner loop, but probably than also will works unpropely. write in while loop $data [] = $r;

read "/" without being changed to "\/" in 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

Fetching single data returns error

I'm trying to fetch couple of single data in my server database but this is throwing some errors. The incoming data is correct. The search function just don't get completed.
Here's the code:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
define('HOST','xxxxxxxxxxx');
define('USER','xxxxxxxxxxxx');
define('PASS','xxxxxxxxx');
define('DB','xxxxxxxxxx');
$con = mysqli_connect(HOST,USER,PASS,DB);
$post_id = $_POST['id'];
$buyer_mobile = $_POST['mobile'];
$buyer_name = $_POST['name'];
$sql = "select mobile from flatowner where id='$post_id'";
$res = mysqli_query($con,$sql);
$owner_mobile = $row['mobile'];
$sql = "select name from user where mobile='$owner_mobile'";
$r = mysqli_query($con,$sql);
$owner_name = $row['name'];
$sql = "INSERT INTO flat_booking (post_id,owner_mobile,owner_name,buyer_mobile,buyer_name) VALUES ('$post_id','$owner_mobile','$owner_name','$buyer_mobile','$buyer_name')";
if(mysqli_query($con,$sql)){
echo "Success";
}
else{
echo "error";
}
mysqli_close($con);
}else{
echo 'error1';
}
What am I doing wrong here? Maybe this:
$owner_mobile = $row['mobile'];
Thanks in advance!
create table flatower and add mobile column
$post_id = 1;
$sql = "select mobile from flatowner where id='$post_id'";
$res = mysql_query($con,$sql);
$row = mysql_fetch_array($res);
$owner_mobile = $row[0]['mobile'];
Your problem is this line:
$owner_mobile = $row['mobile'];
You have not created the $row variable. For this you would need to do something such as:
Do this first:
<?php
$row = array();
while ($result = mysqli_fetch_assoc($res))
{
$row[] = $result;
}
?>
This allows you to do this:
<?php
foreach ($row as $r)
{
var_dump($r); print "<br />"; // One row from the DB per var dump
}
?>

Categories