php - 改进sql查询中的if语句

标签 php sql

我已经编写了一些工作正常的代码,但非常冗长,希望获得有关如何提高效率的指导。我正在运行不同的 SQL 查询,因为一个查询会返回太多结果;

$sql1 = "select blah blah blah";
$sql2 = "select blah blah blah";
$sql3 = "select blah blah blah";

$response1 = $client->executeSQLQuery(array("sql"=>$sql1));
$response2 = $client->executeSQLQuery(array("sql"=>$sql2));
$response3 = $client->executeSQLQuery(array("sql"=>$sql3));

if( is_array($response1->return->row) )
    {
        foreach($response1->return->row as $numbers)
            {
                echo $numbers->dnorpattern . "<br>";
        $count++;
                }
        }
else
        {
        echo $response1->return->row->dnorpattern . "<br>";
    $count++;
        }

if( is_array($response2->return->row) )
        {
        foreach($response2->return->row as $numbers)
                {
                echo $numbers->dnorpattern . "<br>";
                $count++;
                }
        }
else
        {
        echo $response2->return->row->dnorpattern . "<br>";
        $count++;
        }

if( is_array($response3->return->row) )
        {
        foreach($response3->return->row as $numbers)
                {
                echo $numbers->dnorpattern . "<br>";
                $count++;
                }
        }
else
        {
        echo $response3->return->row->dnorpattern . "<br>";
        $count++;
        }

所以,我想将 if 语句更改为像这样的单个 if 语句;

if( is_array($response1->return->row) )
    {
        foreach($response1->return->row as $numbers)
            {
                echo $numbers->dnorpattern . "<br>";
        $count++;
                }
        }
else
        {
        echo $response1->return->row->dnorpattern . "<br>";
    $count++;
        }

$sql 和 $response 的整数是可以预测的,所以我应该可以使用 for 语句

for($x=1, $x<=3, $x++)
    {
    if( is_array($response[$x]->return->row) )
            {
            foreach($response[$x]->return->row as $numbers)
                    {
                    echo $numbers->dnorpattern . "<br>";
                $count++;
                    }
            }
            else
                {
                echo $response[$x]->return->row->dnorpattern . "<br>";
                $count++;
                }
    }

但这不起作用,有人可以解释我如何正确地增加整数吗?

最佳答案

您可以使用大括号语法:

for($x=1; $x<=3; $x++)
{
    if( is_array(${'response' .$x}->return->row) )
    {
        foreach(${'response' .$x}->return->row as $numbers)
        {
            echo $numbers->dnorpattern . "<br>";
            $count++;
        }
    }
    else
    {
        echo ${'response' .$x}->return->row->dnorpattern . "<br>";
        $count++;
    }
}

或者将您的三个响应对象保存在一个数组中,并使用您原来的语法:

$response = [];
$response[1] = $client->executeSQLQuery(array("sql"=>$sql1));
$response[2] = $client->executeSQLQuery(array("sql"=>$sql2));
$response[3] = $client->executeSQLQuery(array("sql"=>$sql3));

尽管您可能只编写更好的 SQL 查询并大大简化事情

关于php - 改进sql查询中的if语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34746762/

相关文章:

sql - 如何对 GROUP BY 中的多个条件的列求和

mysql - 为什么我的查询对于选择小于、大于与索引的组合很慢?

php - 无法将本地 WordPress 连接到远程 MySQL 服务器

php - composer.phar 更新没有任何反应

mysql - mysql 字母数字排序

mysql - 作业帮助(唯一约束)

php - 将字符串与MySQL中的字段值连接起来

php - HTML 表单发布,没有传递数据

javascript - 具有浮点值的 Html 属性 'Max' 会出现问题

php - PHP 中的登录表单无法使用 google 登录