[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Retrieving Images from a image name referenced in a database

Posted on 2013-12-27
9
Medium Priority
?
263 Views
Last Modified: 2013-12-27
I have image names stored in a database and the actual images stored in a folder. I am trying to rework my code so that it will print the actual image which is stored in a folder called uploads.

$q = "SELECT id, productimage FROM proDescript WHERE id=('13')";

$r = mysqli_query($connect, $q);

if (mysqli_num_rows($r) > 0) {

while ($row = mysqli_fetch_array($r, MYSQLI_NUM)) {
	
		<img src="uploads/ <?php echo "<p><h2>Hello $row[1]!</p>"?>"/> ;

Open in new window

0
Comment
Question by:wchirnside
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39742818
Please post the CREATE TABLE statement for the proDescript table and a sample of the data that is contained in columns id and productimage.  Please post a visual of the contents of the "uploads" directory.  Once we see how these things relate, we can help you get the <img> tags right.
0
 
LVL 35

Assisted Solution

by:gr8gonzo
gr8gonzo earned 1000 total points
ID: 39742833
It looks like you're putting HTML inside your PHP blocks:


while ($row = mysqli_fetch_array($r, MYSQLI_NUM)) {
		<img src="uploads/ <?php echo "<p><h2>Hello $row[1]!</p>"?>"/> ;

Open in new window


I would expect to see something like this:

...
while ($row = mysqli_fetch_assoc($r)) {
?>
		<img src="uploads/<?php echo $row["productimage"]; ?>">
<?php
}
?>

Open in new window


Also note that I changed the record retrieval to mysqli_fetch_assoc($r)
0
 

Author Comment

by:wchirnside
ID: 39742918
Thanks for your quick reply

Yes, I am putting HTML with my php which I realize is most likely incorrect.
 
Please note that the following code will bring up the name of the file that is located in the database.
$q = "SELECT id, productimage FROM proDescript WHERE id=('13')";

$r = mysqli_query($connect, $q);

if (mysqli_num_rows($r) > 0) {

while ($row = mysqli_fetch_array($r, MYSQLI_NUM)) {
	
	 echo "<p><h2>Hello $row[1]!</p>";

Open in new window


Sorry but what exactly is meant by create table statement.
uploads.png
Table.png
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39742921
This article shows how to detect errors in the query, including how to visualize both the failing query and the exact error message.  You might want to do that with all of your queries.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

$q = "SELECT id, productimage FROM proDescript WHERE id=('13')";
$r = mysqli_query($connect, $q);
if (!$r) { /* QUERY FAILED */ }

Open in new window

When the query fails, you want to print out the fully-resolved query string from the variable $q along with the contents of mysqli_errno() and mysqli_error().  PHP has the trigger_error() function to help with this.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 39742944
MySQL has the SHOW CREATE TABLE query.  It will give you the information used to create the table.

This is a bit of a guess, but I think you might be able to do something like this.
$q = "SELECT id, productimage FROM proDescript WHERE id=('13')";
$r = mysqli_query($connect, $q);
if (mysqli_num_rows($r) > 0) 
{
    while ($row = mysqli_fetch_object($r))
    {
        echo '<img src="uploads/' . $row->productimage . '" />' . PHP_EOL;
    }
}

Open in new window

If you want to get a foundation in how PHP works, this article will lead you to some good learning resources.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
 

Author Comment

by:wchirnside
ID: 39742963
CREATE TABLE `proDescript` (
 `id` smallint(6) NOT NULL AUTO_INCREMENT,
 `users_id` mediumint(20) NOT NULL,
 `title` varchar(40) CHARACTER SET utf8 NOT NULL,
 `currency` tinyint(4) NOT NULL,
 `amount` varchar(30) CHARACTER SET utf8 NOT NULL,
 `location` varchar(40) CHARACTER SET utf8 NOT NULL,
 `description` varchar(300) CHARACTER SET utf8 NOT NULL,
 `productimage` varchar(100) CHARACTER SET utf8 NOT NULL,
 `active` tinytext CHARACTER SET utf8 NOT NULL,
 `date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=24 DEFAULT CHARSET=latin1
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39742992
Thanks.  I think you will find the "fetch_object()" functions to be more useful over time.  There is less fiddly punctuation involved.  Let us know how things turn out when you try the suggestions.  If you get error messages, please post those here along with the code and we'll try to help get you to a good solution.
0
 

Author Comment

by:wchirnside
ID: 39743033
Both suggestions worked just fine and I did a bit of experimentation as well. I will add the codes to my folder of working scripts.

I took a look at your article and feel that I am learning PHP from reading several of the books that were suggested. The first book I ever bought on this subject was Kevin Yank's "Build Your Own Database Drive Web Site Using PHP & MYSQL" and have since bought the updated version.

There is still lots to learn and I suppose it is never ending.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39743037
Yank is pretty good.  You're right -- it's never ending.
... add the codes to my folder of working scripts.
In my experience, the most valuable asset I have created is the library of teaching examples.  I hope your set of examples is as useful to you as mine has been to me!

Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question