[英]How can I pull multiple rows from a MySQL table and use all of them automatically for the same thing?
Basically, I have multiple URL's stored in a MySQL table. 基本上,我在MySQL表中存储了多个URL。 I want to pull those URLs from the table and have cURL connect to all of them.
我想从表中提取这些URL,并让cURL连接到所有URL。 Currently I've been storing the URL's in the local script, but I've added a new page that I can add and remove them from the database, and I'd like the page to reflect it appropriately.
目前,我一直将URL的内容存储在本地脚本中,但是我添加了一个新页面,可以在数据库中添加和删除它们,并且希望该页面能够正确地反映出来。
Here is what I currently have: 这是我目前拥有的:
$sites[0]['url'] = "http://example0.com ";
$sites[1]['url'] = "http://example1.com";
$sites[2]['url'] = "http://example2.com";
$sites[3]['url'] = "http://example3.com";
foreach($sites as $s)
{
// Now for some cURL to run it.
$ch = curl_init($s['url']); //load the urls and send GET data
curl_setopt($ch, CURLOPT_TIMEOUT, 2); //No need to wait for it to load. Execute it and go.
curl_exec($ch); //Execute
curl_close($ch); //Close it off
}
Now I assume it can't be too amazingly difficult to do, I just don't know how. 现在,我认为这并不是很难做到的,我只是不知道怎么做。 So if you could point me in the right direction, I'd be grateful.
因此,如果您能指出正确的方向,我将不胜感激。 But if you supply me with some code, please comment it appropriately so that I can understand what each line is doing.
但是,如果您向我提供了一些代码,请对其进行适当的注释,以使我能够理解每一行的内容。
Assuming you have a MySQL connection set up, here's the code that will do it for you: 假设您已建立MySQL连接,下面的代码将为您完成此操作:
/* Define $SQL variable being a dataset returned from the database.
The SELECT url FROM urls means "fetch the column 'url' for each row
in the table 'urls' or generate an error" */
$SQL = mysql_query("SELECT url FROM urls") or die(mysql_error());
/* Loop through the dataset and create a new variable which is an
array (loopable) that contains the fetched data */
while($resultSet = mysql_fetch_array($SQL)){
// Now for some cURL to run it.
$ch = curl_init($resultSet['url']); //load the urls and send GET data
curl_setopt($ch, CURLOPT_TIMEOUT, 2); //No need to wait for it to load. Execute it and go.
curl_exec($ch); //Execute
curl_close($ch); //Close it off
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.