2010-01-14 108 views
0

以下是我在MySQL表達式中包含PHP變量的失敗嘗試。用1替換變量會導致打印結果。任何幫助將不勝感激。

$query = " 
SELECT name FROM teams 
WHERE id = '$shooterID'"; 

$shooters = mysql_query($query) 
or die(mysql_error()); 

$i = 0; 
while($shooter = mysql_fetch_array($shooters)) { 
echo $shooter[$i]; 
$i++; 
} 

$shooters = mysql_query(" 
SELECT name FROM teams 
WHERE id = '$shooterID'") 
or die(mysql_error()); 

$i = 0; 
while($shooter = mysql_fetch_array($shooters)) { 
echo $shooter[$i]; 
$i++; 
} 

感謝


試圖利用此方法還沒有完全解決的問題(雖然再次感謝)。這裏是我的修訂努力,進一步上下文一起(我不需要,因爲它是直接從另一個查詢來清理數據。

$shooters = mysql_query(" 
SELECT * FROM events JOIN teams 
on events.shooter = teams.id 
") or die(mysql_error()); 

$i = 0; 
while($results = mysql_fetch_array($shooters)) { 
    $shooterIDs[$i] = $results[0]; 
    $i++; 
} 

//var_dump($shooterIDs); == array(1) { [0]=> string(1) "1" } 

$query = " 
SELECT name FROM teams 
WHERE id = '".$shooterID[0]."'"; 

$shooters = mysql_query($query) 
or die(mysql_error()); 

while($shooter = mysql_fetch_array($shooters)) { 
echo $shooter[0]; 
} 

原來我的最後一次嘗試是在缺少一個「S」變量namee $ shooterIDs [0]愚蠢的錯誤。有可能其他人也已經與所有的幫助,謝謝!

回答

4

查詢是不是你的問題,輸出的是:

這是錯誤

$i = 0; 
while($shooter = mysql_fetch_array($shooters)) { 
echo $shooter[$i]; 
$i++; 
} 

這是正確

while($shooter = mysql_fetch_array($shooters)) { 
echo $shooter[0]; 
} 

此外

只要確保您正確地清理輸入內容,如果你想包含這樣的變量。例如:

$shooterID = (int)$_GET['shooter_id']; 

,迫使數要麼是0,如果它不是一個數字或1,如果他們在shooter_id[]=somthing通過,但它永遠是一個SQL注入字符串。

+3

+1用於正確的消毒輸入。 – 2010-01-14 05:13:05

0

已經已經解決了不要把周圍$ shooterID單引號的查詢中。

你可能也想要這樣的東西:

while($shooter = mysql_fetch_array($shooters)) { 
echo $shooter[0]; 
$i++; 
} 

打印結果。

+0

我已經試過了恐怕 – 2010-01-14 04:43:47

+0

@如果你想通過列名引用它,John會想'mysql_fetch_assoc'。 – 2010-01-14 04:45:34

+0

@doug我以爲同樣的事情,然後發現這一點,並懶得找其他任何東西http://www.tizag.com/mysqlTutorial/mysqlfetcharray.php你可以看到它的使用錯誤,我認爲。 – 2010-01-14 04:46:20

0

你試過:

$query = "SELECT name FROM teams WHERE id = '" . $shooterID . "'"; 

另外,我沒有看到你定義$shooterID隨時隨地確保你定義它。
I.E.

$shooterID = 0; 

此外,

$i = 0; 
while($shooter = mysql_fetch_array($shooters)) { 
    echo $shooter[$i]; 
    $i++; 
} 

應該是

while($shooter = mysql_fetch_array($shooters)) { 
    echo $shooter[0]; 
} 

while($shooter = mysql_fetch_array($shooters)) { 
    echo $shooter['name']; 
} 

while($shooter = mysql_fetch_object($shooters)) { 
    echo $shooter->name; 
} 
0

嘗試這樣的事情(添加爲清楚起見,評論):

// Create the query, assuming $shooterID is an integer 
$query = "SELECT name FROM teams WHERE id = '{$shooterID}'"; 

// Execute query 
$shooters = mysql_query($query); 

// Check result 
if (!$shooters) { die(mysql_error()); } 

// Iterate through rows 
while ($shooter = mysql_fetch_array($shooters)) { 
    // To display the entire $shooter array 
    print_r($shooter); 

    // To select the first item in $shooter array (no matter what it is) 
    echo $shooter[0]; 

    // To specifically select the name field in $shooter array 
    echo $shooter['name']; 

    // To iterate over the $shooter array and display all fields 
    // This will only be the name, unless you change the query to SELECT * FROM, 
    // in which case this will return all fields in the table 
    foreach ($shooter as $field) { 
    echo $field; 
    } 
} 
0

此外,你可能想在你的輸出部分分離:

while ($shooter = mysql_fetch_array($shooters)) 
{ 
    echo $shooter[0], "\n"; // or '<br>' if outputting to html 
}