preg_match曾经在代码中工作,但现在没有定义变量 - 破坏的代码

时间:2017-09-23 20:22:05

标签: php mysql preg-match

我傻眼了,因为没有任何变化,我的MySQL表更新代码已停止工作,打印出以下错误:

[23-Sep-2017 20:04:17 UTC] PHP Notice:  Undefined variable: results in /home/harabla/public_html/testi/update.php on line 46

如上所述,我没有改变代码的任何部分,但我唯一能想到的是第46行的preg_match由于某种原因不再定义变量结果。我想知道我是否应该在目标网站上寻找我正在进行的更改,或者是否还有其他我不知道的事情。

以下是代码的其余部分:

<?php
$servername = "myserver";
$username = "user";
$password = "pass";
$dbname = "dbname";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: $conn->connect_error");
} 


$sql = mysqli_query($conn, "SELECT id from users");
$userinfo = array();

while ($row_user = $sql->fetch_assoc()) {
    $userinfo[] = $row_user;
}

foreach ($userinfo as $user) {
    $url = "http://www.pdga.com/player/$user[id]";
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $html = curl_exec($ch);
    curl_close($ch);


    $dom = new DOMDocument();
    @$dom->loadHTML($html);

    $xpath = new DomXPath($dom);
    $class = 'current-rating';
    $divs = $xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $class ')]");

    foreach ($divs as $div) {
        preg_match('/Current Rating:\s+(\d+)/', $div->nodeValue, $results);
    }


    if (!is_null($results[1])){
        $sql = "UPDATE users SET rating = $results[1] WHERE id = $user[id]";
    } else {
        $sql = "UPDATE users SET rating = '0' WHERE id = $user[id]";
    }

    if ($conn->query($sql) === TRUE) {
        echo "Record updated successfully <br>";
    } else {
        echo "Error updating record <br> $conn->error";
    }

    unset($results);
}

$conn->close();
?>

提前感谢您的协助。

1 个答案:

答案 0 :(得分:0)

所以答案很简单。卷曲访问为http,而有问题的网站已更改为https,我没有注意到。谢谢你的帮助。