我需要让我的脚本更快,怎么样?

时间:2016-12-20 16:09:10

标签: php curl phpmyadmin

基本上我写的这个代码就像魅力一样,但它有一个不好的方面(它真的很慢)。

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

$account = "localhost:3306";
$username = "root";
$password = "luca170385";
$db = "accounts";

$con = new mysqli($account , $username, $password, $db);
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
    echo "Selected database. <br>";
}

$sql = "SELECT `address` FROM `url` WHERE `code` IS NULL";

if ($result = mysqli_query($con,$sql)) {

    while ($row = mysqli_fetch_array($result)){ 

        $url = ($row["address"]);
        $handle = curl_init($url);

        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($handle, CURLOPT_CONNECTTIMEOUT ,2); 

        $response = curl_exec($handle);

        $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
        echo "HTTP-CODE = $httpCode <br>";

        $sql2 = "UPDATE `url` SET `code` = $httpCode WHERE `address` = '".$url."'";
        $con->query($sql2) or die(mysqli_error($con));
        $con->commit();
        if ($con->error) {
            printf("Errormessage: %s\n", $con->error);
        }
    }
    printf ($row["address"]);
}
mysqli_close($con);
curl_close($handle);
?>

代码的作用是,查看我的数据库,获取HTTP-CODE为NULL的URL并检查HTTP-CODE并将答案放在数据库中,但就像我说的那样有效,有没有办法可能多次运行脚本,但都在同一个脚本?,欢迎所有输入。谢谢!

0 个答案:

没有答案