2012-01-04 49 views
0

嘿家伙,我想重复这个声明13次通过改变这个语句在一个循环或什么。 "WHERE spending.SectorID = 1,2,3,4,5,6,7,8,9,10,11,12,13"我怎样才能成功地做到这一点,而无需再次输入此代码12次。我会单独显示数据虽然〜喜欢where sectorid = 1;属于一个表有一个按钮来启动该特定查询PHP Multiarray LOOP WHERE CLAUSE

<?php 
$spendingname= array(); 
$spendingpercent = array(); 
$spendingid = array(); 

mysql_select_db($database_conn2, $conn2); 
$query_Spending = "SELECT CONCAT(spending.SectorID, spending.ExpenditureID) AS 'SpendingID', 
expenditure.ExpenditureName, spending.SpendingPercent, spending.SectorID 
FROM spending 
INNER JOIN expenditure ON spending.ExpenditureID = expenditure.ExpenditureID 
WHERE spending.SectorID = 1"; 
$Spending = mysql_query($query_Spending, $conn2) or die(mysql_error()); 
$totalRows_Spending = mysql_num_rows($Spending); 
while($row_Spending = mysql_fetch_assoc($Spending)) 
{ 
$spendingname[] = $row_Spending['ExpenditureName']; 
$spendingpercent[] = $row_Spending['SpendingPercent']; 
$spendingid[]= $row_Spending['SpendingID']; 
} 
mysql_free_result($Spending); 
?> 
+0

但家伙们将如何识别它?我必须$ {'消费'。$ i} = array(); – 2012-01-04 18:08:15

回答

1
<?php 
    $spendingname= array(); 
    $spendingpercent = array(); 
    $spendingid = array(); 

    mysql_select_db($database_conn2, $conn2); 
    for($x=1;$x<14;$x++) 
    { 
    $query_Spending = "SELECT CONCAT(spending.SectorID, spending.ExpenditureID) AS 'SpendingID', 
    expenditure.ExpenditureName, spending.SpendingPercent, spending.SectorID 
    FROM spending 
    INNER JOIN expenditure ON spending.ExpenditureID = expenditure.ExpenditureID 
    WHERE spending.SectorID = $x"; 
    $Spending = mysql_query($query_Spending, $conn2) or die(mysql_error()); 
    $totalRows_Spending = mysql_num_rows($Spending); 
    while($row_Spending = mysql_fetch_assoc($Spending)) 
    { 
    $spendingname[$x] = $row_Spending['ExpenditureName']; 
    $spendingpercent[$x] = $row_Spending['SpendingPercent']; 
    $spendingid[$x]= $row_Spending['SpendingID']; 
    } 
    mysql_free_result($Spending); 
    } 

//To access and print all elements. 
for($x=1;$x<count($spendingname);$x++) 
{ 
echo "The value for query $x"; 
echo $spendingname[$x]." ".$spendingpercent[$x]." ".$spendingid[$x]."<br><br><br>"; 
} 

?> 
+0

该数组是否会同时存储所有13个值?> – 2012-01-04 18:10:09

+0

13个查询的结果将单独存储在数组元素中。运行这个例子,你会明白的。 – 2012-01-04 18:32:01

1

你只需要包装脚本的主要部分:

for ($n in range(1,13)) { 

for ($n=1; $n<=13; $n++) { 

和恒定1与替换$n

编辑:还是取决于你想要如何呈现这些数据最后你也许可以从修改SQL:

WHERE spending.SectorID = 1 

WHERE spending.SectorID >= 1 AND spending.SectorID <= 13 

WHERE spending.SectorID IN (1,2,3,4,5,6,7,8,9,10,11,12,13) 

(因为MySQL的优化器的工作方式应该同样高效)

0

WHERE spend.SectorID > 0 AND spending.SectorID < 13

0

使用IN条款。具体而言,你可以做一些事情,如WHERE spending.SectorID IN ('" . implode("','", range(1,13)) ...

1

有一件事你不要想做的是做一个单一的查询就足够了13数据库查询。查询一旦与以下WHERE条款,然后遍历结果:

WHERE spending.SectorID <= 13 "; 

- 或 -

WHERE spending.SectorID IN (1,2,3,4,5,6,7,8,9,10,11,12,13)"; 
+0

我想单独显示数据,虽然〜像where sectorid = 1;属于带有按钮的表以启动该特定查询。可能吗?我不想一次显示全部 – 2012-01-04 18:16:48

+0

是的。我建议在['mysql_fetch_array()'](http://php.net/manual/en/function.mysql-fetch-array.php)和['mysql_fetch_assoc()'](http:/ /php.net/manual/en/function.mysql-fetch-assoc.php) – rdlowrey 2012-01-04 18:20:33

1

你可以使用PreparedStatement与参数,然后,你可以循环的作为查询的执行以及结果检索。

但我不明白这样做的原因,除非你想做一些不同的结果。否则,你为什么不使用

WHERE spending.SectorID BETWEEN 1 AND 13 ORDER BY spending.SectorID 

0

如果你想循环一段代码特定的次数,你可以使用for循环。这是一个PHP脚本,可以循环13次。

$loops = 13; 
for ($i = 1; $loops <= $i; $i++) { 
    //Your code// 
} 

你也应该更换 “WHERE spending.SectorID = 1,2,3,4,5,6,7,8,9,10,11,12,13” 与“WHERE spending.SectorID = $ i“,并且这会增加您的SQL语句在每次代码循环时搜索的内容。

希望这会有所帮助。

0
<?php 
$sectorcount = $row_SectorCount['COUNT(*)']; 
//number of rows in database 
mysql_select_db($database_conn2, $conn2); 
for($x=1; $x<=$sectorcount; $x++) 
{ 
${'spendingname'.$x} = array(); 
${'spendingpercent'.$x} = array(); 
${'spendingid'.$x} = array(); 

$query_Spending = "SELECT CONCAT(spending.SectorID, spending.ExpenditureID) AS 'SpendingID', 
expenditure.ExpenditureName, spending.SpendingPercent, spending.SectorID 
FROM spending 
INNER JOIN expenditure ON spending.ExpenditureID = expenditure.ExpenditureID 
WHERE spending.SectorID = $x"; 
$Spending = mysql_query($query_Spending, $conn2) or die(mysql_error()); 
$totalRows_Spending = mysql_num_rows($Spending); 

while($row_Spending = mysql_fetch_assoc($Spending)) 
{ 
${'spendingname'.$x}[] = $row_Spending['ExpenditureName']; 
${'spendingpercent'.$x}[] = $row_Spending['SpendingPercent']; 
${'spendingid'.$x}[]= $row_Spending['SpendingID']; 
} 
mysql_free_result($Spending); 
}     
?> 

我设法解决它但不过花费名是唯一无法检索。消费名是唯一的字符串,其他百分比和ID和所有整数。这就是为什么发生这个问题