将输出存储在 mysql 中 [英] storing output in mysql

查看:32
本文介绍了将输出存储在 mysql 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将这个问题的输出存储在 mysql 中

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

根据大小过滤网页中的图片

<?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) . "')");
    }
} 

?>

但令我惊讶的是,仅存储了输出的第一个链接/链接.我已经使用 echo 来检查并且 echo 给出了正确的输出.

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.

我用于存储此代码的 mysql 数据类型是文本,我正在使用此 mysql 查询插入 mysql,但仅保存第一行.

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) . "')");

从 echo 获取结果时一切似乎都很好,但后来它没有存储在 mysql 中.

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

我试图在表单字段中填写这些回显详细信息,令我惊讶的是,只有第一行也填写在表单字段中,所以无论 mysql 存储的是根据表单字段输出,我只是试图检查它的 mysql 问题还是什么.我直接在 phpmyadmin 中执行了查询,一切都刚刚存储,但通过表单它没有得到,但是 echo 提供了完整的细节.

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.

推荐答案

这是我使用的代码

<?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 />";
    }
}

?>

我的结果如下:

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.

这篇关于将输出存储在 mysql 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆