[英]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 即使like
: contains
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
似乎花了最长的时间。
SELECT * from table_name where SUBSTRING(url, -3, 3) = 'jpg';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.