简体   繁体   English

如何确定mysql数据库中的链接是否有效?

[英]How can I find out if links in mysql database are valid?

Massive Novice, so any help greatly appreciated. 大量新手,因此对您有所帮助。

I have a mysql database with about 30,000 entries, each with a link to a thumbnails in .jpg format. 我有一个大约30,000个条目的mysql数据库,每个条目都有指向.jpg格式的缩略图的链接。

I was trying to figure out if there was anyway to go through the mysql database and verify that each link is still valid - aka if the .jpg file is where it is meant to be? 我试图找出是否仍然要通过mysql数据库并验证每个链接是否仍然有效-aka .jpg文件是否在原处?

So each entry has an id, name, thumb (which is an url). 因此,每个条目都有一个ID,名称,缩略图(这是一个URL)。

Thanks! 谢谢!

For links, do a query to get all the links, something like: 对于链接,请执行查询以获取所有链接,例如:

SELECT DISTINCT url FROM your_stuff;

You didn't mention the exact nature of your database, but something like that should work. 您没有提到数据库的确切性质,但是类似的东西应该起作用。

Then loop over it and for each one, use something like cURL to do a HEAD request - this is better than a usual GET request because it's identical, except the server shouldn't return the actual file, so you don't have to download every image just to see if they're there. 然后遍历它,对于每个循环,使用cURL之类的东西来执行HEAD请求-这比通常的GET请求要好,因为它是相同的,除了服务器不应该返回实际文件之外,因此您不必下载每个图像只是为了查看它们是否在那里。 Just do the HEAD request, and confirm that the server answered with a status 200. 只需执行HEAD请求,并确认服务器以状态200回答即可。

This question goes into a little more about the HEAD request in cURL . 这个问题涉及cURL中的HEAD请求

Considering this line in your question: "each with a link to a thumbnails in .jpg format. " 考虑问题中的这一行:“每个都有指向.jpg格式的缩略图的链接。”

You may also try a regexp : ending with .jpg 您也可以尝试使用regexp :以.jpg结尾

SELECT DISTINCT url_column as regex_u, id 
    FROM your_table
    where url_column regexp '\(.jpg)$';

even with like : contains jpg 即使likecontains JPG

    SELECT DISTINCT url_column as like_u, id 
    FROM your_table
    where url_column like '%.jpg%';

Another with instr: 另一个instr:

    SELECT DISTINCT url_column as instr_u , id
    FROM your_table
    where instr(url_column, '.jpg') > 0;

If you want to match a whole ulr 如果你想匹配一个整体ulr

    SELECT DISTINCT url_column as url_u, id 
    FROM your_table
    where url_column regexp '^(https?://|www\\.)[\.A-Za-z0-9/_\-]+\\.(jpg)$'
    ;

Another using Right : 另一个使用Right

    SELECT DISTINCT url_column as right_u, id 
    FROM your_table
    where Right(url_column,4) = '.jpg';

Please check the explain plan for the most efficient solution. 请查看explain plan以获取最有效的解决方案。 LIKE seems to take the longest. LIKE似乎花了最长的时间。

SQL小提琴

SELECT * from table_name where SUBSTRING(url, -3, 3) = 'jpg';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当字符串无效或在数据库中找到时,如何抛出 PDO/MySQL 错误? - How can I throw an error with PDO/MySQL when a string is not valid or found in the database? 如何解析网站以从表中获取链接? - How can I parse a website to get the links out of a table? 我如何在本月使用mysql找出订单明智的礼帽 - How can i find out order wise topper in this month using mysql 从Mysql表中消失的记录,如何找出发生了什么? - Record Disappeared from Mysql Table, How Can I Find Out What Happened? 我如何在 mysql 工作台中查找、检查和更改我的 $db_host、用户、密码、名称? - How can I find out, check and change my $db_host, user, pass, name in mysql workbench? 如何才能通过查询找到网络上的数据库ip位置? (MySQL的) - How can I found out the database ip location on network only with query? (mysql) 如何通过PHP中的while循环回显数据库中的所有MySQL表标题 - How can I echo out all MySQL table titles in a database, by a while-loop in php 我怎样才能在MySQL数据库表中找不到数据的php代码? - How can I write a php code for data can not find in table of MySQL database? 如何找到带有正则表达式的Twitter个人资料链接? - How can I find twitter profile links with a regex? 我如何按类在 html 中查找 URL,制作链接 (php) - How Can i Find URLs in html by class, Make Links (php)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM