0

I am stuck for 2 days getting this to work.

I am data from a website, all data is getting perfect except for the images.

I have this code for image part.

       foreach($html->find('li')as $pakimages)
      {
          foreach($pakimages->find('img') as $pakimage)

                    {
                        $images = $pakimage;

                        echo $images->src .'<br>';


                    }


      }

When I run this code its fetches all images of the vehicles, but I need only first images of the vehicle not all related vehicle images.

  • 2
    html is just text. you insert/retrieve it like you would any other text. And you are vulnerable to [sql injection attacks](http://bobby-tables.com). – Marc B Jun 01 '15 at 17:08
  • bro my question is simple, how to retrieve images from database and display – Syed Käshäñ Ñïzâmî Jun 01 '15 at 17:36
  • You are doing it wrong! You shouldn't store the HTML like that instead, when you crawl you should really save the image (To file system or database) and then use that. There is an incredible overhead in having to parse every little piece of info from HTML in a database. Learn about normalization first if you want to do it the (my)SQL way... – EJTH Jun 01 '15 at 17:47
  • 1
    Ugh, don't "bro" people. We're professionals here. Especially don't do that when he told you a valuable piece of info that you aren't heeding. AND EJTH is correct as well, by the looks of it you need to clean up that data before it hits the database. You're storing a bunch of data each row you don't need and it isn't normalized. At least if all you are doing is saving a file path, it's easy to work on. – patricksweeney Jun 01 '15 at 17:49
  • Thanks EJTH for the recommendation, I will try to save the pic to file system and then tell you about where I stuck – Syed Käshäñ Ñïzâmî Jun 01 '15 at 17:59

3 Answers3

0

If you just want to get images, use the following SQL:

SELECT image FROM vehicle

Twisty
  • 30,304
  • 2
  • 26
  • 45
0

Instead of

foreach($pakimages->find('img') as $pakimage)
{
    $images = $pakimage;
    echo $images->src .'<br>';
}

Why don't you just do

$image = $pakimages->find('img');
echo $image[0]->src . '<br>';
Hkan
  • 3,243
  • 2
  • 22
  • 27
  • I have done this. Notice: Undefined offset: 0 in C:\xampp\htdocs\filegetcontents.php on line 186 Notice: Trying to get property of non-object in C:\xampp\htdocs\filegetcontents.php on line 186 http://cache2.pakwheels.com/ad_pictures/778/toyota-prius-s-1-8-2011-7788787.jpg http://cache2.pakwheels.com/ad_pictures/778/toyota-prius-s-1-8-2011-7788788.jpg http://cache4.pakwheels.com/ad_pictures/778/toyota-prius-s-1-8-2011-7788790.jpg http://cache4.pakwheels.com/ad_pictures/778/toyota-prius-s-1-8-2011-7788793.jpg – Syed Käshäñ Ñïzâmî Jun 27 '15 at 07:21
  • I only need image at index[0] of the vehicles, the code above retrieve all images of the vehicles – Syed Käshäñ Ñïzâmî Jun 27 '15 at 07:25
  • *Undefined offset* error is because one of the vehicles didn't have any images. You can always check with `count($pakimages->find('img'))`. – Hkan Jun 27 '15 at 08:05
-1
  • Image:

For insert and retrieve images to and from database using php : Firstly, you should check if your image column is BLOB type! and You can See this example : https://vikasmahajan.wordpress.com/2010/07/07/inserting-and-displaying-images-in-mysql-using-php/

  • Html code:

Inserting html code in a mysql table

how to insert HTML code into DB using php

Good Luck.

Community
  • 1
  • 1
Mohammed Akdim
  • 2,223
  • 1
  • 13
  • 21
  • Why would you store images on mySQL and not the file system? – EJTH Jun 01 '15 at 17:53
  • If you have another solution you are welcome. Thanks – Mohammed Akdim Aug 25 '15 at 10:33
  • Don't store images in your mySQL database? – EJTH Aug 25 '15 at 10:40
  • Yes, after my search in the internet i found that "store images in the database" is not advisable and bad practice. Do not store images in the database. Store images in directories and store references to the images in the database. so how to store images in the file system ?? – Mohammed Akdim Aug 25 '15 at 15:46
  • Like you store any other file in the file system? This is pretty basic stuff. It all depends on what you use, if you use PHP you could basicly just `move_uploaded_file` to the place where you want it. I'd suggest storing the true file name in database though and don't use that in the filesytem, it just leads to cases where path injection could become a viable attack vector... – EJTH Aug 26 '15 at 09:18