创建灵活的更新查询

发布于 2024-07-15 09:41:39 字数 1056 浏览 8 评论 0原文

我正在尝试创建灵活的更新查询。 我现在有这样的东西:

            $lsQuery = "UPDATE `";
        $lsQuery .= $psTableName;
        $lsQuery .= " SET ";
        foreach($psValues as $lsKey => $lsValue)
        {
            $lsQuery .= $lsKey;
            $lsQuery .= " = '";
            $lsQuery .= $lsValue;
            $lsQuery .= "' AND ";
        }
        $lsQuery .= "` ";
        $lsQuery .= "WHERE ";
        if(isset($psWhere)){
            foreach($psWhere as $lsKey => $lsValue)
            {
                $lsQuery .= $lsKey;
                $lsQuery .= " = '";
                $lsQuery .= $lsValue;
                $lsQuery .= "' AND ";
            }
        }
        $lsQuery = substr($lsQuery,0,(strlen($lsQuery)-5));

但是当我在屏幕上打印我的查询时,我得到类似的东西:

UPDATE persons SET per_password = '2a6445462a09d0743d945ef270b9485b' AND WHERE per_email = '[email protected]'

我怎样才能摆脱这个额外的“AND”?

I'm trying to create an flexible update query. I have now something like this:

            $lsQuery = "UPDATE `";
        $lsQuery .= $psTableName;
        $lsQuery .= " SET ";
        foreach($psValues as $lsKey => $lsValue)
        {
            $lsQuery .= $lsKey;
            $lsQuery .= " = '";
            $lsQuery .= $lsValue;
            $lsQuery .= "' AND ";
        }
        $lsQuery .= "` ";
        $lsQuery .= "WHERE ";
        if(isset($psWhere)){
            foreach($psWhere as $lsKey => $lsValue)
            {
                $lsQuery .= $lsKey;
                $lsQuery .= " = '";
                $lsQuery .= $lsValue;
                $lsQuery .= "' AND ";
            }
        }
        $lsQuery = substr($lsQuery,0,(strlen($lsQuery)-5));

But when I print my query on the screen I get something like:

UPDATE persons SET per_password = '2a6445462a09d0743d945ef270b9485b' AND WHERE per_email = '[email protected]'

How can I get rid of this extra 'AND'?

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

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

发布评论

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

评论(3

柒夜笙歌凉 2024-07-22 09:41:39

我可能会从以下开始:

function update($table, $set, $where) {
  $change = array();
  foreach ($set as $k => $v) {
    $change[] = $k . ' = ' . escape($v);
  }
  $conditions = array();
  foreach ($where as $k => $v) {
    $conditions[] = $k . ' = ' . escape($v);
  }
  $query = 'UPDATE ' . $table . ' SET ' .
    implode(', ', $change) . ' WHERE ' .
    implode(' AND ', $conditions);
  mysql_query($query);
  if (mysql_error()) {
    // deal with it how you wish
  }
}

function escape($v) {
  if (is_int($v)) {
    $v = intval($v);
  } else if (is_numeric($v)) {
    $v = floatval($v);
  } else if (is_null($v) || strtolower($v) == 'null') {
    $v = 'null';
  } else {
    $v = "'" . mysql_real_escape_string($v) . "'";
  }
  return $v;
}

I'd probably start with:

function update($table, $set, $where) {
  $change = array();
  foreach ($set as $k => $v) {
    $change[] = $k . ' = ' . escape($v);
  }
  $conditions = array();
  foreach ($where as $k => $v) {
    $conditions[] = $k . ' = ' . escape($v);
  }
  $query = 'UPDATE ' . $table . ' SET ' .
    implode(', ', $change) . ' WHERE ' .
    implode(' AND ', $conditions);
  mysql_query($query);
  if (mysql_error()) {
    // deal with it how you wish
  }
}

function escape($v) {
  if (is_int($v)) {
    $v = intval($v);
  } else if (is_numeric($v)) {
    $v = floatval($v);
  } else if (is_null($v) || strtolower($v) == 'null') {
    $v = 'null';
  } else {
    $v = "'" . mysql_real_escape_string($v) . "'";
  }
  return $v;
}
情感失落者 2024-07-22 09:41:39

如果您想保留现有代码。

$lsWhere = array();
foreach($psWhere as $lsKey => $lsValue)
{
    $lsWhere[] = $lsKey." = '".mysql_real_escape_string($lsValue)."'";
}
$lsQuery .= join(" AND ", $lsWhere);

If you want to keep your existing code.

$lsWhere = array();
foreach($psWhere as $lsKey => $lsValue)
{
    $lsWhere[] = $lsKey." = '".mysql_real_escape_string($lsValue)."'";
}
$lsQuery .= join(" AND ", $lsWhere);
橘寄 2024-07-22 09:41:39

这不是一个令我特别自豪的解决方案,但您始终可以添加 $lsQuery .= 'someField=someField'

It's not a solution I'm particularly proud of, but you can always add $lsQuery .= 'someField=someField'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文