因此,我有以下PHP代码:
$tabid = getTabid($module);
if($tabid==9)
$tabid="9,16";
$sql = "select * from field ";
$sql.= " where field.tabid in(?) and";现在,?到底是如何工作的呢?我隐约知道在PHP中,?:是一个三元运算符,但这里没有使用冒号,而且?也是Postgresql查询的一部分。
最后的查询看起来有点像这样:
select * from field where field.tabid in('9,16')那么,问号被$tabid的内容替换了,这是如何发生的呢?
问题是Postgres不接受('9,16')作为整数,它需要像(9,16)一样写,那我该怎么做呢?如何删除撇号?
非常感谢你的帮助,祝你有愉快的一天!
编辑:请求更多代码:
$sql.= " field.displaytype in (1,2,3) and field.presence in (0,2)";后面跟着if语句,我认为这是相关的语句:
if($tabid == 9 || $tabid==16)
{
$sql.= " and field.fieldname not in('notime','duration_minutes','duration_hours')";
}
$sql.= " group by field.fieldlabel order by block,sequence";
$params = array($tabid);
//Running the query.
$result = $adb->pquery($sql, $params);哦,我想我现在明白了,我想它是一个占位符,是pquery函数的一部分:
function pquery($sql, $params, $dieOnError=false, $msg='') {
Stuff
$sql = $this->convert2Sql($sql, $params);
}现在,这似乎是有趣的地方,这是convert2Sql函数的一部分:
function convert2Sql($ps, $vals) {
for($index = 0; $index < count($vals); $index++) {
if(is_string($vals[$index])) {
if($vals[$index] == '') {
$vals[$index] = "NULL";
}
else {
$vals[$index] = "'".$this->sql_escape_string($vals[$index]). "'";
}
}
}
$sql = preg_replace_callback("/('[^']*')|(\"[^\"]*\")|([?])/", array(new PreparedQMark2SqlValue($vals),"call"), $ps);
return $sql;
}我认为问题出在
$vals[$index] = "'".$this->sql_escape_string($vals[$index]). "'";线路。sql_escape_string($str)函数只返回pg_escape_string($str)。
对于超长的编辑,我很抱歉,但我仍然不能通过我恐怕,感谢所有的帮助!
编辑2:我修复了这个问题,只需将$tabid = "9,16"更改为$tabid = array(9,16)即可。我不知道为什么,哦,我还必须删除group by语句,因为Postgresql要求每个字段都放在该语句中。
发布于 2011-09-29 21:31:19
它是预准备语句的位置参数
请参阅:http://php.net/manual/en/function.pg-prepare.php
您实际上并没有‘删除’引号,在执行pg_execute时,您必须将int的SQL数组而不是字符串值传递给参数。
举个例子:
// Assume that $values[] is an array containing the values you are interested in.
$values = array(1, 4, 5, 8);
// To select a variable number of arguments using pg_query() you can use:
$valuelist = implode(', ', $values);
// You may therefore assume that the following will work.
$query = 'SELECT * FROM table1 WHERE col1 IN ($1)';
$result = pg_query_params($query, array($valuelist))
or die(pg_last_error());
// Produces error message: 'ERROR: invalid input syntax for integer'
// It only works when a SINGLE value specified.相反,您必须使用以下方法:
$valuelist = '{' . implode(', ', $values . '}'
$query = 'SELECT * FROM table1 WHERE col1 = ANY ($1)';
$result = pg_query_params($query, array($valuelist));https://stackoverflow.com/questions/7598052
复制相似问题