php mysql查询多个params并获得单独的结果

时间:2022-09-25 16:34:59

Im trying to think of a way to form a query where I can query for each parameter I need, and get the results back on a per param basis. Ive tried a combination of AND/OR but not getting the expected results.

我试图想办法形成一个查询,我可以查询我需要的每个参数,并在每个参数的基础上得到结果。我尝试了AND / OR的组合但没有得到预期的结果。

the basis of the query I wanna do is to the extent of

我想做的查询的基础是

select fb_wall, fb_checkin, phone 
from member_prefs where 
memeberid = 21 
OR memeberid = 22 
OR memeberid = 23 
OR memeberid = 24

The catch is, I need to either ignore when one of the memberid's doesnt have data and keep the query going (or catch the id, so if one is found I can add the row for it in another query after the fact but still keep the query going).

问题是,我需要忽略当其中一个memberid没有数据并保持查询继续(或捕获id,所以如果找到一个,我可以在事后添加另一个查询中的行,但仍保留查询去)。

Right now my dummy data is 3 out of the 4 memberid's have data and one doesn't if I do AND in the query, the query stops and yields no results. Where as if I do the OR i seem to only be returning one of there sets of data.

现在我的虚拟数据是4个memberid中的3个有数据,如果我在查询中执行AND,则一个不存在,查询将停止并且不会产生任何结果。就好像我做OR一样,我似乎只返回其中一组数据。

What I want to do is after the query build an array to pass around that would look like

我想要做的是在查询构建一个数组来传递它看起来像

array(
   [0] array("memberid"=>21, "fb_wall"=>0, "fb_checkin"=>1, "phone"=>0),
   [1] array("memberid"=>22, "fb_wall"=>1, "fb_checkin"=>0, "phone"=>1),
   [2] array("memberid"=>24, "fb_wall"=>1, "fb_checkin"=>1, "phone"=>1)
)

just seem to be having a little issue forming the initial query, and like I said if I could in this case pass 23 else where so I could run an insert command on that id for the same table that would be awesome but not needed for the over all need here.

只是似乎有一个小问题形成初始查询,就像我说,如果我可以在这种情况下传递23其他所以我可以在该id上运行插入命令对于同一个表,这将是很棒但不需要的满足所有需要。

2 个解决方案

#1


2  

How about:

怎么样:

//parameter data
$members = array(
    22 => null,
    23 => null,
    24 => null,
    25 => null
);
//make a comma separated list of ids
$ids = implode(',', array_keys($members));
//generate query text
$sql = '
    select memberid, fb_wall, fb_checkin, phone 
    from   member_prefs 
    where  memberid in ('.$ids.')
';
$result = mysql_query($sql);
while ($row = mysql_fetch_row($result)) {
    $members[$row[0]] = $row;    
}

#2


1  

Start with an array with the relevant keys, and replace the elements as you read the rows.

从包含相关键的数组开始,并在读取行时替换元素。

$ares = array(21 => null, 22 => null, 23 => null, 24 => null);

#1


2  

How about:

怎么样:

//parameter data
$members = array(
    22 => null,
    23 => null,
    24 => null,
    25 => null
);
//make a comma separated list of ids
$ids = implode(',', array_keys($members));
//generate query text
$sql = '
    select memberid, fb_wall, fb_checkin, phone 
    from   member_prefs 
    where  memberid in ('.$ids.')
';
$result = mysql_query($sql);
while ($row = mysql_fetch_row($result)) {
    $members[$row[0]] = $row;    
}

#2


1  

Start with an array with the relevant keys, and replace the elements as you read the rows.

从包含相关键的数组开始,并在读取行时替换元素。

$ares = array(21 => null, 22 => null, 23 => null, 24 => null);