Just to preface im very new to this so this might be a dumb question but...
I am trying to design a website pulling from a database where i've stored the product info for the 'shop'. This is the code i have so far.
session_start();
include("connections.php");
$con = mysqli_connect($servername, $username, $password, $database);
$sql="SELECT product_title, product_desc, product_image, product_price, product_type FROM tbl_products";
$results=mysqli_query($con, $sql);
if(mysqli_num_rows($results) > 0) {
while($row=mysqli_fetch_array($results)) {
echo $row[0];
echo "<br>";
echo $row[1];
echo "<br>";
echo $row[2];
echo "<br>";
echo $row[3];
echo "<br>";
echo $row[4];
echo "<br>";
}
}
mysqli_close($con);
?>
It works fine at displaying the info however rather than displaying the image its just displaying the file name e.g. hoodie(1).jpg. I assume i need to save the images to the database or to the folder but i tried putting them in my code folder and they still dont show up, any ideas?
To display the image use:
echo '<img src="'.$row[2].'" alt="" />';
To upload a image, learn file uploading:
https://www.tutorialspoint.com/php/php_file_uploading.htm
Related
<?php
$conn=mysqli_connect("localhost","id6755695_artemi8","sharanod"
,"id6755695_user_info");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$department = $_POST["department"];
$year = $_POST["year"];
$sem = $_POST["semester"];
$reg = $_POST["regulation"];
$sql = "SELECT book_name, author, edition, image FROM dept_search WHERE
department='$department' AND year='$year' AND semester='$sem' AND
regulations='$reg' ";
$result=mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result))
{
if($row)
{
echo "<br>";
?>
<img src=" <?php echo $row['image']; ?> " height="300" width="300">
<?php
echo "<br>";
echo "<b>",$row['book_name'],"</b>";
echo "<br>";
echo $row['author'];
echo "<br>";
echo $row['edition'];
}
else
{
echo "sorry book not found";
}
}
mysqli_close($conn);
?>
please help me with this code,i am building a library management system.. The thing is I should be able to display the books if the given values are present i have in the database if not book not found must be displayed but in while loop after if, else does not runs.....
As others have pointed out, your else statement will never run. If you are already inside the while loop, you will certainly have $row defined and for that reason, else will never run.
What you can do is, check beforehand if the query returned actual results, like so:
$result=mysqli_query($conn,$sql);
if($result->num_rows > 0){
while($row = mysqli_fetch_assoc($result)){
echo "<br>";
?>
<img src=" <?php echo $row['image']; ?> " height="300" width="300">
<?php
echo "<br>";
echo "<b>",$row['book_name'],"</b>";
echo "<br>";
echo $row['author'];
echo "<br>";
echo $row['edition'];
}
}else{
echo "Sorry book not found";
}
You can try with mysqli_num_rows .. sample code as follows :
$rowcount=mysqli_num_rows($conn,$sql);
if($rowcount!=0){
$result=mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result))
{
echo "<br>";
?>
You are looping through all the rows returned from the "mysqli_fetch..." command. Your "if" and "else" is useless -- you will always have rows. If you get no rows, you do not even enter the body of the while loop.
You need to COUNT the rows returned (count($row)) and display a message that nothing was found if the count is less than one.
All you need to do is that you have to change if the condition from if($row) to if($other_condition)
Currently, you are just checking either there is something inside $row, and this condition will never be wrong unless you will assign it null. Because where $row will have something then while loop will be executed, and when while loop will be executed then if condition will be executed.
you have to simply one thing, that is to change if condition like given below...
if($row['value'] == 'something')
I need to upload and retrieve image from a database, I am able to store the image in the database but unable to display it later. please help
I wrote the following code to retrieve from the database.
$result1=mysql_query("INSERT INTO userdata(id, username, firstname, lastname, imageType, image)VALUES('', '" . $_SESSION['username'] . "', '" . $_SESSION['firstname'] . "', '$lastname','{$image_size['mime']}','{$imgData}')") or die("Invalid query: " . mysql_error());
if($result1)
{
echo "</br>";
echo "Registration successful";
echo "</br>";
echo $lastid=mysql_insert_id();//get the id of the last record
echo "uploaded image is :"; ?>
<img src="imageView.php?image_id=<?php echo $lastid; ?>" /><br/>
<?php
echo "</br>";
}#if result1into db successful
else
{
echo $result1;
echo "Problem in database operation";
imageView.php has the following code:
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("wordgraphic") or die(mysql_error());
if(isset($_GET['id'])) {
$sql = "SELECT imageType,image FROM userdata WHERE id=". $_GET['image_id'];
$result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error());
$row = mysql_fetch_array($result);
header("Content-type: " . $row["imageType"]);
echo $row["image"];
}
mysql_close($conn);
?>
What could be possibly wrong with the code?
When i try to run imageView.php with static id image is getting displayed. So i guess the error is in passing the variable is this code:
echo "uploaded image is :"; ?>
<img src="imageView.php?image_id=<?php echo $lastid; ?>" /><br/>
<?php
what could be possibly wrong?
just a smal rectification
<img src="imageView.php?image_id=<?php echo $lastid; ?>" />
instead of this
src = src path where u r saving the image file lets say in images folder that would be here $imagedata store the file name in the DB so that you can retreive it from the image folder
<img src="images/$imgData" width="your wish" height="your wish"/>
Hey guys am having trouble displaying an image from the DB on a web page.
Am using this to display
echo "<img
src=image.php?"' width=300 height=270/>";
here is my image.PHP
<?php
session_start();
$con1=mysql_connect("localhost","root","roots");
mysql_select_db("blog",$con1);
if(!$con1)
{
die('could not connect'.mysql_error());
}
$q="select image from data where Number=1";
$result= mysql_query($q,$con1);
if($result)
{
$row=mysql_fetch_array($result);
header("content-type:image/jpeg");
echo $row['image'];
}
else
{
echo mysql_error();
}
?>
echo $row['image']; simply display the data's fetched from DB.
Supposed your image column holds data like this http://domain/project/images/img.jpg
echo '<img src="'.$row['image'].'" width=300 height=270 />' ;
I want to retrieve the image from the database and want to display it in a row like if there is 20 images in a database then it should get display in 5 rows containing 4 images in each row...
My code is working properly but i am facing problem in image path as i hv stored the image-name in database and image inside admin/uploads folder.... like uploads folder is inside the admin folder... adminfolder/uploadsfolder/file-name
here in my code i am getting problem in tracing the image path.....
I am Facing problem in this line..
// my all images is in admin/uploads folder
echo "<td><img src=\"".$path[$i]."\" height='100'/></td>";
Here is my full code
<?php
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('dogs_db') or die(mysql_error());
$sql = "SELECT file_name FROM dogsinfo";
$res = mysql_query($sql) or die(mysql_error());
echo "<table><tr>";
while($fetch = mysql_fetch_assoc($res)) {
$path[] = $fetch['file_name'];
}
for($i=0;$i<count($path);$i++){
if($i%4 == 0){
echo "</tr>";
}
// I am Facing Problem here ...
// my all images is in admin/uploads folder
echo "<td><img src=\"".$path[$i]."\" height='100'/></td>";
}
echo "</tr></table>";
?>
Please help me to sort out this....
How are within the admin/uploads folder, you must put in the src of the img tag.
<?php
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('dogs_db') or die(mysql_error());
$sql = "SELECT file_name FROM dogsinfo";
$res = mysql_query($sql) or die(mysql_error());
echo "<table><tr>";
while($fetch = mysql_fetch_assoc($res)) {
$path[] = $fetch['file_name'];
}
for($i=0;$i<count($path);$i++){
if($i%4 == 0){
echo "</tr>";
}
// I am Facing Problem here ...
// my all images is in admin/uploads folder
echo "<td><img src=\"admin/uploads/".$path[$i]."\" height='100'/></td>";
}
echo "</tr></table>";
?>
I would like to ask for some help because I am stuck at that problem. I have a MySQL DB and I am trying to create some kind of a blog system. The problem which occure to me is this:
I would like to check is there is an image tag if not not to display it. The system works perfect without images but it would be nice if I new where I messed it up. Here is my code up to now:
function outputStory($article, $only_snippet = FALSE){
global $conn;
if($article){
$sql = "SELECT ar.*, usr.name FROM cms_articles ar LEFT OUTER JOIN cms_users usr ON ar.author_id = usr.user_id WHERE ar.article_id = " . $article;
$result = mysql_query($sql, $conn);
if($row = mysql_fetch_array($result)){
echo "<h2>" . htmlspecialchars($row['title']) . "</h2>\n";
echo"<h5><div class='byLine'>От:" . htmlspecialchars($row['name']) . "</div>";
echo "<div class='pubdate'>";
if($row['is_published'] == 1){
echo date("F j, Y",strtotime($row['date_published']));
} else {
echo "No articles are published yet!";
}
echo "</div></h5>\n";
if ($only_snippet){
echo "<p>\n";
echo nl2br(trimBody($row['body']));
// I think I messed this statement alot but don't know how to make it work properly :S
if(hasPostImage() == true){
$getPostImage = "SELECT * FROM cms_images";
$getImgResult = mysql_query($getPostImage,$conn);
$rowImg = mysql_fetch_array($getImgResult);
echo"<img src='".$rowImg['img_src']."' alt='".$rowImg['img_desc']."'>";
} else {
echo '<img style="display:none">';
}
echo "</p>\n";
echo "<h4>More...</h4><br>\n";
} else {
echo "<p>\n";
echo nl2br($row['body']);
echo "</p>\n";
}
}
}
}
I hope the question is not silly or something else and thank you for the help in advance.
There are many ways for doing this, my first approach is to first check you are getting file in $_FILES is its okay then there is functions available in PHP to check file size means this will give you files size in bytes and height and width as well. File Size()
also getimagesize() if this criteria matches your criteria then proceed further