Php image doesn't show - php

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.

Related

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

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

How can I retrieve images from MYSQL- Encrypted characters error

i'm working with uploading and retrieving images from database using php and mysql.I have tried the following codes below but it returns lot of encrypted characters. The data type of the image in mysql is longblob.
while ($getRows = mysql_fetch_array($iQuer)){
$yyy = $getRows['user_id'];
$get = mysql_query("SELECT * FROM users where id ='".$yyy."'");
$divider = mysql_fetch_array($get);
$getName = $divider['user_name'];
$getImage = $divider['profile_pic'];
echo "<tr>";
echo "<td><div id = 'postsdiv'>" . "<img src = ".$getImage."/> <strong>". $getName ."</strong><br>". $getRows['post'] . "</div><br>";
echo "</tr>"; }
I also tried another syntax but it returns nothing:
while ($getRows = mysql_fetch_array($iQuer)){
$yyy = $getRows['user_id'];
$get = mysql_query("SELECT * FROM users where id ='".$yyy."'");
$divider = mysql_fetch_array($get);
$getName = $divider['user_name'];
$getImage = $divider['profile_pic'];
echo "<tr>";
echo "<td> <img src='data:image/png;base64,'" . base64_encode($getImage) . " />'"
echo "<td><div id = 'postsdiv'>" . "<strong>". $getName ."</strong><br>". $getRows['post'] . "</div><br>";
echo "</tr>";
}
Can you try it?
$getImage = $divider['profile_pic'];
header("content-type:image/jpeg"); // Can you add this line
echo $getImage;

Echo first result from MYSQL differently than all the other results

I have a mysql table in which contains video titles, video embed html, video description, and video thumbnails. I want it to output the first entry as;
$result = mysqli_query($con,"SELECT * FROM entries");
while($row = mysqli_fetch_array($result)) {
echo $row['title'];
echo $row['html'];
echo $row['desc'];
}
and after that, I would like it to output the next five entries as
$result = mysqli_query($con,"SELECT * FROM entries");
while($row = mysqli_fetch_array($result)) {
echo $row['title'];
echo "<a href='". $row['id'] . "'><img src='" . $row['thu'] . "'></a>";
}
however I have no idea how to do this. I am kind of a newb when it comes to mysql. Any help would be appreciated.
I have looked at other similar questions but none of them really fit the bill.
Poor man's solution would be something like:
$result = mysqli_query($con,"SELECT * FROM entries");
$first = true;
while($row = mysqli_fetch_array($result)) {
if ($first) {
echo $row['title'], $row['html'], $row['desc'];
$first = false;
continue 2;
}
echo $row['title'], "<a href='{$row[id]}'><img src='{$row[thu]}'></a>";
}
(As explained in PHP chatroom)
Since mysqli_fetch_array() gets you a row, you can use it before the while() without a problem like this
$result = mysqli_query($con,"SELECT * FROM entries");
$row = mysqli_fetch_array($result);
echo $row['title'];
//etc.
while($row = mysqli_fetch_array($result)) {
echo $row['title'];
echo "<a href='". $row['id'] . "'><img src='" . $row['thu'] . "'></a>";
}
Track it with a sentinel value
$i = 0;
while($row = mysqli_fetch_array($result)) {
if($i == 0){
//first iteration
echo $row['title'];
echo $row['html'];
echo $row['desc'];
}else{
echo $row['title'];
echo "<a href='". $row['id'] . "'><img src='" . $row['thu'] . "'></a>";
}
$i++;//increment value
}

php mysql only returning first record

I have this following code in a switch statement since the query can vary based on screen-name(1 word), full name (2 words...maybe 3), or the email address which "explode"'s to 3 words (I put it back together later, no biggy). However, cannot locate the problem in the following code:
$query = "SELECT * FROM personal WHERE FirstName='$searchName' OR LastName='$searchName' OR Email='$searchName' OR displayName='$searchName'";
$result = mysql_query($query) or die(mysql_error());
echo "<center><table border='1' width='70%'>";
if (mysql_num_rows($result)) {
$row = mysql_fetch_assoc($result);
$myBuddy = $row['FirstName'].
" ".$row['LastName'];
$picName = $row['FirstName'].$row['LastName'].$row['RecNum'];
if (file_exists('../members/'.$picName.
'/profile.jpg')) {
$picLine = "<img src = '../members/".$picName.
"/profile.jpg' alt='profile' height='100' width='100' />";
}
echo "<tr>";
echo "<td style='text-align:center'>".$picLine.
"</td>";
echo "<td style='text-align:center; color:red'>".$myBuddy.
"</td>";
echo "<td style='text-align:center'><input type='button' value='Add Buddy'></td>";
}
echo "</table>";
break;
Problem is, I have several people in my MySql database with the same last name (Jones), however in the code above only returns the first occurance of someone with the last name Jones. Trying to return them all, not just one. I know I am just overlooking something small & stupid - been doing php/mysql for 2 years now - never had to do a search page before. Any help is greatly appreciated.
while ($row = mysql_fetch_assoc($result)) {
//...
//use $row
}
instead of
$row = mysql_fetch_assoc($result);
You have too loop the results, like in the following example:
echo "<center><table border='1' width='70%'>";
if (mysql_num_rows($result) )
{
while($row = mysql_fetch_assoc($result)) {
$myBuddy = $row['FirstName'] . " " . $row['LastName'];
$picName=$row['FirstName'].$row['LastName'].$row['RecNum'];
if (file_exists('../members/' . $picName . '/profile.jpg'))
{
$picLine = "<img src = '../members/" . $picName .
"/profile.jpg' alt='profile' height='100' width='100' />";
}
echo "<tr>";
echo "<td style='text-align:center'>" . $picLine . "</td>";
echo "<td style='text-align:center; color:red'>" . $myBuddy . "</td>";
echo "<td style='text-align:center'><input type='button' value='Add Buddy'></td>";
}
echo "</table>";
break;
}
Above, I have used a while loop.
while($row = mysql_fetch_assoc($result)) {
// code
}

Troubleshooting "Notice: Undefined index" error

could anyone check my code why i get a notice like this?
Notice: Undefined index: id in C:\xampp\htdocs\HRPO\module\reports\jo\view_jo.php on line 76
line 76:
$id=$_GET['id'];
here is the first could of which I get my id:
<?php
echo "<dl>";
echo "<dt width = 200 id=\"label\">"."SSA"."</dt>";
echo "<dd align='right'>";
$result = mysql_query("SELECT ssa.first_name,ssa.SSA_ID
FROM staffing_specialist_asst ssa
left join jo_partner jp on jp.SSA_ID = ssa.SSA_ID
group by first_name") or die(mysql_error());
$dropdown = "<select name=\"SSA_ID\" style=\"position:relative; left:-51px;\">\n";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "\r\n<option value='{$row['SSA_ID']}'>{$row['first_name']}</option>";
}
$dropdown .= "\r\n</select>";
echo $dropdown;
echo "</dd>";
echo "</dl>";
?>
and the second code where line 76 is found:
<?php
$id=$_GET['id'];
if(isset($_POST['submit']))
{
$datefrom = $_POST['timestamp'];
$dateto = $_POST['timestamp1'];
//echo $option;
$_SESSION['datefrom'] = $datefrom;
$_SESSION['dateto'] = $dateto;
if(( $datefrom == NULL) || ($dateto == NULL)){
echo "<SCRIPT LANGUAGE='javascript'> confirmationError() ;</SCRIPT>";
exit();
}
$final =("SELECT distinct jp.receivedDate as rDate, ssa.first_name as saFName, ssa.last_name as saLName,job.client_order_number as joNum,
job.job_order_type as joType, job.job_title as joTitle, cl.name as clientName
,ss.first_name as ssFName,ss.last_name as ssLName,jp.acknowledgeDate as aDate, stat.status as stat
FROM staffing_specialist_asst ssa
left join jo_partner jp on ssa.SSA_ID = jp.SSA_ID
left join job_order job on jp.job_order_number = job.job_order_number
left join jo_status stat on job.job_order_number = stat.job_order_number
left join staffing_specialist ss on jp.SS_ID = ss.SS_ID
left join client cl on job.client_ID = cl.client_ID
where jp.receivedDate between '$datefrom1' and '$dateto1'
and ssa.SSA_ID='$id'
group by jp.receivedDate
order by jp.receivedDate asc");
echo $final;
$query = mysql_query($final);
echo "<table>";
while($row = mysql_fetch_array($query))
{
$rDate = $row['rDate'];
$saFName = $row['saFName'];
$saLName = $row['saLName'];
$joNum = $row['joNum'];
$joType = $row['joType'];
$joTitle = $row['joTitle'];
$clientName = $row['clientName'];
$ssFName = $row['ssFName'];
$ssLName = $row['ssLName'];
$aDate = $row['aDate'];
$stat = $row['stat'];
echo "<tr>";
echo "<td width='150' colspan=\"1\" align=\"center\">".$rDate."</td>";
echo "<td width='150' colspan=\"1\" align=\"center\">".$saFName."".$saLName."</td>";
echo "<td width='150' colspan=\"1\" align=\"center\">".$joNum."</td>";
echo "<td width='150' colspan=\"1\" align=\"center\">".$joType."</td>";
echo "<td width='150' colspan=\"1\" align=\"center\">".$joTitle."</td>";
echo "<td width='150' colspan=\"1\" align=\"center\">".$clientName."</td>";
echo "<td width='150' colspan=\"1\" align=\"center\">".$ssFName."".$ssLName."</td>";
echo "<td width='150' colspan=\"1\" align=\"center\">".$aDate."</td>";
echo "<td width='150' colspan=\"1\" align=\"center\">".$stat."</td>";
echo "</tr>";
}
echo "</table>";
}
?>
Thanks in advance for all the suggestions and help.
$_GET['id'] expected the get variable 'id' in the url (i.e. www.google.com?id=4 then $_GET['id'] would equal 4).
In order to avoid this you could do this before checking the get value if (! empty($_GET)) {$id = $_GET['id']}
EDIT: The actual error ended up being the assumption of needing to use $_GET instead of the possibility of $_POST for form data.
Looks like the select box is SSA_ID but you are using a $_GET['id'], try changing it to $_GET['SSA_ID'];

Categories