PHP Multiarray LOOP WHERE CLAUSE

时间:2012-01-04 17:50:06

标签: php mysql sql arrays function

嘿伙计们,我想通过在一个循环或其他内容中更改此声明来重复此声明13次。 "WHERE spending.SectorID = 1,2,3,4,5,6,7,8,9,10,11,12,13"如何在不输入此代码12次的情况下成功完成此操作。我会单独显示数据虽然〜像where sectorid = 1;属于一个带有按钮的表来启动该特定查询

<?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);
?>

8 个答案:

答案 0 :(得分: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的优化器的工作方式应该同样有效)

答案 1 :(得分: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>";
}

?>

答案 2 :(得分:1)

想要做的一件事是,当单个查询就足够时,进行13次数据库查询。使用以下WHERE子句查询一次,然后迭代结果:

WHERE spending.SectorID <= 13 ";

-OR -

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

答案 3 :(得分:1)

您可以使用带参数的preparedStatement,然后循环执行查询以及结果检索。

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

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

使用IN子句。具体来说,您可以执行WHERE spending.SectorID IN ('" . implode("','", range(1,13)) ...

之类的操作

答案 6 :(得分:0)

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

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

您还应将“WHERE spend.SectorID = 1,2,3,4,5,6,7,8,9,10,11,12,13”替换为“WHERE spend.SectorID = $ i”,并且这会增加每次代码循环时SQL语句搜索的内容。

希望这有帮助。

答案 7 :(得分: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);
}                   
?>

我设法解决了它,但是,只有一个人无法检索。 spendname是唯一的字符串,其他百分比和id以及所有整数。是这个问题出现的原因