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

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();

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

阅读 6.3k
2 个回答

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

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;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题