CI query()方法的名字占位符

发布于 2022-09-04 20:24:44 字数 471 浏览 34 评论 0

PHP ci框架的query方法支持 ? 占位符

$sql="select id,name,phone from class where id>? and id<?";
return $this->db->query($sql,array($idStart,$idEnd))->result_array();

难道不支持名字占位符吗?

$sql="select id,name,phone from class where id>:idStart and id<:idEnd";
return $this->db->query($sql,array('idStart'=>$idStart,'idEnd'=>$idEnd))->result_array();

结果提示语法错误,请问诸位是不支持还是我的用法错误
谢谢各位了

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

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

发布评论

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

评论(2

紅太極 2022-09-11 20:24:44

‘idStart’和‘idEnd’前面都加上一个‘:’试一下。

苯莒 2022-09-11 20:24:44

CI不支持名字占位符噢

query的源码如下system/database/DB_driver.php

/**
 * Bind marker
 *
 * Character used to identify values in a prepared statement.
 *
 * @var string
 */
public $bind_marker     = '?';

/**
 * Compile Bindings
 *
 * @param    string    the sql statement
 * @param    array    an array of bind data
 * @return    string
 */
public function compile_binds($sql, $binds)
{
    if (empty($this->bind_marker) OR strpos($sql, $this->bind_marker) === FALSE)
    {
        return $sql;
    }
    elseif ( ! is_array($binds))
    {
        $binds = array($binds);
        $bind_count = 1;
    }
    else
    {
        // Make sure we're using numeric keys
        $binds = array_values($binds);
        $bind_count = count($binds);
    }

    // We'll need the marker length later
    $ml = strlen($this->bind_marker);

    // Make sure not to replace a chunk inside a string that happens to match the bind marker
    if ($c = preg_match_all("/'[^']*'|\"[^\"]*\"/i", $sql, $matches))
    {
        $c = preg_match_all('/'.preg_quote($this->bind_marker, '/').'/i',
            str_replace($matches[0],
            str_replace($this->bind_marker, str_repeat(' ', $ml), $matches[0]),
            $sql, $c),
            $matches, PREG_OFFSET_CAPTURE);

        // Bind values' count must match the count of markers in the query
        if ($bind_count !== $c)
        {
            return $sql;
        }
    }
    elseif (($c = preg_match_all('/'.preg_quote($this->bind_marker, '/').'/i', $sql, $matches, PREG_OFFSET_CAPTURE)) !== $bind_count)
    {
        return $sql;
    }

    do
    {
        $c--;
        $escaped_value = $this->escape($binds[$c]);
        if (is_array($escaped_value))
        {
            $escaped_value = '('.implode(',', $escaped_value).')';
        }
        $sql = substr_replace($sql, $escaped_value, $matches[0][$c][1], $ml);
    }
    while ($c !== 0);

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