【问题标题】:Something is wrong with my custom php function我的自定义 php 函数有问题
【发布时间】:2015-06-02 09:09:12
【问题描述】:

好的,所以我正在尝试创建一个自定义函数,该函数将在 iframe 中为最终用户回显站点 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?

【问题讨论】:

    标签: php mysql sql arrays


    【解决方案1】:

    @Azeez Kallayi - 您不需要手动索引数组。

    $seenUrls[] = $row2['site_url'];
    

    此外,您可以获取所有结果

    $rows = mysqli_fetch_all($result2,MYSQLI_ASSOC);
    foreach($rows as $row){
    echo $row['site_url'];
    }
    

    【讨论】:

      【解决方案2】:

      我不会运行两个查询并尝试合并结果数组。您可以使用 mysql 本身来返回尚未查看的站点:

      SELECT sites.site_url FROM sites
      LEFT JOIN views ON views.site_url=sites.site_url AND views.user='$currentUsername'
      WHERE sites.site_url IS NOT NULL AND views.site_url IS NULL
      

      这将只返回来自站点的 site_urls,这些站点在视图表中没有条目。 LEFT JOIN 将加入这两个表,并且对于每个不匹配的行,views.site_url 中都会有 NULL 值,这就是我检查 IS NULL 的原因。

      如果您按照建议设置为 $row 字段内容而不是 $row 本身,则 $urlToShow 的保存应该有效,但如果您想检查变量中的内容,请不要使用 echo 使用此:

      print_r($urlToShow);
      

      如果变量是一个数组,那么你会看到它的内容。

      【讨论】:

        【解决方案3】:

        我认为问题在于您的代码是在您创建 $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++;
        

        } 并尝试

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-09-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-21
          • 2011-08-28
          • 1970-01-01
          相关资源
          最近更新 更多