从数据库返回字符串中查找某个字符

时间:2011-11-09 10:19:40

标签: php

我有以下课程。哪个返回某些字段。我对参考感兴趣。我希望它分割数据,以便在返回查询后引用包含一个'A'然后返回它,如果它包含一个B返回它。单独,所以我能够把它放在表格中显示返回的金额,b返回等。数据按房间列出。

<?php


class CHWIPProgress {

    var $conn;

    // Constructor, connect to the database
    public function __construct() {
        require_once "/var/www/reporting/settings.php";
        define("DAY", 86400);
        if(!$this->conn = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD)) die(mysql_error());
        if(!mysql_select_db(DB_DATABASE_NAME, $this->conn)) die(mysql_error());
    }

    public function ListWIPOnLocation($location) {
    $sql = "SELECT DISTINCT
            `ProgressPoint`.`PPDescription` AS Description ,`Bundle`.`WorksOrder` AS WorksOrder, `Bundle`.`BundleNumber` AS Reference,`TWOrder`.`DueDate` AS DueDate , `Stock`.`Description` as Stock , `Stock`.`ProductGroup` as Group 
        FROM TWOrder,Bundle,ProgressPoint, Stock
                    WHERE `Bundle`.`CurrentProgressPoint`=`ProgressPoint`.`PPNumber`
        AND `TWOrder`.`Colour`=`Bundle`.`Colour`
        AND `TWOrder`.`Size`=`Bundle`.`Size`
                    AND `TWOrder`.`WorksOrderNumber`=`Bundle`.`WorksOrder`
                    AND `TWOrder`.`Product`=`Stock`.`ProductCode`
                    AND `ProgressPoint`.`PPDescription` = '" . $location . "'
        ORDER BY `TWOrder`.`DueDate` ASC";
        mysql_select_db(DB_DATABASE_NAME, $this->conn);
        $result = mysql_query($sql, $this->conn);
                    $num_rows = mysql_num_rows($result);
                    echo "Number of rows : $num_rows"; 
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $return[] = $row;
        }       

        return $return;

    }


}
?>

1 个答案:

答案 0 :(得分:0)

ListWIPOnLocation函数似乎返回一个数组,其中每个项对应于查询中的一行。您可以使用PHP string functions迭代此数组并隔离项目,并可能将其他数组添加。排列数据的方法不止一种,这里有一个例子:

$returned = array();
$returned["A"] = array();
$returned["B"] = array();
foreach($return as $row) {
    if (strpos($row["Reference"], "A") !== FALSE) { // note the !== operator
        $returned["A"][] = $row;
    }
    if (strpos($row["Reference"], "B") !== FALSE) { // note the !== operator
        $returned["B"][] = $row;
    }
}
var_dump($returned);

或者,您可以使用LIKE子句中的MySQL WHERE运算符来检索引用列包含特定字符的行:

WHERE ... AND Reference = LIKE '%A%'