简体   繁体   English

我不明白这里是如何使用“?”的

[英]I don't understand how “?” is being used here

So, I have this PHP code: 所以,我有这个PHP代码:

$tabid = getTabid($module);
if($tabid==9)
  $tabid="9,16";
$sql = "select * from field ";
$sql.= " where field.tabid in(?) and";

Now, how exactly does the ? 现在,究竟是怎么回事? work here? 在这里工作? I vaguely understand that in PHP, ?: is a ternary operator, but the colon isn't being used here, and ? 我隐约明白,在PHP中, ?:是一个三元运算符,但这里没有使用冒号,而且? is part of a Postgresql query anyway. 无论如何,它是Postgresql查询的一部分。

The final query looks a bit like this: 最终查询看起来有点像这样:

select * from field where field.tabid in('9,16')

So, the question mark is replaced by the contents of $tabid, how does that happen? 所以,问号被$ tabid的内容所取代,这是怎么发生的?

The issue is that ('9,16') is not accepted by Postgres as an integer, it needs to be written like (9,16) , so how do I do that? 问题是Postgres不接受('9,16')整数,它需要写成(9,16) ,所以我该怎么做? How do I remove the apostrophes? 如何删除撇号?

Thanks a lot for the help, have a good day! 非常感谢您的帮助,祝您有个美好的一天!

edit: More code was requested: 编辑:请求了更多代码:

$sql.= " field.displaytype in (1,2,3) and field.presence in (0,2)";

followed by if statements, I think this is the relevant one: 其次是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);

Oh, I think I see now, I think it is a place holder, a part of the pquery function: 哦,我想我现在看到了,我认为它是占位符,是pquery函数的一部分:

function pquery($sql, $params, $dieOnError=false, $msg='') {
  Stuff
  $sql = $this->convert2Sql($sql, $params);
  }

Now, this is where it seems to get fun, here's part of the convert2Sql function: 现在,这是它似乎变得有趣的地方,这里是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;
}

The problem I think lies in the 我认为的问题在于
$vals[$index] = "'".$this->sql_escape_string($vals[$index]). "'"; line. 线。 The sql_escape_string($str) function just returns pg_escape_string($str) . sql_escape_string($str)函数只返回pg_escape_string($str)

Sorry for the super long edit, but I still haven't been able to get past I am afraid, thanks for all the help! 对不起超长编辑,但我仍然无法过去我害怕,感谢所有的帮助!

Edit 2: I fixed the problem, all it took was changin $tabid = "9,16" to $tabid = array(9,16) . 编辑2:我解决了问题,所有需要的是changin $tabid = "9,16"$tabid = array(9,16) I have no idea why, oh and I also had to remove the group by statement because Postgresql requires every field to be placed in that statement. 我不知道为什么,哦,我还必须删除group by语句,因为Postgresql要求将每个字段放在该语句中。

it is a positional parameter for a prepared statement 它是预准备语句的位置参数

See: http://php.net/manual/en/function.pg-prepare.php 请参阅: http//php.net/manual/en/function.pg-prepare.php

You don't actually 'remove' the quotes, you have to pass SQL array of ints instead of a string value into the parameter when doing pg_execute 你实际上并没有'删除'引号,你必须在执行pg_execute时将int数组的SQL数组而不是字符串值传递给参数

An example: 一个例子:

// 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.

Instead you must use the following approach: 相反,您必须使用以下方法:

$valuelist = '{' . implode(', ', $values . '}'
$query = 'SELECT * FROM table1 WHERE col1 = ANY ($1)';
$result = pg_query_params($query, array($valuelist));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM