2015-04-01 101 views
0

這是我的網站的自定義函數,我需要在iframe中顯示不同的URL。我只想向用戶顯示每個網址一次,因此我首先創建一個可用的URL的數組,然後是一個已經看到的URL的數組,然後最後比較這兩個URL以創建一個用於顯示URL的新數組。然後我需要將該數組的第一個元素回顯到iframe中。php mysqli_query函數似乎運行兩次

的問題似乎在於在foreach函數中,我執行了進入數據庫$entry = "INSERT INTO views VALUES ('', '$currentUsername', '$urlToShow')";

我有許多不同的方法試過了,但是不管我做什麼,我得到兩個URL從$urlsToShow陣列加入到數據庫,並且只有其中一個會在我的iframe中回顯。結果是每個其他站點完全跳過,用戶從不會看到它們。

我有print_r'd $urlsToShow,以確保它是一個數組,我也這樣做,以確保$urlToShow不是一個數組。

我甚至不知道這是一個PHP的問題更多...

下面是代碼:

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['site_url']; 
      $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['site_url']; 
      $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) { 
      $entry = "INSERT INTO views VALUES ('', '$currentUsername', '$urlToShow')"; 
      mysqli_query($con,$entry); 
      echo $urlToShow; 
      break; 
     } 
    } 
    // Show the allSeen file when all the ads are seen 
    else {echo 'includes/allSeen.php';} 
    mysqli_free_result($result); 
    mysqli_close($con); 
} 

編輯

我把意見from here和使功能更小,但同樣的結果仍在發生。兩個網址會被添加到視圖表中,但只有兩個網址中的第一個會被回傳到iframe中。

function get_urls() { 
    require 'config.php'; 
    global $con; 
    global $currentUsername; 
    $con = mysqli_connect($hostname, $dbusername, $dbpassword, $dbname); 
    $query = "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"; 
    $result = mysqli_query($con,$query); 
    $urlsToShow = mysqli_fetch_assoc($result); 
    if (!empty($urlsToShow)) { 
     $urlToShow = $urlsToShow['site_url']; 
     echo $urlToShow; 
     $entry = "INSERT INTO views VALUES ('', '$currentUsername', '$urlToShow')"; 
     mysqli_query($con,$entry); 
    } 
    else {echo 'includes/allSeen.php';} 
    mysqli_free_result($result); 
    mysqli_close($con); 
} 

編輯2

我也曾嘗試用PHP的ReadFile();函數,因爲我認爲這可能是導致這種情況的iframe。但不,結果相同。也許我打電話功能錯了?

<iframe src="<?php get_urls();?>"/> 

編輯3

我也曾嘗試echo'ing與urlToShow沿着函數中的iframe的html標籤,看看是否有差別。不,它沒有。

有人嗎?

回答

1

花了我兩天的時間終於找出了這個問題的真正原因。顯然Firefox和Chrome中都存在一個錯誤(因爲我爲了以防萬一都對它進行了測試)。

tldr;

如果離開<link>標籤href空的腳本將運行兩次

例(不這樣做):

<link rel="Shortcut Icon" href="" type="image/x-icon"/>

href需要有一個價值!

你可以閱讀更多關於它在這裏:https://bugzilla.mozilla.org/show_bug.cgi?id=489115

+0

所以我們遇到了一個問題,我不知道如何解決(MODS,請參考)。我們針對實際遇到的問題提供了一種成功且可重複的解決方案,但最初的問題不包括實際的違規代碼。僅僅將違規代碼添加到OP中就足夠了(可能使先前的評論/答案無效)?是否應該重新修改OP?應該刪除主題答案(一旦添加了違規代碼)?這應如何處理? (@ynef,不要做我說的任何話,先來看看反饋吧)。 – Mike 2015-04-13 18:02:11

1

您有權訪問PHP錯誤日誌嗎?我調試的路徑將是雙重的:

  1. 添加static $calls = 0;到函數的頂部,只是在它之下error_log('Called '.(++$called).' time(s).');添加一行。這會告訴你它是否作爲同一請求的一部分被調用了多次。

  2. 如果您確定它在同一個請求中被多次調用,請嘗試在debug_backtrace中記錄第一個條目。

放置在函數的開始下列登錄功能從調用的位置。

$trace = current(debug_backtrace()); 
error_log("Called by {$trace['file']}:{$trace['line']}"); 

合起來,這兩樣東西應該可以識別你的問題。

+0

我嘗試添加debug_print_backtrace();在mysqli_query之前,我認爲它只運行一次,因爲結果是#0 get_urls在...... – ynef 2015-04-02 12:05:46

+0

處調用這個關鍵是利用錯誤日誌。我懷疑這是在輸出發送後或另一個請求後被調用的。 – Mike 2015-04-03 17:11:42

+0

我設法自己找出問題所在。謝謝您的幫助。對此,我真的非常感激! – ynef 2015-04-06 17:46:56