gpt4 book ai didi

php - 我的自定义 php 函数有问题

转载 作者:行者123 更新时间:2023-11-29 06:28:36 24 4
gpt4 key购买 nike

好的,所以我正在尝试创建一个自定义函数,它将在 iframe 中为最终用户回显网站 url。

脚本必须检查用户是否已经看过该站点,如果他们已经看过,则不再显示它,而是从数据库中获取另一个站点 url 等。

这是我到目前为止的想法:

function get_urls() {
require 'config.php';
global $con;
global $currentUsername;
$con = mysqli_connect($hostname, $dbusername, $dbpassword, $dbname);
$query = "SELECT site_url FROM sites WHERE site_url IS NOT NULL";
$result = mysqli_query($con, $query);

// Get all the site urls into one array
$siteUrls = array();
$index = 0;
while($row = mysqli_fetch_assoc($result)) {
$siteUrls[$index] = $row;
$index++;
}

$query2 = "SELECT site_url FROM views WHERE user = '$currentUsername' AND site_url IS NOT NULL";
$result2 = mysqli_query($con, $query2);

// Get urls the user has already seen into another array
$seenUrls = array();
$index = 0;
while($row2 = mysqli_fetch_assoc($result2)) {
$seenUrls[$index] = $row2;
$index++;
}

// Compare the two arrays and create yet another array of urls to actually show
$urlsToShow = array_diff($siteUrls, $seenUrls);

if (!empty($urlsToShow)) {
// Echo the url to show for the iframe within browse.php and add an entry to the database that the user has seen this site
foreach ($urlsToShow as $urlToShow) {
echo $urlToShow;
$query = "INSERT INTO views VALUES ('', '$currentUsername', '$urlToShow')";
mysqli_query($con, $query);
break;
}
}
// Show the allSeen file when all the ads are seen
else {echo 'includes/allSeen.php';}
mysqli_free_result($result);
mysqli_close($con);
}

我目前发现了两个错误。首先,$siteUrls 和 $seenUrls 都可以,但是当我使用 array_diff 比较两者时,它返回一个空数组。

其次,脚本不会将站点 url 写入数据库,因为 $urlToShow 是一个数组而不是单个 url?

最佳答案

我认为您的代码中的问题在于您创建 $siteUrls、$seenUrls 数组的地方。 mysqli_fetch_assoc() 函数将为您提供结果行作为关联数组。所以如果你想在 while 循环中改变你的一些代码。请改变这个

while($row = mysqli_fetch_assoc($result)) {
$siteUrls[$index] = $row;
$index++;
}

while($row = mysqli_fetch_assoc($result)) {
$siteUrls[$index] = $row['site_url'];
$index++;
}

并且在第二个 while 循环中也是如此。改变这个

while($row2 = mysqli_fetch_assoc($result2)) {
$seenUrls[$index] = $row2;
$index++;
}

while($row2 = mysqli_fetch_assoc($result2)) {
$seenUrls[$index] = $row2['site_url'];
$index++;

}并尝试

关于php - 我的自定义 php 函数有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29327793/

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