2013-02-22 108 views
0

好吧,我有點困惑,我是否想要做或不可以。mysql在查詢內爆炸

在數據庫中,字段'slot'包含表單中的信息(第一部分:第二部分:第三部分)其中:是分隔符。

我需要比較$matchid第一部分的領域。所以我們說slot1 = 100:200:300

我需要檢查$matchid是否等於第一塊的任何字段的數據庫

$matchid = mysql_fetch_row(mysql_query(
    "SELECT `matchid` 
     FROM `matches` 
     WHERE `winner` = 0 AND 
      (`slot1` OR `slot2` OR `slot3` OR `slot4` OR `slot5` 
      OR `slot6` OR `slot7` OR `slot8` OR `slot9` OR `slot10`) = $matchid" 
    )); 

如果$matchid 100最初和slot1 = 100:200:300比查詢不會返回任何東西,但我需要這是一個匹配。

我不知道我是否正確解釋了這一點,但在這裏我們走了。我試圖使用爆炸,但沒有真正的工作

+1

你爲什麼不把它們放在單獨的列中?把它們放在一列中。 – MacMac 2013-02-22 23:23:08

+0

然後我會有30列而不是10. – DeanR 2013-02-22 23:26:40

+2

[**請不要在新代碼**中使用'mysql_ *'函數](http://bit.ly/phpmsql)。他們不再被維護[並且被正式棄用](http://j.mp/XqV7Lp)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。 – Kermit 2013-02-22 23:28:13

回答

0

,使其與您現有的數據結構的工作,你可以做這樣的事情:

SELECT `matchid` FROM `matches` 
WHERE `winner` = 0 AND (
    `slot1` LIKE "{$matchid}:%" 
    OR `slot2` LIKE "{$matchid}:%" 
    /* etc, etc... */ 
) 

話雖這麼說,你將有一個更清潔的查詢如果您將插槽分隔到自己的表中。 此外,要麼逃避$匹配或使用準備好的語句。

+0

這給出了一個錯誤 – DeanR 2013-02-22 23:40:14

+0

當你用雙引號包裝整個字符串時,大括號會被php轉義......只需用實際變量替換整個「{$ matchid}」位價值,但是你選擇這樣做。 – 2013-02-24 18:09:42

0

嘗試:

$matchid = mysql_fetch_row(mysql_query("SELECT `matchid` FROM `matches` WHERE `winner` = 0 AND   (`slot1` OR `slot2` OR `slot3` OR `slot4` OR `slot5` OR `slot6` OR `slot7` OR `slot8` OR `slot9` OR `slot10`) = ".$matchid)); 

通知點到在連接字符串。通過這種方式,php傳遞了$ matchid的實際值,而在您的代碼中,您傳遞了字符串「$ matchid」。

在這種情況下,$ matchid是一個整數,你必須在查詢中使用strval($ matchid)。

+0

這與我的問題沒有關係 – DeanR 2013-02-22 23:44:35