简体   繁体   English

如何找出使用PHP匹配的多语句mysql_query的哪一部分?

[英]How to find out what part of a multi-statement mysql_query was matched using PHP?

I have a query in PHP. 我在PHP中有一个查询。 It's much more complex, but for clarity let's say the query is: 它要复杂得多,但为清楚起见,我们假设查询是:

$query = "SELECT * FROM my_table WHERE (foo = 'bar') OR (foo = 'bar_bar') OR (foo = 'bar_bar_bar')";
$result = mysql_query($query);

Is there a way to find out for each row of $result if it matched either foo = 'bar' , foo = 'bar_bar' or foo = 'bar_bar_bar' ? 是否有一种方法可以找出$result每一行是否匹配foo = 'bar'foo = 'bar_bar'foo = 'bar_bar_bar'

I know I could simply do this: 我知道我可以这样做:

while($row = mysql_fetch_assoc($result) {
    switch($row['foo']) {
        case 'bar' :
        // we matched 'bar'
        break;
        case 'bar_bar' :
        // we matched 'bar_bar'
        break;
        case 'bar_bar_bar' :
        // we matched 'bar_bar_bar'
        break;
    }
}

But that's inefficient as you're basically querying your result, plus many queries and results would be way too complex to do it this way. 但这效率很低,因为您基本上是在查询结果,再加上许多查询和结果,以这种方式执行起来太复杂了。

So, is there an easy and efficient way to find, for example an index of what statement of the query was matched, or is my best bet to split up the query in to separate queries? 因此,有没有一种简单有效的方法来查找(例如,查询的哪个语句匹配的索引),或者我最好的选择是将查询拆分为多个单独的查询?

Thanks. 谢谢。

Why do you have to check that ? 为什么要检查一下? You can directly write the required function based on the result . 您可以根据结果直接编写所需的函数。 In the function you are gonna write instead of hard coding it to a name just give 在函数中,您要编写而不是将其硬编码为名称,

$row['var_name'] . $row['var_name']

you have to just select foo from my table 你只需要从我的表中选择foo

$query = "SELECT foo FROM my_table WHERE (foo = 'bar') OR (foo = 'bar_bar') OR (foo = 'bar_bar_bar')";
$result = mysql_query($query);

//You can simply use mysql_fetch_array() instead of mysql_fetch_assoc //您可以简单地使用mysql_fetch_array()代替mysql_fetch_assoc

while($row = mysql_fetch_array($result) {
    switch($row['foo']) {
        case 'bar' :
        // we matched 'bar'
        break;
        case 'bar_bar' :
        // we matched 'bar_bar'
        break;
        case 'bar_bar_bar' :
        // we matched 'bar_bar_bar'
        break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM