-4

As the title says, I can't insert & display any image to & from mySQL db table. Image table looks like the photo attached and my php code looks like this:

<?php
    $sql2 = "SELECT * FROM images";
    $records2 = mysql_query($sql2);
       while($images = mysql_fetch_assoc($records2)) {   
           echo $images['path'];     
       }
?>

The path is displayed in the page, not the actual image.

I've made the MYSQL connection so it is fine.enter image description here

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Sergiu Turus
  • 27
  • 1
  • 1
  • 8
  • 1
    *"I can't insert & display any image"* - what do you mean by "insert"? Use `` which should be the clincher here. You're just echoing the path here. You might have problems with this also, in the way you set that path for it, if and when you access your executable from a different location. – Funk Forty Niner Jun 06 '16 at 13:53
  • 1
    `echo "";` there, that ought to do it. and if that doesn't work, then that will mean that the path you've set for it, should have been as a full server path starting at the root, and not a relative path. If so, then you'll need to add to it. I.e.: `echo "";` – Funk Forty Niner Jun 06 '16 at 13:57
  • I'd like to insert an image from my PC to the mySQL table using its path. – Sergiu Turus Jun 06 '16 at 13:58
  • so.... 2nd comment; what did that do? That should have worked. I made an edit to it, so you'll need to reload it. – Funk Forty Niner Jun 06 '16 at 14:04
  • You'll need to ping me, I can't stand here for very much longer. To ping someone means to use the `@` symbol followed by their name. – Funk Forty Niner Jun 06 '16 at 14:10
  • it doesn't work. I'm not on Linux either – Sergiu Turus Jun 06 '16 at 14:10
  • 1
    "doesn't work" isn't much to go on here. You say you want to insert an image but your question doesn't support it. There are a few ways to upload images, so you'll need to Google that if that's what the issue here is, that there's no image in your path set in the db or you used a relative path and now you're most likely getting errors for, but not checking for them. The **less** I know, the **more** time it takes. Remember that. – Funk Forty Niner Jun 06 '16 at 14:12
  • 2
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 06 '16 at 14:14
  • I want to insert a path to an image into my table so I can display it using PHP. I've been googling and found different methods, none of them are using img path @Fred -ii- – Sergiu Turus Jun 06 '16 at 14:18
  • @SergiuTurus You can't just tell MySQL "insert this file", it doesn't work that way. You need to load it in as binary data. – tadman Jun 06 '16 at 14:23
  • Am I the only one realizing that `mysqli_query()` is not connecting to the database? And yes, Jay Blanchard is right, stop using `mysql_*`. – Zeke Jun 06 '16 at 14:23
  • Still, I'm so against this approach... I wouldn't save the entire path to the file, I think it's better to save only the name and maybe the extension, and maybe even encrypting the name or using some kind of ID value for the images depending on what I need... mainly because you might want to change the folder later so... – Zeke Jun 06 '16 at 14:26
  • 1
    @Zeke *"Am I the only one realizing that mysqli_query() is not connecting to the database?"* - I came back to this question just to see where this was at, out of curiousity. I too thought the same thing an hour ago, yet the OP states in the question: *"I've made the MYSQL connection so it is fine."* - The question's unclear for me after commenting back and forth with the OP, so I decided to no longer do that, nor posting an answer, since I have no idea which animal(s) I'm dealing with here. As to which MySQL API they're using though, is unknown and if it truly is connecting with `mysql_`. – Funk Forty Niner Jun 06 '16 at 15:03
  • @Fred-ii- Agreed. I really don't know what's going on, I think your comments solve the issue, maybe there's no `` tag either (haha). In any case, there's nothing else I can think of... maybe the file wasn't uploaded? I don't know. Closed case to me. – Zeke Jun 06 '16 at 15:12
  • 1
    @Zeke I had written up an answer for it earlier but didn't press the *"Post Your Answer"* button as soon as I saw their *"it doesn't work"* comment. I'd of been chased down a very deep rabbit hole for it. Rabbits see/run better than I do in such a dark and closely-confined area. At least I know what "that animal" can do ;-) – Funk Forty Niner Jun 06 '16 at 15:15
  • It works now @Fred-ii- the mySQL path wasn't quite accurate – Sergiu Turus Jun 06 '16 at 18:02
  • @SergiuTurus Great! That's what my initial feeling was, being the path. – Funk Forty Niner Jun 06 '16 at 18:08
  • @SergiuTurus At least you didn't forget the `WHERE` clause in a `DELETE` statement haha glad it worked, but be more specific and show more code next time! Happy coding :) – Zeke Jun 06 '16 at 23:13
  • 1
    @Fred-ii- post the answer, you're the rightful owner of an accepted answer here. – Zeke Jun 06 '16 at 23:13
  • 1
    @Zeke Strangely enough, I had it already set in a temp text file I still hadn't closed. It's been posted below. *Cheers* – Funk Forty Niner Jun 06 '16 at 23:19

1 Answers1

0

This is an answer that I had prepared beforehand, but never submitted it at the time.

Consult "Footnotes".

You're just echoing the path here and not the image source.

I.e.: <img src..>.

You might have problems with this also, in the way you set that path for it, if and when you access your executable from a different location.

echo "<img src=\"$images['path']\">";

If that doesn't work, then that will mean that the path you've set for it, should have been as a full server path starting at the root, and not a relative path.

If so, then you'll need to add to it.

I.e.: echo "<img src=\"/var/usr/htdocs/Projects/facebook/img/$images['path']\">";

or and as an example from the screenshot you left:

echo "<img src=\"/Projects/facebook/img/$images['path']\">";


Footnotes:

"It works now @Fred-ii- the mySQL path wasn't quite accurate – Sergiu Turus 5 hours ago"

It seems that I was right all along.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141