0

i am trying to store the output of this question in mysql

filter images from webpage based on size

<?php 

include('connect.php'); 

$html = file_get_contents("http://santabanta.com/photos/amisha-patel/402186.htm"); 

$doc = new DOMDocument();
@$doc->loadHTML($html);

$tags = $doc->getElementsByTagName('img'); 

foreach ($tags as $tag) { 
    $data = get_headers($tag->getAttribute('src'),1); 
    if((intval($data["Content-Length"])/1024)>=10){ 
        echo $tag->getAttribute('src');
        $url=''.$tag->getAttribute('src').'';
        echo $url;
        mysql_query ("INSERT INTO table1 (url) VALUES ('" . mysql_real_escape_string($url) . "')");
    }
} 

?>

But to my surprise only first link/link of the output are being stored . i have used echo to check and echo is giving correct output.

My mysql datatype for storing this code is text and i am using this mysql query to insert into mysql but only first line is being saved.

mysql_query ("INSERT INTO tablea (url) VALUES ('" . mysql_real_escape_string($url) . "')");

Everything seems to be fine when getting result from echo but later it is not storing in mysql.

I tried to fill these echo details in form field and to my surprize only first line got filled in form field as well so whatever mysql is storing is as per form field output which i just tried to check whether its mysql problem or what. I executed the query directly in phpmyadmin and everything just got stored but through form it is not getting however echo gives full details.

7
  • This line will insert one column of one record into your table. Why are you expecting it to do more? Are you executing this inside a loop? Please post more context. Commented Aug 17, 2012 at 2:07
  • @somekittens please see full code Commented Aug 17, 2012 at 2:14
  • @PeterGluck please see full code Commented Aug 17, 2012 at 2:15
  • The new code posted still doesn't show the context of the INSERT statement. That's what we need to see. Commented Aug 17, 2012 at 2:18
  • Are you checking the return value of your mysql_query()? That should give you clues as to why it doesn't store your data Commented Aug 17, 2012 at 2:25

1 Answer 1

1

Here is the code I used

<?php 

include('connect.php'); 

$html = file_get_contents("http://santabanta.com/photos/amisha-patel/402186.htm"); 

$doc = new DOMDocument();
@$doc->loadHTML($html);

$tags = $doc->getElementsByTagName('img'); 

foreach ($tags as $tag) { 
    $data = get_headers($tag->getAttribute('src'),1); 
    $filesize = (intval($data["Content-Length"])/1024);
    // Modify the integer below to what size KB you want.
    if($filesize >= 10){ 
        //echo $tag->getAttribute('src');
        $url = $tag->getAttribute('src');
        // Do the insert here so it enters if all criteria are met.
        $query = "INSERT INTO tablea (url) VALUES ('" . mysql_real_escape_string($url) . "')";
        $result = mysql_query($query);
        // This to check what the error may be and your insert statement.
        if(!$result) echo mysql_error() . " | " . $query . "<br />";
    } else {
        echo "File not added: ".$tag->getAttribute('src')." | " .$filesize. "kb<br />";
    }
}

?>

And My result is as follows:

File not added: http://media.santabanta.com/medium/indian%20%20celebrities(f)/sambhavna%20seth/sambhavna-seth-10a.jpg | 6.015625kb
File not added: http://media.santabanta.com/medium/bollywood%20movies/jism%202/jism-2-17a.jpg | 4.9638671875kb
File not added: http://media.santabanta.com/medium/indian%20%20celebrities(f)/kangana%20ranaut/kangana-ranaut-45a.jpg | 5.09375kb
File not added: http://media.santabanta.com/medium/indian%20%20celebrities(f)/bidita%20bag/bidita-bag-0a.jpg | 8.7138671875kb
File not added: http://media.santabanta.com/medium/indian%20%20celebrities(f)/ileana/ileana-14a.jpg | 6.703125kb
File not added: http://media.santabanta.com/medium/indian%20%20celebrities(f)/yami%20gautam/yami-gautam-6a.jpg | 4.78515625kb
File not added: http://media.santabanta.com/medium/bollywood%20movies/ek%20tha%20tiger/ek-tha-tiger-15v.jpg | 6.15234375kb
File not added: http://media.santabanta.com/medium/events/independence%20day/independence-day-78a.jpg | 8.470703125kb
File not added: http://media.santabanta.com/medium/bollywood%20movies/ek%20tha%20tiger/ek-tha-tiger-14a.jpg | 5.8408203125kb
File not added: http://media.santabanta.com/medium/indian%20%20celebrities(f)/diana%20penty/diana-penty-3a.jpg | 7.7099609375kb
File not added: http://media.santabanta.com/medium/indian%20%20celebrities(f)/sana%20khan/sana-khan-2a.jpg | 6.6640625kb
File not added: http://media.santabanta.com/medium/emotions/love/love-126a.jpg | 4.18359375kb
File not added: http://b.scorecardresearch.com/p?c1=2&c2=13655906&cv=2.0&cj=1 | 0.0009765625kb

Since the files do not match the criteria, they are not being added. Only two images where added to the database this time.

Sign up to request clarification or add additional context in comments.

7 Comments

please see my updated code it ids exactly what you have given.echo gives correct output it means my codes are correct but its not coming in either form or in mysql
@user1515503 Check my edit and try the modified code post back any errors.
Sorry, misplaced the exclamation point. But it is fixed and tested using the same page and no images met the criteria. The code works fine.
your if($!result) echo mysql_error() . " | " . $query . "<br />"; is failing and not allowing progrm to execute
Once again I am sorry that I misplaced it but it is corrected. The above edited code is CORRECT!!!! Sorry if you cannot troubleshoot a simple line of php.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.