有没有更好的方法来获取PHP中的mysql_query()自定义重复和排序?

时间:2012-08-17 22:05:37

标签: php mysql

我很惊讶,目前还没有答案......它告诉我,我做的事情从根本上是错误的。或者其他人都是。

假设我有一个可能重复的值的数组,它们是作为POST请求进入客户端排序的,目标是返回由这些值索引的MySQL的结果集,并且与这些值的顺序相同< em>并保留冗余 !!!

所以,如果值是:1,2,3,2,3,4,5,5,5

期望的结果是:

+----+-------+
| id | value |
+----+-------+
|  1 | one   |
|  2 | two   |
|  3 | three |
|  2 | two   |
|  3 | three |
|  4 | four  |
|  5 | five  |
|  5 | five  |
|  5 | five  |
+----+-------+

以下代码似乎有效,但基督,认真吗?我必须滥用array_intersect()并且有两个不同的循环,其中一个是嵌套的,只是为了使一个简单的结果集得到排序并以与输入数组相同的模式重复?这不可能是它应该如何完成的。

function repQuery($inids,$incol,$querystring,$emptyval = "NULL"){
    # incol is the name of a column in the query
    # inids is an array within which the value of incol must be found
    # querystring is a query of the form...
    # "select FOO from BAR where %s in (%s) BLAH BLAH BLAH"
    # The first %s will be replaced by $incol and the second by $inids
    $qq = mysql_query(sprintf($querystring,$incol,implode(",",$inids))) or die(mysql_error());
    $nf = mysql_num_fields($qq); $dummyrow = array();
    for($ii = 0; $ii < $nf; $ii+= 1){
        $dummyrow[mysql_field_name($qq,$ii)] = $emptyval;
    };
    $out = array_fill(0,count($inids),$dummyrow);
    while($rr = mysql_fetch_assoc($qq)){
        foreach(array_keys(array_intersect($inids,array($rr[$incol]))) as $ii) {
            $out[(int)$ii] = $rr;
        }
    }
    return $out;
}

我的问题是:是否有一些可接受的模式或PHP数组命令或其他我忽略的东西?在R中,上述内容将是一行的。

2 个答案:

答案 0 :(得分:1)

虽然如果它是MySQL中的一个功能会很棒,但我认为没有办法将ORDER BY用于重复模式(你可以通过{{1}获得“自定义模式” },但不完全在那里。

我不确定PHP中的任何特殊方法,但使用临时数组可能会减轻头痛:

ORDER BY FIELD()

这将使用行的ID作为数组索引,将function repQuery($inids,$incol,$querystring,$emptyval = "NULL"){ $qq = mysql_query(sprintf($querystring,$incol,implode(",",$inids))) or die(mysql_error()); // build a temporary array of all of the results - the row's "ID" is the array index $tmpResults = array(); while ($row = mysql_fetch_assoc($qq)) { $tmpResults[$row['id']] = $row; } // iterate through the order/list of IDs and insert each result one after the other $out = array(); foreach ($inids as $id) { $out[] = $tmpResults[$id]; } return $out; } 数组中的每个SQL结果(每个ID一个)放置。下一步是遍历$tmpResults列表,其中包含要复制的模式,并将相应的记录结果插入$inids数组。

答案 1 :(得分:0)

我使用PDO并从下面的PDO获取数据库中的数据。因此,如果您因为某种原因无法通过PDO获取数据,请不要费心阅读。 (因为我使用PDO,所以我不熟悉mysql_ *命令。)

如果是我,我会在传递给MySQL之前使$ inids唯一(array_unique)并更改SQL语句以在SELECT语句中包含ID。类似的东西:

select %s, FOO from BAR where %s in (%s)

然后sprintf成为

$tempIds = array_unique($inids);
sprintf($querystring,$incol,$incol,implode(",",array_unique($tempIds)))

PDO就像是

$db->prepare(sprintf($querystring,$incol,$incol,implode(",",$tempIds)));
$db->execute(array());
$results = $db->fetchAll();

因此,对于您的组织结构,我会使用以下内容:

$ids = array();
$srch = array();
foreach ($results AS $key=>$dbData) {
  $ids[] = $dbData['ID'] . ":" . $dbData['FOO'];
  $srch[] = (string) $dbData['ID'];
} 

$out = str_replace($srch, $ids, $inids);

我想有更好的方法可以做到这一点,但我还没有想出来。假设$ inids是一个类似的数组,这应该可以工作:

$inids = array('1', '2', ...);

如果值不是字符串,则在传递给str_replace之前需要将它们转换为字符串。然后当你想在你的页面或某个地方输出它们时,你会这样做:

foreach ($out AS $key=>$str) {
$data = explode(':', $str);
  echo '<br>' . $data[0] . \t . '|' . \t . $data[1];
}

随意使用echo语句。