2013-01-03 20 views
0

我有設置在MySQL具有兩列的表:使用in_array比較MySQL數據

  • ID(AUTO_INCREMENT)
  • recentlyplayed(VARCHAR,建立如 「1 44」 或 「2 140」等)

我想獲取MySQL數據到一個數組,並將它與MySQL以外定義的東西進行比較。但由於某種原因,它似乎不起作用。這裏是我的代碼:

$ whichPlaylist是1個字符INT,$ num是3個字符INT。

//Note: These values are actually received through file_get_contents('textfile.txt'); 
$whichPlaylist = "1"; 
$num = "44"; 

//Combine playlist number and video number 
$joint = $whichPlaylist. "" .$num; 


//Insert joint var into Mysql database 
mysql_query("INSERT INTO recentlyplayed (numplayed) VALUES('$joint') "); 


$query = "SELECT * FROM recentlyplayed"; 
$result = mysql_query($query) or die ("no query"); 

while($row = mysql_fetch_array($result)) 
{ 

$stored[] = $row['1']; 
} 


if (in_array($joint, $stored['1'])){ 
$reroll = 1; 
} 

echo $reroll; 

即使$關節的相同值在MySQL表,$重擲不會回1.莫非是因爲這種額外的間隔插入$關節變量是什麼時候?實際上,我從數字後面的一個新行的文本文件中獲取$ whichPlaylist和$ num。任何幫助表示讚賞。 :)

+0

'$ row ['1']'和'$ stored ['1']'是各個數組元素,鍵值爲字符串值'1'。你可能的意思是'$ row [1]'和'$ stored',但是你的代碼有其他許多錯誤。不要使用'ext/mysql';將你的變量作爲參數傳遞給預處理語句;命名您希望在您的選擇列表中獲取的列,而不是使用'*'通配符;儘可能避免將整個結果集加載到數組中(例如,在可能的情況下將某些處理移入數據庫,或者在抓取時對每條記錄執行處理);儘快退出循環。 – eggyal

+0

'mysql'-擴展名已過時,不再維護,將被標記爲已棄用PHP5.5。使用'PDO_MySQL'或'MySQLi'。 http://php.net/en/mysql-connect – KingCrunch

回答

0

在if語句後移動While End括號,並查看它是否有效。

0

試試這個代碼在你的腳本:

$stored = array(); 
while($row = mysql_fetch_array($result)) { 
    $stored[] = $row[1]; 
} 

if (in_array($joint, $stored)){ 
    $reroll = 1; 
} 

該數組是$存儲(存儲不是$ [ '1'])。