php select 函数生成奇怪的 where 子句

发布于 2024-07-16 08:37:56 字数 1749 浏览 4 评论 0原文

您好,我尝试创建一个函数来生成选择函数。

但是下面的代码

public function select($psTableName, $paFields ="",$paWhere=array())
{
    //initial return value
    $lbReturn = false;
    try
    {
        $lsQuery = "SELECT * FROM `";
        $lsQuery .= $psTableName;
        $lsQuery .= "` ";
        if (!empty($paWhere)){
                $lsQuery .= "WHERE ";
                print_r($paWhere);
                foreach($paWhere as $lsKey => $lsValue)
                {
                    echo $lsKey."<br>";
                    $paWhere[] = $lsKey." = '".mysql_real_escape_string($lsValue)."'";
                }
                $lsQuery .= implode(" AND ", $paWhere);
                //$lsQuery = substr($lsQuery,0,(strlen($lsQuery)-5));
        }

        //echo $lsQuery;
        //execute $lsQuery
        $this->msLastQuery = $lsQuery;
        if(!$this->execute())
        {
            throw new DBException("Select failed, unable to execute query: ".$lsQuery);
        }
        else
        {
            $lbReturn = true;
        }
    }
    catch(DBException $errorMsg)
    {
        ErrorHandler::handleException($errorMsg);
    }
    return $lbReturn;
}

生成了这个sql语句:

SELECT * FROM `persons` WHERE [email protected] AND 2d1cf648ca2f0b2499e62ad7386eccc2 AND 1 AND per_email = '[email protected]' AND per_password = '2d1cf648ca2f0b2499e62ad7386eccc2' AND per_active = '1'

我不知道为什么它首先只显示where子句后面的值,然后返回并显示键=>; 价值观。

有任何想法吗?

Hello I try to create a function to generate select functions.

But the following code

public function select($psTableName, $paFields ="",$paWhere=array())
{
    //initial return value
    $lbReturn = false;
    try
    {
        $lsQuery = "SELECT * FROM `";
        $lsQuery .= $psTableName;
        $lsQuery .= "` ";
        if (!empty($paWhere)){
                $lsQuery .= "WHERE ";
                print_r($paWhere);
                foreach($paWhere as $lsKey => $lsValue)
                {
                    echo $lsKey."<br>";
                    $paWhere[] = $lsKey." = '".mysql_real_escape_string($lsValue)."'";
                }
                $lsQuery .= implode(" AND ", $paWhere);
                //$lsQuery = substr($lsQuery,0,(strlen($lsQuery)-5));
        }

        //echo $lsQuery;
        //execute $lsQuery
        $this->msLastQuery = $lsQuery;
        if(!$this->execute())
        {
            throw new DBException("Select failed, unable to execute query: ".$lsQuery);
        }
        else
        {
            $lbReturn = true;
        }
    }
    catch(DBException $errorMsg)
    {
        ErrorHandler::handleException($errorMsg);
    }
    return $lbReturn;
}

generates this sql statement:

SELECT * FROM `persons` WHERE [email protected] AND 2d1cf648ca2f0b2499e62ad7386eccc2 AND 1 AND per_email = '[email protected]' AND per_password = '2d1cf648ca2f0b2499e62ad7386eccc2' AND per_active = '1'

I don't know why it first shows only the values after the where clause and then goes back and shows the key => values.

Any idea's?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨凡馨 2024-07-23 08:37:56

您可以在循环中重用 $paWhere,以便附加到当前值。 您需要使用一个新的数组:

$result = array();
foreach($paWhere as $lsKey => $lsValue) {
    $result[] = $lsKey . " = '" . mysql_real_escape_string($lsValue) . "'";
}
$lsQuery .= implode(" AND ", $result);

You reuse $paWhere in your loop so you append to the current values. You need to use a fresh array:

$result = array();
foreach($paWhere as $lsKey => $lsValue) {
    $result[] = $lsKey . " = '" . mysql_real_escape_string($lsValue) . "'";
}
$lsQuery .= implode(" AND ", $result);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文