gpt4 book ai didi

php - 使用mysql查询与php比较数据

转载 作者:行者123 更新时间:2023-11-30 01:09:35 25 4
gpt4 key购买 nike

我正在使用 PHP 和 MySQL 构建一个脚本,以将某个实时页面与旧版本进行比较 - 我通过 md5 对其进行哈希处理并将其与最新版本进行比较。

现在我尝试使用以下命令提取某个页面的最新已知哈希值:

SELECT latest_hash FROM tracked_sites WHERE domain = 'domain.com

这显示了某个“domain.com”的latest_hash的实际内容现在我尝试将其放入有效变量中,以便我可以使用以下内容进行比较:

$latestmd5_sql=(mysqli_query($con,"SELECT latest_hash FROM tracked_sites WHERE domain = 'domain.com'"));

现在,我认为我拥有数据库的实际内容,我正在尝试将其与它进行比较

if ((md5(file_get_contents("https://domain.com/page.html")))==$latestmd5_sql)

但是,由于某种原因,我得到的答案是 False。我尝试使用 echo 或 print_r 打印 $latestmd5_sql var,但它似乎是空的或空数组,我对我做错了什么感到有点困惑,并且很想得到想法。

最佳答案

在您提供的代码中,$latestmd5_sql 将是一个资源,而不是数据库中的值。
您需要从资源“获取”数据才能比较值。

下面是一个示例来说明从 sql 代码到 php 变量的工作流程:

// your sql
$sql="SELECT latest_hash FROM tracked_sites WHERE domain = 'domain.com'";

// the query (returns a resource)
$query = mysqli_query($con,$sql);

// fetch the resulting data (this is the part you're missing)
$result=mysqli_fetch_assoc($query);

// take a look at the data (for debugging purposes)
echo"DATA:<pre>".print_r($result,true)."</pre>";

// compare
if ((md5(file_get_contents("https://domain.com/page.html")))==$result['latest_hash']) {
echo"<p>Match</p>";
} else {
echo"<p>No Match</p>";
}

关于php - 使用mysql查询与php比较数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19551218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com