How to display BLOB content in php - php

Iv try to display BLOB-1B content or image in PHP, I have an app in Android that take an image then upload to my host mysql I have no problem with upload now its time to display in PHP, here is my code.
//config.php
public function _owners_img($stall_id){
$request ="SELECT `img` FROM `img_stall` WHERE `stall_id` = '$stall_id' LIMIT 1";
$result_query = $this->con->query($request);
while($data = $result_query->fetch_assoc()){
$result_data = $data['img'];
}
if (!empty($result_data)) {
return $result_data;
}else {
return false; }
}
//owner_img.php
include_once 'config.php';
$db = new database($con);
$db->dbconnect();
$owner = 3;
echo "<dt><strong>Stall Image:</strong></dt><dd>" .
'<img src="data:image/jpeg;base64,'.
base64_encode($db->_owners_img($owner)).
'" width="290" height="290">' . "</dd>";
but the problem is Am only getting small image icon or image not found. how do I fix this?

Related

PHP - Display different image based on mysql row result

Sorry for my English.
I'm learning PHP and I try to create a small member area, I think it's a good way to learn.
In my member area, some members are "verified" and some are not.
How can I display different pics based on data stored in Mysql using PHP?
What I want is to display "Picture1" if "1" is the value stored in a MySQL column and "Picture2" if "2" is the value, etc... You are "unverified member" so you see picture 1, verified member see picture 2...
I know how to SELECT data using MySQLi but I can't find what I have to do next?
Thank you.
Lets assume that you have a database named as db and table as tb with columns name and verify. Name is string and verify is Boolean. Also verify stored 1 if user is verified and 0 it isn't.
so, you can do it by iteration statements (either by using if else or by switch statements)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query="SELECT * FROM `tb`";
$sql=$conn->query($query);
if($sql->num_rows>0)
{
while($row=$sql->fetch_assoc())
{
//display name
echo $row['name'];
//check if user is verified or not
//using if else statement
if($row['verify']=="0")
{
echo '<img src="./picture1.jpg">';
}
else
{
echo '<img src="./picture2.jpg">';
}
//using switch case
switch($row['verify'])
{
case(0):
{
echo '<img src="./picture1.jpg">';
}
case(1);
{
echo '<img src="./picture2.jpg">';
}
}
//remember i have used both methods hence it will show the image twice , i did it just you possible ways , you choose any one from it.(if else or switch case)
}
}
$conn->close();
?>
There's a few ways. If it will always follow a number system... use this:
$picture = "Picture" . $verified;
// example: $picture = "Picture1", if $verified = 1
Lots of ways to concatenate...
$pic = "Picture$verified.png";
$picture = "Picture" . $verified . ".jpeg";
It will just depends on how you have it setup..
switch($verified){
case 1:
$picture = "Picture1";
break;
case 2:
$picture = "Picture2";
break;
case default:
$picture = "Picture1";
break;
}
Another way...
if($verified == 1){ $picture = "Picture1"; } else { $picture = "Picture2"; }
The reason for the switch statement, or this if/else statement is that it can account for verified being equal to 0, null, or another non 1 or 2 value that may be stored in the database for whatever reason. With the first example, if $verified (verified column = to 1 or 2) has an odd value, the picture# may not actually be real.
Now, of course this is just to handle the different ways. You would have to make sure it lines up properly with your actual image...
$picture = "Picture1"; // would be dynamic to code above
$ext = ".png"; // optional, could be added into the $picture variable above easily too
$filePath = "/images/user_uploads/"; // make sure path is correct
$filePathName = $filePath . $picture . $ext;
// see? We combine path/pictureName/extension to create an image URL
if(file_exists($filePathName) !== false){ // make sure exists
$imageCode = "<img src='$filePathName' alt='Not Found'>";
} else {
// file does not exist! ut oh!
}
It depends on how your images map to the values in your database, but assuming you have a simple relationship like:
1: /images/1.jpg
2: /images/2.jpg
Simply grab the data from your database and map it to a variable (in my example it's $image), and then concatenate it with the filepath inside an echo statement inside of an <img src> attribute:
<img src="<?php echo '/images/' $image . '.jpg'; ?>"/>
This will output:
<img src="/images/1.jpg"/>
<img src="/images/2.jpg"/>
Based on the chosen image.
Now you just have to set that variable to the right value based on your conditional:
<?php
if ($authenticated) {
$image = 1;
}
else {
$image = 2;
}
?>
<img src="<?php echo '/images/' $image . '.jpg'; ?>"/>
Or more succinctly with a ternary:
<?php ($authenticated ? $image = 1 : $image = 2) ?>
<img src="<?php echo '/images/' $image . '.jpg'; ?>"/>
Try this:
<img src="<?php echo "/pictures/Picture{$number}.jpg"; ?>" />

fetch multiple rows from mysql with one query and use them in php

Hi there i got stuck on this code and i don't seem to find a solution to it.
I have this now...
pdf download
and i want to display this...
enter image description here
all the PDFs are in a table and one product have 2-3 PDFs
here is my code:
enter image description here
and the table looks like this...
enter image description here
Sorry for not placing the code:
here is the code i speak and the images is for a visual aspect of what i want the code to do...
<?php
$data_sheet = false;
$datasheet_query = $db->Execute("SELECT TECHLINK_ID, TECHLINK_INCODE, TECHLINK_URLID, TECHLINK_products_id, TECH_URL, TECH_TITLE
FROM FTECHLINK
LEFT JOIN FTECHSHEET_URL ON TECH_ID = TECHLINK_URLID
WHERE TECHLINK_products_id = ".(int)$_GET['products_id']."");
if (!$datasheet_query->EOF){
$data_sheet = true;
$pdf_title = $datasheet_query->fields['TECH_TITLE'];
$pdf_file = $datasheet_query->fields['TECH_URL'];
$pdf_path = "acrobat/".$pdf_file;
$image_name = str_ireplace('.pdf','.jpg',$pdf_file);
$image_path = "acrobat/".$image_name;
$tech_image = '<div class="download_image_container">' . zen_image($image_path, $products_name, "173", "245", 'class="box_image_pdf"') . '</div>';
$tech_title = '<div class="download_title_container"><a class="document_title"href="' . $pdf_path . '"target="_blank">' . $pdf_title . '</a></div>';
}
?>
<?php if ($data_sheet == true) { ?>
<div id="datasheet" class="tab-pane">
<div class="product-tab">
<?php
echo $tech_image;
echo $tech_title;
?>
</div>
</div>

upload canvas drawing to mysql db using php as an image

Hy guys. I want to take a drawing from a canvas instead of saving it as an image like this https://github.com/eligrey/FileSaver.js .I want to upload it to my local server mysql using php.
please help
<?php
$conn = new mysqli("localhost","root","","userdetails");
$results = $conn->query("SELECT * FROM userInfo");
while($row = $results->fetch_assoc()){
echo "Name :".$row['uname'].
"<br/>Surname : ".$row['surname'].
"<br/> Image : <img width='200' height='100' src='images/".$row['userImage']."' />";
}
#$uname = $_POST['uname'];
#$surname = $_POST['surname'];
#$image = $_POST['img'];
if(isset($uname)){
$conn->query("INSERT INTO userInfo values('','$uname','$surname','$image')");
move_uploaded_file($image, '../images/'.basename($image));
}
?>

Unable to get the image from mysql DB

I'm new to php. I have a sample mysql db in that I have a table named testdb with columns id(INT) and image(BLOB). I have uploaded an image into testdb. Uploaded successfully. The following is the php code. The variable $conn contains the connection details. I have a html page which redirects to this php page on submitting.
<?php
$name = $_FILES["sample"]["name"];
echo $name . "<br/>";
$tmp_name = $_FILES["sample"]["tmp_name"];
echo $tmp_name . "<br/>";
$size = $_FILES["sample"]["size"];
echo $size . "<br/>";
$contents = file_get_contents($tmp_name);
$htmlen = htmlentities($contents);
$cont = mysql_real_escape_string($contents);
$query = "INSERT INTO testdb(image)
VALUES ('$cont')";
$dbquery = mysql_query($query, $conn);
if($dbquery){
echo "successfully inserted";
}
else{
echo "could not inserted" . mysql_error();
}
?>
I am trying to get the image with the following code. But it is showing string characters rather than the image. As far as I know this should work fine.
<?php
$query = "SELECT image, id
FROM testdb ";
$dbquery=mysql_query($query , $conn);
if(! $dbquery){
echo "Could not selected the data from database. " . mysql_error();
}
while( $row = mysql_fetch_array($dbquery) ){
$decodeimg = html_entity_decode($row["image"]);
echo "<img src= $decodeimg/><br/> hellow orld <br/>";
}
?>
Could anyone help me with this. Thanks in advance.
Instead of storing the actual image in your database (which is redundant because it is probably stored on your server too); why don't you just store the PATH to the image as a string, query the string from your db and then append it to the 'src' attribute with php.
I am also got the same error when show the BLOB image from DB. I just use the decoding method for this problem....
$photo=$myrow['image'];
echo '<img src="data:image/jpeg;base64,' . base64_encode( $photo ) . '" width="150" height="150" />

images from database via php can't display

thank's for help. I have problem displaying images retrieving from my database.
I cant see the image when loading image.php in img src or directly from the page. When i display the variable without header('Content-type: image/jpeg'); i can see all the code inside, as i put this line all goes off.
I have a table called TABLE with id, title, img stored as longblob directly uploaded inside phpmyadmin.
Can anyone help me?
index.php
<?php
session_start();
include "admin/include/connection2.php";
$data = new MysqlClass();
$data->connect();
$query_img ="SELECT * FROM table ORDER BY data ASC LIMIT 4";
$post_sql = $data->query($query_img);
if(mysql_num_rows($post_sql) > 0){
while($post_obj = $data->estrai($post_sql)){
$id = $post_obj->id;
$titolo = stripslashes($post_obj->title);
$data_articolo = $post_obj->data;
$immagine = $post_obj->img;
// visualizzazione dei dati
echo "<h2>".$titolo."</h2>";
echo "Autore <b>". $autore . "</b>";
echo "<br />";
echo '<'.'img src="image.php?id='.$post_sql['id'].'">';
echo $id;
echo "<hr>";
}
}else{
echo "no post aviable.";
}
// here is the image.php code
<?php
include "admin/include/connection2.php";
$data = new MysqlClass();
// connect
$data->connetti();
$id = $_GET['id'];
echo $id;
$query = mysql_query("SELECT * FROM articoli_news WHERE id='".$id."'"; //even tried to send id='1' but not working
echo $query;
$row = mysql_fetch_array($query);
echo $row['id']; //correct displaying
$content = base64_decode($query['img']);
header('Content-type: image/jpeg');
echo $content;
?>
Delete all "echo" commands except "echo $content;" because there are also appear in the output, and damage your image.
And use ob_start(); in the begining of the script, and check out your script file not contain any of whitespace characters before or after the php begint and close tags .

Categories