Error with image display - php

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>";

Related

No img showing for mysql query and no stocknr when hovering

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)

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>";

Why is my images not appearing, when trying to get them from database

Hiya I am getting information from my database to present onto the webpage, everything is correct with connection as the information is present on the page.
But when it comes to the pictures they come up with a little box of where they are meant to be.
Heres the code I have:
<?php
error_reporting(0);
require './db/connect.php';
include './includes/header.php';
?>
<h2>Production</h2>
<?php
if($result = $connection->query("SELECT * FROM Production")){
if($count = $result->num_rows){
$Image = $rows['Image'];
while($row = $result->fetch_object()){
echo '<pre>'.'<img class="productionimages" src="path/'.$Image.'" />',' '
,$row->ProductionName,' ',$row->ProductionType, '</pre>';
}
$result->free();
}
}
echo $result;
include './includes/footer.php';
?>
Heres a picture below of what appears on the screen.
You need to set $Image within the while loop and $rows['Image'] should be $row->Image, like so.
<?php
if($result = $connection->query("SELECT * FROM Production")){
if($count = $result->num_rows){
while($row = $result->fetch_object()){
$Image = $row->Image;
echo '<pre>'.'<img class="productionimages" src="path/'.$Image.'" />',' '
,$row->ProductionName,' ',$row->ProductionType, '</pre>';
}
$result->free();
}
}
echo $result;
include './includes/footer.php';
?>

Mysql Field Data not displaying when a link is clicked?

I'm trying to get data from a database if a link is clicked.
I used the example codes suggested from this example -Getting mysql field data when a link is clicked?
But it doesn't work when I click on a link nothing comes up.
main.php
<?php
include('conn.php');
$sql2 = "SELECT Title FROM addpromo";
$result2 = mysql_query($sql2);
echo "<div id=\"links\">\n";
echo "<ul>\n";
while ($row2 = mysql_fetch_assoc($result2)) {
echo "<li> <a href=\"fullproject.php?title=\""
. urlencode($row2['Title']) . "\">"
. htmlentities($row2['Title']) . "</a>\n</li>";
}
echo "</ul>";
echo "</div>";
?>
This is displaying correct.but when I click at a link nothing is showing up in fullproject.php, Just a blank page.
fullproject.php
<?php
// Connect to server.
include('conn.php');
$projectname = isset($_GET['Title']);
$sql1 = "SELECT Title FROM addpromo WHERE Title = '$projectname'";
$result1 = mysql_query($sql1);
while ($row1 = mysql_fetch_assoc($result1)) {
echo "Project Name: " . $row1['Title'] . "<br />";
echo "<br /> ";
}
?>
Can someone help me to fix this, or any other way to make this(to get data from a database if a link is clicked) possible?
Change to this
main.php
<?php
include('conn.php');
$sql2="SELECT Title FROM addpromo";
$result2=mysql_query($sql2);
echo '<div id="links">';
echo '<ul>';
while($row2 = mysql_fetch_assoc($result2)){
echo '<li>'.htmlentities($row2['Title']).'</li>';
}
echo '</ul>';
echo '</div>';
?>
fullproject.php
<?php
if(isset($_GET['title'])){
include('conn.php');
$projectname= $_GET['title'];
$sql1="SELECT Title FROM addpromo WHERE Title = '$projectname'";
$result1=mysql_query($sql1);
while($row1 = mysql_fetch_assoc($result1)) {
echo "Project Name: " . $row1['Title']. "<br />";
echo "<br /> ";
}
}
?>
This is storing a boolean value $projectname= isset($_GET['Title']);, whether or not the title is set. Instead use $projectname = $_GET['Title'];
isset returns a boolean value (true/false) and you want the actual value of the variable:
$projectname= $_GET['title'];
Furthermore, you have to pass only the title as the URL parameter, without enclosing it within quotes. So there is an error in this line:
echo "<li> <a href=\"fullproject.php?title=" . urlencode($row2['Title']) . "\">"
Note the lack of \" after title=

How to retrieve all images from sql to the html DIV tag using php?

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;
}
?>

Categories