Having a problem uploading a picture from a database, wondering if i'm doing something wrong..
I've checked that the image is there by using <img scr='/upload/imgpath.jpg'>
Any suggestions?
$sql1 = "SELECT pic, stocknr, status FROM stock WHERE status = '1'";
$result = mysqli_query($con,$sql1);
while($row1 = mysqli_fetch_assoc($result));
{
$stocknr2 = $row1['stocknr'];
$picname = $row1['pic'];
$upfile = $picname;
echo "<center>";
echo "<a href='details.php?stocknr=$stocknr2'>";
echo "<img src='/upload/$upfile' border=1 width=350 height=280></a>";
echo "</center>";
}
mysqli_free_result($result);
You just need to change while row;
while($row1 = mysqli_fetch_assoc($result)
Related
I have this code has been working for few months without any problem. Now this page does not work and the problem from php code but I dont know where
<?php
$query = "SELECT * FROM offerimage order by name";
$result = mysqli_query($connection, $query);
if ($result->num_rows > 0) {
echo "<table>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
$image = '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'" height="100" width="100"/>';
//echo '<br></br>';
echo $image;
echo '<br></br>';
echo "</tr>";
}
} else {
echo "</table>";
}
?>
I GET THIS ERROR
"ERR_EMPTY_RESPONSE"
Why don't you output something when the query returns no rows, since that's probably the cause?
Also you should have the images inside a <td> element.
Edited to show row-by-row retrieval of images. Assumes the database table offerimage has a unique integer ID (like an AUTO_INCREMENT column) named id, change queries to match the actual table definition.
<?php
// get array of offerimage IDs ordered by name
$query = "SELECT id FROM offerimage order by name";
$result = mysqli_query($connection, $query);
$ids = [];
while ($row = $result->fetch_assoc()) {
$ids[] = (int)$row['id'];
}
if (count($ids) > 0) {
// display each image
echo '<table>';
foreach ($ids as $id) {
$query = "SELECT image FROM offerimage WHERE id = {$id}";
$result = mysqli_query($connection, $query);
$row = $result->fetch_assoc();
echo "<tr><td>";
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image'] ).'" height="100" width="100"/>';
echo "</td></tr>";
mysqli_free_result($result);
}
echo "</table>";
} else {
echo "<p>No offers found.</p>";
}
Other suggestions: Don't use SELECT * if you only need one column. Use SELECT image instead.
Check for errors when making the database connection (you don't show that code) and from the query, use error_log() or send to the browser so you can see if something went wrong there.
I solve it by save images in folder and then save image location in DB.
Code will not display topics from database. I just get a blank pages.
Any solutions?
The pages loads but it will not display any thing. They want me to add more context but it breaks it so here you go.
<?php
//Database stuff.
include_once("connect.php");
if ($conn->connect_error) {
trigger_error('Database connection failed: ' . $conn->connect_error, E_USER_ERROR);
}else{
mysqli_select_db($conn,"2159928_db");
}
$tid = '';
$cid = $_GET['cid'];
$tid = $_GET['tid'];
$sql = "SELECT * FROM topics WHERE category_id='".$cid."' AND id='".$tid."' LIMIT 1";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) == 1){
echo "<table width ='75%'>";
if (isset($_SESSION['uid'])) {
//echo "<form action ='post_reply.php?cid=".$cid." &tid =".$tid. "' method = 'post'>
//<input type = 'submit' name = 'submit' value = 'Reply'/>";
//echo "<tr><td colspan ='2'><center><input type='submit' value='Reply' onClick = 'window.open = 'post_reply.php?cid=".$cid." &tid =".$tid."' />";
echo "<tr><td colspan ='2'><center><input type='submit' value='Reply' onClick='window.open(\"post_reply.php?cid=$cid&tid=$tid\")' />";
} else {
echo "<tr><td colspan = '2'><p><center> Please login to reply to topics.</p></td></tr>";
}
//Trying to display this. Doesn't even display border box.
while ($row = mysqli_fetch_assoc($result)) {
$sql2 = "SELECT * FROM posts WHERE category_id='".$cid."' AND topic_id='".$tid. "'";
$result2 = mysqli_query($conn, $sql2);
while ($row2 = mysqli_fetch_assoc($result2)){
echo "<tr><td valign ='top' style = 'border: 5px solid #ffffff;'><div style = 'min-height: 125px;'>".$row['topic_title']."<br/>
by ".$row2['post_username']. " - " .$row2['post_date']. "<hr/>".$row2['post_content']."</div></td>";
}
//This part not relevant.
$old_views = $row['topic_views'];
$new_views = $old_views + 1;
$sql3 = "UPDATE topics SET topic_views='".$new_views."' WHERE category_id='".$cid."' AND id ='".$tid."' LIMIT 1";
$result3 = mysqli_query($conn, $sql3);
}
echo "</table>";
} else {
echo "<p>This topic does not exist.</p>";
}
?>
Try echo-ing out your $sql to see if the query is correct.
If query is correct. Try var_dump to see if there are any results.
Please check below code and help me out if anything is going wrong in image display code
Untitled Document
<body>
<p>how to display image</p>
<p>image is not seeingd</p>
<?php
$con= mysql_connect("localhost","root","");
$d= mysql_select_db("matrimonial",$con);
$id=$_REQUEST['id'];
$sql = "select * from advertiesment where S_NO ='1'" or die(mysql_error());
$result = mysql_query($sql,$con);
if($rows=mysql_fetch_assoc($result))
{
$image = $rows['filepath'];
echo "<img style='width:150px;height:150px' src='$image'>";
echo "<br>";
}
?><p>how to display image</p>
</body>
</html>
if($rows=mysql_fetch_assoc($result))
The condition is bad. If you have more results from SQL query,you need to use while.
while ($rows = mysql_fetch_assoc($result)) {...
If you have only one, remove just if:
$rows = mysql_fetch_assoc($result);
echo ...
EDIT:
$image = $rows['filepath'] . '/' . $rows['filename'];
Dont use :
if($rows=mysql_fetch_assoc($result))
{
$image = $rows['filepath'];
echo "<img style='width:150px;height:150px' src='$image'>";
echo "<br>";
}
use this directly
$rows=mysql_fetch_assoc($result)
$image = $rows['filepath'];
echo "<img style='width:150px;height:150px' src='$image'>";
echo "<br>";
I want to show all images from sql to my webpage.All images will come But the only
thing is it will not show every image separately.
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("buy") or die(mysql_error());
$get = "Select * from hibuy";
$get2 = mysql_query($get);
while ($row=mysql_fetch_array($get2, MYSQL_ASSOC)){
header("Content-type: image/jpeg");
echo "<div >";
echo "<img src='".$row['image']."'>"; // Here This is not Working
echo "</div>";
}
?>
Your approach is "a bit unorthodox", but this way it might work:
while ($row=mysql_fetch_array($get2, MYSQL_ASSOC)){
$imageData = base64_encode($row['image']);
// Format the image SRC: data:{mime};base64,{data};
$src = 'data:image/jpeg;base64,'.$imageData;
echo "<div >";
echo "<img src='".$src."'>";
echo "</div>";
}
Given, you have image data in your database, not the URL of the image.
In that case, just leave the header....
as seen here: http://davidwalsh.name/data-uri-php
You can try giving an id to each one of the divs or style them for margin that is greater than or equal to the image width.Assuming you image width is 50px, this code shall work.
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("buy") or die(mysql_error());
$get = "Select * from hibuy";
$get2 = mysql_query($get);
$i = 0;
while ($row=mysql_fetch_array($get2, MYSQL_ASSOC)){
header("Content-type: image/jpeg");
echo "<div style='margin-left:".$i."'>";
echo "<img src='".$row['image']."'>"; // Here This is not Working
echo "</div>";
$i+=50;
}
?>
When I run this query, it works fine but the individual fields would pull out. Like ThumbFilePath and Title.
If I run the query with only one field like:
$result = mysql_query("select ThumbFilePath from artwork where SCID = $SCID") or die(mysql_error());
It works fine. Any ideas why I can't pull the other fields?
<?php
$dbname = 'pdartist2';
$table = 'artowrk';
// query
$result = mysql_query("select AID, ThumbFilePath, Title, DisplayOrder from artwork where SCID = $SCID") or die(mysql_error());
while($row = mysql_fetch_row($result))
{
foreach($row as $cell)
{
echo "<div id='thumb_container'>";
echo "
<a href='gallery_detail.php?AID=$AID'>
<img src='http://markdinwiddie.com/PHP2012/$ThumbFilePath' title='Enlarge' alt='Enlarge' border='0'>
</a>
";
echo "$Title";
echo "</div>";
}
}
mysql_free_result($result);
?>
In general, you'll ALWAYS need to dereference the columns you need. For example:
while($row = mysql_fetch_row($result)) {
$aid = $row['AID'];
$tpath= $row['ThumbFilePath'];
$title = $row['title'];
...
<?php
$dbname = 'pdartist2';
$table = 'artowrk';
// query
$sql = "select AID, ThumbFilePath, Title, DisplayOrder from artwork where SCID = $SCID";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_row($result)) {
$AID = $row['AID'];
$ThumbFilePath= $row['ThumbFilePath'];
$Title = $row['Title'];
$DisplayOrder = $row['DisplayOrder'];
echo "<div id='clear'></div>";
echo "<div id='thumb_container'>";
echo "<a href='gallery_detail.php?AID=$AID'><img src='http://markdinwiddie.com/PHP2012/$ThumbFilePath' title='Enlarge' alt='Enlarge' border='0'></a>";
echo "<div id='name_spacer'></div>";
echo "<div id='thumbdesc'>";
echo "$Title";
echo "</div>";
echo "</div>";
}
mysql_free_result($result);
?>