How to fetch all datas and images from mysql tableusing PDO? - php

I'm trying to get a list of news from MySQL but I have some issues with PDO and I can not get. Actually I have titles, messages, photos and categories. Please see the code below and if you can help me. Images are in folder named 'userdatas'
$sth = $conn->prepare("SELECT * FROM news");
$sth->execute();
while($row = $sth->fetch(PDO::FETCH_ASSOC)){
echo $row['id'];
echo $row['title'];
echo $row['category'];
echo $row['message'];
echo '<img src="' . $row['photo'] . '" height="60" width="40"> ';
echo "<br>";
}

The problem with the above code is that you are only specifying the image name. You need something that will specify the full path to the image like this:
$sth = $conn->prepare("SELECT * FROM news");
$sth->execute();
while($row = $sth->fetch(PDO::FETCH_ASSOC)){
echo $row['id'];
echo $row['title'];
echo $row['category'];
echo $row['message'];
echo '<img src="PATH/TO/IMAGE/' . $row['photo'] . '" height="60" width="40"> ';
echo "<br>";
All you need to do is replace PATH/TO/IMAGE with the actual folder path, or change the database to include the full path

Related

Image display from database

I am trying to display an image from database with blob datatype,
I tried all code from all sites and videos but I can't get a result.
$db = new mysqli ("localhost","root","","catchyourbook");
$sql = mysqli_query($db,"SELECT * FROM `diplo computer engg`");
$result = mysqli_fetch_array($sql);
echo '<img src="data:image/png;base64,'.base64_encode( $result['Front page'] ).'"/>';
echo "<tr>";
echo "<td><img src='$row[Front page].png' height="150px" width="300px"></td>";
echo "</tr>\n";

php image type blob wont show

I am trying to show the blop images i have in my mysql database. But all i get is a whitebox with no picture in it.
PHP code:
<?php
require_once "include/config.php";
session_start();
$sql = "SELECT * FROM docenten";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo "<dt><strong>Foto:</strong></dt><dd>" .
'<img src="data:image/jpeg;base64,'.
base64_encode($row['foto']).
'" width="290" height="290">' . "</dd>";
}
}else{
echo "0 result";
}
?>
But when the code runs all i get is this: http://imgur.com/nhIO2LQ
Anyone know a solution?
use addslashes before insert
$img = addslashes(file_get_contents($_FILES['images']['tmp_name']));
$query = "INSERT INTO tableName (id,image) VALUES('','$image')";

How to add bootstrap grid with database result using php

I am newbie in php. i am using the following code to get my desired data from mysql database. but what i want to do, whenever database show a result i want to put it into a bootstrap grid( like col-sm-4) each time. Right now my grid is coded in HTML, how can i generate it with the query result each time ? thanks in advance.
<div class="col-sm-4">
<h3>image </h3><br>
<?php
$sql = "SELECT * FROM sctable";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$link = "http://localhost/sc/uploads/" .$row[1];
echo "<img width='100%' height='200' src=$link />"."<br />";
echo "";
}
?>
</div>
Do you mean something like this where you just want to put each rows data into its own div
<?php
$sql = "SELECT * FROM sctable";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo '<div class="col-sm-4">';
echo '<h3>image </h3><br>';
$link = "http://localhost/sc/uploads/" .$row[1];
echo '<img style="width:100%;height:200px" src="' . $link . '"/>';
echo '</div>';
}
?>
Just for future reference, its a bad idea to use the full url for your own site in code like this. If you move it to a live site this code wont work anymore. Better to use relative paths and let the system do some of the work for you. So it should work whatever the domain is where you move the code.
<?php
$sql = "SELECT * FROM sctable";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo '<div class="col-sm-4">';
echo '<h3>image </h3><br>';
> Changed next line
$link = "sc/uploads/" .$row[1];
echo '<img style="width:100%;height:200px" src="' . $link . '"/>';
echo '</div>';
}
?>
You can just wrap the image:
echo "<div class='col-sm-4'> <img width='100%' height='200' src='$link' /></div>";

php - show sql value with line break on retrieve

i'm using the following code to show clickable pictures, but underneath the picture it shows a description for the picture, in this part:
<br>".$row['optie'];"
but some lines in the table are too long to display the picture and description correctly next to each other.
So how can i show these descriptions and add a line break?
<?php
$query = "SELECT * FROM $tabel";
$result = mysql_query($query);
$aantal = 0;
echo "<table>";
echo "<tr>";
while( ($row = mysql_fetch_array($result)))
{
$src = $row['afbeelding'];
echo '<div class="Image">';
echo "<td align='center'><h2><a href='pagina3.php?lang=" . $_SESSION['lang'] . "&naam=" . $naam . "&postcodehuisnummer=" .$postcodehuisnummer ."&fietskeuze=" . $fietskeuze . "&opties=" . $row['optie'] . "&optieid=" . $row['opties_id'] . "' '><img src=".$src." width='400px'><br>".$row['optie'];"</a><br /><br /> ";
echo '</div>';
$aantal++;
if ($aantal==3) {echo "<tr>"; $aantal=0;}
}
echo "</tr></tr>";
echo "</table>";
?>
You should simply concatenate your value onto the end of your URL.
$row['optie'] = str_replace('\r', '<br>', $row['optie']);

Php image doesn't show

Why the following php script doesn't show the image. I checked that database connection is OK and Mysql Query is also Ok.
Php Code:
$upload_directory = "uploaded"; //set upload directory
$getimages = mysql_query("SELECT * FROM property_step3 WHERE propertyid =
'$propertyid' AND active =1 AND uname = '$uname'");
$re2 = mysql_fetch_array($getimages)
$images = mysql_real_escape_string(htmlspecialchars(trim($re2['imgname'])));
echo '<img src="$upload_directory/$images" width="98" height="68" /></a>';
May be I'm wrong..
I don't understand why are some people are voting me DOWN ??!!
Update:
<?php
$uname = $_SESSION['uname'];
$check = mysql_query("SELECT * FROM property_step1 WHERE uname = '$uname' AND active
= 1");
$num = mysql_num_rows($check);
if($num == 0)
{
echo "<h3>You didn't upload any property. Click <a href='publishadvert.php'>here</a>
to publish your property</h3>";
}
else
{
echo "<h3>You have $num published property.</h3>";
echo "<table width='1020' cellpadding='0' cellspacing='0'>";
echo "<tr>";
echo "<td class='tabletr3'><b>Property Title</b></td>";
echo "<td class='tabletr3'><b>Area</b></td>";
echo "<td class='tabletr3'><b>State</b></td>";
echo "<td class='tabletr3'><b>City</b></td>";
echo "<td class='tabletr3'><b>Country</b></td>";
echo "<td class='tabletr3'><b>Images</b></td>";
echo "<td class='tabletr3'><b>Action</b></td>";
echo "</tr>";
while($re = mysql_fetch_array($check))
{
$propertyid = (int) $re['propertyid'];
$country = mysql_real_escape_string(htmlspecialchars(trim($re['pro_country'])));
$state = mysql_real_escape_string(htmlspecialchars(trim($re['pro_state'])));
$area = mysql_real_escape_string(htmlspecialchars(trim($re['pro_area'])));
$city = mysql_real_escape_string(htmlspecialchars(trim($re['pro_city'])));
$title = mysql_real_escape_string(htmlspecialchars(trim($re['pro_title'])));
$getimages = mysql_query("SELECT * FROM property_step3 WHERE propertyid =
'$propertyid' AND active =1 AND uname = '$uname'");
$upload_directory = dirname(__file__) . '/uploaded/'; //set upload directory
while($re2 = mysql_fetch_array($getimages))
{
$images = mysql_real_escape_string(htmlspecialchars(trim($re2['imgname'])));
}
echo "<tr>";
echo "<td class='tabletr2'>$title</td>";
echo "<td class='tabletr2'>$country</td>";
echo "<td class='tabletr2'>$state</td>";
echo "<td class='tabletr2'>$city</td>";
echo "<td class='tabletr2'>$area</td>";
echo '<img src="'.$upload_directory.'/'.$images.'" width="98"
height="68" /></a>';
echo "<td class='tabletr2'><img src='uploaded/$images'
/></td>";
echo "<td class='tabletr2'><a href='editproperty.php?propertyid=$propertyid&
uname=$uname'>Edit</a> | <a href='deleteproperty.php?propertyid=$propertyid'>Delete</a>
</td>";
echo "</tr>";
}
echo "</table>";
}
?>
Mistake 1: What Waqar Alamgir said, but more explicitly. Trying to use variables in single quotes.
echo '<img src="$upload_directory/$images" width="98" height="68" /></a>';
will output
<img src="$upload_directory/$images" width="98" height="68" /></a>
whereas
echo "<img src=\"$upload_directory/$images\" width=\"98\" height=\"68\" /></a>";
or
echo '<img src="'. $upload_directory . '/' . $images .'" width="98" height="68" /></a>';
or
echo '<img src="', $upload_directory, '/', $images, '" width="98" height="68" /></a>';
will output
<img src="uploaded/theimagename.jpg" width="98" height="68" /></a>
where $images equals theimagename.jpg
Mistake 2: (Spotted by Pankaj Khairnar)
$re2 = mysql_fetch_array($getimages)
is missing a semi-colon. This will break your script.
$re2 = mysql_fetch_array($getimages);
Mistake 3 (c/o Michael Berkowski)
Don't call mysql_real_escape_string() on output data
Problem with quotes you can not use varaibles in single '
$upload_directory = "uploaded";
$getimages = mysql_query("SELECT imgname FROM property_step3 WHERE propertyid =
'$propertyid' AND active =1 AND uname = '$uname'");
$re2 = mysql_fetch_assoc($getimages)
$images = $re2['imgname'];
echo 'Debug image: ',$images;
echo '<img src="',$upload_directory,'/',$images,'" width="98" height="68" />';
use like this
$upload_directory = "uploaded"; //set upload directory
$getimages = mysql_query("SELECT * FROM property_step3 WHERE propertyid =
'$propertyid' AND active =1 AND uname = '$uname'");
$re2 = mysql_fetch_array($getimages);
$images = trim($re2['imgname']);
echo '<img src="'.$upload_directory.'/' .$images. '" width="98" height="68" /></a>';
also ; at the end is missing on your statement
$re2 = mysql_fetch_array($getimages)
it should be like this
$re2 = mysql_fetch_array($getimages);
Maybe you need the full url to make the image shows, like adding
http://localhost/.../uploaded/50b11aefaad43font-8.jpg
I am not sure the directory hierarchy on your machine.
I assume that the 'uploaded' folder is inside your htdocs root.

Categories