2010-01-14 88 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 
}