In a query say i have a image path that have multiple images stored as path=http://localhost/images/xyz.When i run the code:

<?php
//Retrieves data from MySQL
mysql_connect("localhost", "root", "") or die 
("Could not save image name Error: " .   mysql_error());
mysql_select_db("dawat") or die("Could not select database");
$data = mysql_query("SELECT 'images_path' FROM images_tbl") or
die(mysql_error());
//Puts it into an array
$file_path = 'http://localhost/images/xyz';
while($row = mysql_fetch_assoc( $data ))
{//Outputs the image and other data
    $src=$file_path.$row["images_path"];
    echo "<img src=".$src.">  <br>";
}
?>

Everything is working fine,but only images are not showing.On place of images it is showing small thumnails.Please suggest what to do.... Sql query i used is:

CREATE TABLE images_tbl(
   images_id INT NOT NULL AUTO_INCREMENT,
   images_path VARCHAR(200) NOT NULL,
   submission_date DATE,
   PRIMARY KEY (images_id)
);

Recommended Answers

All 31 Replies

I suggest using a relative path for $file_path instead of a full http link for e.g:

$file_path = 'images/xyz/';
commented: It even doesn't work.. +0

It also doesn't helping

In your browser, right click the image placeholder, inspect element. What is the path? compare that path to where you know the images are being stored. Is is the same, different? likely different, hence no image. Seems you simply need to get the path correct.

It is <img src="images_pathimages_path"></img>So what should i do right now??I think the path is same..

Well, images_pathimages_path, doesn't look right. I'm not a PHP developer so its hard for me to tell you what is wrong with your code, unless the data in your DB table has this value for its records?

Try taking the single quotes out from your select.

You appear to be selecting the string literal 'images_path' on line 6

this in not right <img src="images_pathimages_path"></img> result, second remove single quete in your query, if you want to select column, like your query is this

$data = mysql_query("SELECT 'images_path' FROM images_tbl") or die

change this to

$data = mysql_query("SELECT images_path FROM images_tbl") or

//note i remove single quete from your images_path,

second change this line to

$src=$file_path.$row["images_path"];
path generated by this code is http://localhost/images/xyz/YourFileName
to

$src=$file_path.'/'.$row["images_path"];
path generated by this code is http://localhost/images/xyzYourFileName

note the different you got one extra / after xyz, assuming xyz is folder, remeber xyz should be a folder not actual file name other wise it will not work, if you

still cant get it working inspect your element, it will lot easier for anyone to help you out, consider your self one step away from solution,

thanks

Thanx but it even doesn't work i have updated code as:

<?php
//Retrieves data from MySQL
mysql_connect("localhost", "root", "") or die 
("Could not save image name Error: " .   mysql_error());
mysql_select_db("dawat") or die("Could not select database");
$data = mysql_query("SELECT images_path FROM images_tbl") or
die(mysql_error());
//Puts it into an array
$file_path = 'http://localhost/images/xyz';
while($row = mysql_fetch_assoc( $data ))
{//Outputs the image and other data
    $src=$file_path.'/'.$row["images_path"];
    echo "<img src=".$src.">  <br>";
}
?>

sorry my bad check for the view page source

and one more thing, its only gonna happened if you have two empty row in the image path, column
check your view page source should look like this

<img src=http://localhost/images/xyz/>
<img src=http://localhost/images/xyz/>

and one more thing do you know what your doing in the while loop in this line

$file_path = 'http://localhost/images/xyz';
while($row = mysql_fetch_assoc( $data ))
{//Outputs the image and other data
    $src=$file_path.'/'.$row["images_path"];

you are telling loop to run according to number of row you got in the column image_path,

thats why you are getting result like this means you have two row, because you are getting two path like this src="http://localhost/images/xyz/http://localhost/images/xyz"

so i am assuming that you got two row in that column and both are empty.

second you must know what you are doing in this line in while loop

$src=$file_path.'/'.$row["images_path"];//in your while loop

$src is variable name you give than you say its value going to be

$file_path.'/'.$row["images_path"];

this line have three part

in here $file_path is equeal to http://localhost/images/xyz

than you add .'/'. which is equal to / means you are saying add this after file path

than you are saying add this part $row["images_path"]

as you can see that your image path is this which you just posted in your reply

http://localhost/images/xyz/

if you look closly it is giving you value for first part which $file_path, than second is just / but there is no third value means your $row["image"];
is not returning any thing,

but than how can i say that you got two row in there, because your loop is running twice. thats means you got two row in there but none of them any value,

go and check the image_path column in your database in image_tbl, and post your reply, with this information whats the value of each row in image_path, column

thanks

why so many edit, cause english is not the first language so i tend to make plenty of mistake, if you dont understand some think because of my poor english ask again, i will explain you again.

Forget everything,i had tried many ways but it can't.I request you all i just need code that will fetch images from php stored in a folder by a html page.So please provide me code in which i will store images in folder and fetch from code.Images must be stored in path not in mysql itself.Please and thanx eveyone who had helped for removing above errors.

rani_4, i am not going to give you the code, you want to down vote my comment you can down vote it, i have solution for you but I dont know weather you are intrested or not. reason not to give code is this i am learning my self so i come here and other forum try to see real world problem and try to solve them in that process i gain extra knowledge, so giving you a code its feel like giving some thing to a lazy person, means you can do it if you follow other people advice but you choose not to do it.

i dont know why but i am posting these next line as well, its just a hint.

you have to run insert query,
you have to tell us your folder structure,
and third you have to give us atleast one or two image name for the image file with extenstion.

if you dont know how to run insert query i will help you, if you dont know what the folder structure is i will make one for you for your code, if you dont know what the extenstion is i will tell you how to grab it,

but giving you a code is big no no.

Its ok,so much caring for me doesn't need.i can solve and i have bundles of code.But i was just asking so i can get fastly.But i request you to test my above code because even it is not running.Just a request.

you can be rest assured that there is nothing wrong in your code, your code have no mistake, the problem is there from where you are getting your data through code, i am just joking your bundle of code if its for php will not help you cause your problem is data related. not php related,

your folder structure should be this in order to for your code to work.

images (folder) in images folder you need two thing
your file with this code.
AND your (xyz) folder,
in xyz(folder) you need your images.

and even than if does not work thats mean you have to run insert query. to populate your data base

in short there is nothing wrong in your code its bit outdated but nothing wrong,

and i dont know why you wrote this <<<Its ok,so much caring for me doesn't need>>

may be you were angry about my last post i dont know,

so nothing wrong with your code make sure your structure in order, nothing happen run insert query,
Thanks have a nice day. one more question are you learning by your self, if yes and you do have access to internate try some website they have free vedio and blog. because i learning by my self as well and all the material i got, i got it for free.

and one more thing your insert query gonna be run through php not by sql and once only. If you could not do it ask again and sorry i told you in my first post consider your self to be one step away from solution, you have taken many steps after that, but this time i truly beleive you are only one step away from solution. thanks

and what do you mean by this
<<<test my above code because even it is not running>>>
means what you are trying to say is this, that this code is running but not producing the desire result. because this code is fine. reason i am saying this i tried your code, it gave me desired outcome.

Each image has a file name, and a parent directory (path). What does the images_path field in your database hold? File name, path, or both?

$src=$file_path.'/'.$row["images_path"];

Your code already concatenates the path regardless of your images_path value. If that field also contains the path, then you will of course have duplicate path, which is wrong.

Hi,

I am not sure what is stored in your database, I am assuming only image name is stored there.

I have tested this any worked fine for me,

<?php
//Retrieves data from MySQL
mysql_connect("localhost", "root", "", "dawat") or die 
("Could not save image name Error: " .   mysql_error());
mysql_select_db("dawat") or die("Could not select database");

// remove single quotes from images_path
$data = mysql_query("SELECT images_path FROM images_tbl") or
die(mysql_error());
//Puts it into an array

// add "/" at last
$file_path = 'http://localhost/images/xyz/';

while($row = mysql_fetch_assoc( $data ))
{
//Outputs the image and other data
    $src = $file_path . $row["images_path"];
    // Test with echo, 
    echo $src;
    echo "<img src=".$src.">  <br>";
}
?>

Image name is stored in image path.There i just store http://localhost/images/xyz/ and image in xyz folder.So where i am doing wrong please suggest.

dear last time when you posted the result for inspect element you told us that you are getting this result, http://localhost/images/xyz/ this result its self suggest that you got nothing in your images_path column, that's why you are getting this result, because your query is returning null value. and one more thing it might be a typo your last post where you say image name is stored in the image path, image path and images path are two completly different things, my suggestion is this run a insert query it should give you result as there is nothing wrong in the code which you got now, you had only two mistake and you corrected them.

before you run insert query this is official warning mysql is depriciated means they are vunralbe to sql injection, you should learn mysqli function or pdo, but i am giving you query mysql one so i dont confuse you further.

run this query after your connection.

AFTER THESE LINE

mysql_connect("localhost", "root", "") or die 
("Could not save image name Error: " .   mysql_error());
mysql_select_db("dawat") or die("Could not select database");
$data = mysql_query("SELECT images_path FROM images_tbl") or
die(mysql_error());

RUN THIS INSERT QUERY

$sql="INSERT INTO images_tbl (images_path) VALUES ('image name')";
$result=  mysql_query($sql);

replace image name with actual image name

lets say my image name with extention is 001.jpg, so run the query like this

$sql="INSERT INTO images_tbl (images_path) VALUES ('001.jpg')";
$result=  mysql_query($sql);

so your whole code should look like this

<?php

mysql_connect("localhost", "root", "") or die 
("Could not save image name Error: " .   mysql_error());
mysql_select_db("dawat") or die("Could not select database");
$data = mysql_query("SELECT images_path FROM images_tbl") or
die(mysql_error());

$sql="INSERT INTO images_tbl (images_path) VALUES ('001.jpg')";
$result=  mysql_query($sql);

$file_path = 'http://localhost/PhpProject2/images/xyz';
$resultstr = array();
while($row = mysql_fetch_assoc( $data ))
{//Outputs the image and other data
    $src=$file_path.'/'.$row["images_path"];
    echo "<img src=".$src.">  <br>";
}
?>

than give another name and run the query again, i dont know how many images you got, you can run the loop and insert them all at once. but i dont want to confuse you so just change the image name and run again, lets say you have five images you can do it five times.

after you finish you should delete these line from your code, because if you dont each time you refresh the page it gonna run the insert query,

if you dont understand just do it as i say after inserting all your images delete these line or comment them out like this.

//$sql="INSERT INTO images_tbl (images_path) VALUES ('001.jpg')";
//$result=  mysql_query($sql);

you should get the desired result, i got it the same way,

thanks.

read the above solution carefully it should be a last thing and it should give you deserid out come, remember you have to give image name with extention, and as i can see your below code for your data structure,

CREATE TABLE images_tbl(
   images_id INT NOT NULL AUTO_INCREMENT,
   images_path VARCHAR(200) NOT NULL,
   submission_date DATE,
   PRIMARY KEY (images_id)
);

you should have no problem, but remember dont forget to give extention, as in 001.jpg jpg is extention.

you dont need to change your code as its giving you proper outcome, and you dont have to give data structure as you already provided. so just the run the insert query as i am telling you.

Please tell us problem solved or not.
Thanks

and one more things. Please post the outcome, if you got any error messages please post them only, and if you dont get any error messages but it still does not work, please only post the inspect element or View page source. and if it's give you desired outcome please tell us so thanks

Thank you so much Arif.Many many thanx,this small mistake has been solved and credit goes to you only.

Arif there is a small query ,suppose i have 100 images in folder and with running of this code i am able to fetch only which i uploaded in db,can i display all images without uploading to db as each image name.I think no but what you say???

your welcome, so you did not have any thing in your database? any way its nice to know that problem solved

yes you can do that way as well, but that way is very complicated
i suggest you make form for this

Can you clue it just a example?? or give a link on net i can find??because i always like complicated things.Just a request

this bloody place chat room no good sorry i sent you 4 or five messages i though you are here. yah in short you can do that, but my suggestion is to make a form run while loop and good to go thanks if you reply early i will reply else i am leaving in five ten minutes

first you should run this code to delete the matching images from the database, because you dont want to insert one image twice

    $con=mysql_connect("localhost", "root", "") or die 
    ("Could not save image name Error: " .   mysql_error());
    mysql_select_db("dawat") or die("Could not select database");
    $data = mysql_query("SELECT images_path FROM images_tbl") or
    die(mysql_error());
    $directory = realpath("images");

    //get all image files with a .jpg extension.
    $images = glob($directory . "*.jpg");

    //print_r($images);
    //print each file name
    foreach($images as $image)
    {
      $sql="delete from  images_tbl where images_path='$image'";
    $result=  mysql_query($sql) or die(mysqli_error($con));
    }

than you got go go just paste this code

<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */



$con=mysql_connect("localhost", "root", "") or die 
("Could not save image name Error: " .   mysql_error());
mysql_select_db("dawat") or die("Could not select database");
$data = mysql_query("SELECT images_path FROM images_tbl") or
die(mysql_error());
$directory = realpath("images");

//get all image files with a .jpg extension.
$images = glob($directory . "*.jpg");

//print_r($images);
//print each file name
foreach($images as $image)
{
  $sql="INSERT INTO images_tbl (images_path) VALUES ('$image')";
$result=  mysql_query($sql) or die(mysqli_error($con));
}

its gonna insert every image in your folder xyz,

Note: this coding should be done in xyz folder thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.