Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_match过去在代码中工作,但现在不定义变量中断代码_Php_Mysql_Preg Match - Fatal编程技术网

Php preg_match过去在代码中工作,但现在不定义变量中断代码

Php preg_match过去在代码中工作,但现在不定义变量中断代码,php,mysql,preg-match,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出于某种原因不再定义变量结果。我想知道我是否应该寻找在我所退出的目标站

我目瞪口呆,因为在没有任何更改之后,我的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();
?>

所以答案很简单。curl是以http访问的,而相关网站已经改为https,我没有注意到。谢谢你的帮助。

所以答案很简单。curl是以http访问的,而相关网站已经改为https,我没有注意到。谢谢您的帮助。

如果curl查询遇到一些错误并返回一些没有任何元素的随机内容怎么办?我想可以,但我检查了url等,应该没问题。请注意,他们将url更改为https。让我试试
var\u dump($div->nodeValue)
给出什么?您还可以使用parameterie.idd接受SQL注入。事情就这么简单,而我却没有给予足够的重视。谢谢如果curl查询遇到一些错误并返回一些没有任何元素的随机内容怎么办?我想可以,但我检查了url等,应该没问题。请注意,他们将url更改为https。让我试试
var\u dump($div->nodeValue)
给出什么?您还可以使用parameterie.idd接受SQL注入。事情就这么简单,而我却没有给予足够的重视。谢谢在这种情况下,我建议删除这个问题。作为旁白
foreach($divs As$div){preg_匹配('/Current Rating:\s+(\d+),$div->nodeValue,$results)}如果(!is_null($results[1]){
是一个非常奇怪的设计。我想你必须重写这个。在这种情况下,我建议删除这个问题。作为旁白
foreach($divs As$div){preg_匹配('/Current Rating:\s+){/“,$div->nodeValue,$results);}如果(!is_null($results[1]){
是一个非常奇怪的设计。我想你必须重写这个。