Something is wrong with my custom php function

后端 未结 3 1617
粉色の甜心
粉色の甜心 2021-01-23 22:49

Okay so I am trying to create a custom function that will echo a site url inside an iframe for the end user.

The script has to check whether or not the user has already

相关标签:
3条回答
  • 2021-01-23 23:39

    I think the problem is in your code is at the place where you are creating your $siteUrls, $seenUrls arrays. mysqli_fetch_assoc() function will give you a result row as an associative array. So if you want to change some of your code in the while loops. Please chnage this

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

    To

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

    And in the second while loop also. Change this

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

    To

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

    } and try

    0 讨论(0)
  • 2021-01-23 23:43

    i would not run two queries and try to merge the result array. you can use mysql itself to just return the sites that have not been seen yet:

    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
    

    This will return only site_urls from sites, that have no entry in the views table. The LEFT JOIN will join the two tables and for every non-matching row there will be NULL values in the views.site_url, so that is why i am checking for IS NULL.

    Your saving of $urlToShow should work, if you set to $row field content and not $row itself as suggested, but if you want to check what is in the variable, don't use echo use this:

    print_r($urlToShow);
    

    If the variable is an array, you will see it's content then.

    0 讨论(0)
  • 2021-01-23 23:55

    @Azeez Kallayi - you don't need to index array manually.

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

    In addition, you can fetch all result

    $rows = mysqli_fetch_all($result2,MYSQLI_ASSOC);
    foreach($rows as $row){
    echo $row['site_url'];
    }
    
    0 讨论(0)
提交回复
热议问题