我错过了什么?我正在构建一个动态查询并准备postgres的查询。它应该工作但是eval语句并没有发挥它的魔力。我错过了什么?
除了eval之外,还有另一种传递带有多个变量的字符串的方法吗?感谢。
简要说明。
$condition = array();
$values = array();
$pgarray = array();
$country = 254;
$city = "Seattle";
$condition[] = " AND city = $";
$values[] = $city;
$pgarray[] = "\$city";
$condition[] = " AND country = $";
$values[] = $country;
$pgarray[] = "\$country";
$as = sizeof($condition);
for ( $x=0; $x<$as; $x++ ) {
$index = $x + 1; //We need to start at one not 0
$qclause = $qclause . $condition[$x] . $index . " ";
}
// This is what qclause equates to.
// AND city = $1 AND country = $2
$pgarray = implode(", ", $pgarray);
// This is what pgarray equated to.
// $city, $country
$query = "Select companyid, city, name from company where 1 = 1 $qclause";
$result = pg_prepare($dbconnect, 'q1', $query);
$runthis = "pg_execute(\$dbconnect, 'q1', array($pgarray) )";
// This is what $runthis equated to.
// pg_execute($dbconnect, 'q1', array($city, $country) )
$result = eval($runthis);
While ..... {
bla bla bla
}
哦,我也做了
$result = eval('return $runthis');
不会产生任何错误。准备会记录在postgres日志文件中,但pg_execute永远不会发生。
我错过了一些非常明显的东西吗?
由于
JT
答案 0 :(得分:0)
已更改
$pgarray[] = "\$city";
$pgarray[] = "\$country";
要
$pgarray[] = $city;
$pgarray[] = $country;
并且
$runthis = "pg_execute(\$dbconnect, 'q1', array($pgarray) )";
要
$result = pg_execute($dbconnect, 'q1', $pgarray );
它解决了问题,或者我应该说出我的错误代码。