2017-03-14 7 views
1

私は何が欠けていますか?動的なクエリを作成し、postgresのクエリを準備しています。それはうまくいくはずですが、eval文はそれを魔法にしません。私は何が欠けていますか?php evalとpg_executeが動作しません! arg

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

を行っています。 prepareはpostgresログファイルに記録されますが、pg_executeは決して起こりません。

本当に明白なものがありませんか?

おかげ

JT

+0

なぜこれで 'eval()'を使用していますか?ハッカーにあなたのサーバーを自分のものと主張するよう誘いますか? * prepared statements *を使用し、 'eval() 'を使用しないでください – Xorifelse

+0

@ Xorifelseこれはevalがやっていることです。準備された文を動的に生成する。問題は、変数の区切りリストをpg_executeの配列に渡すことができないということです。それにかかわらず、例とコードを見ると、目標は準備された義務であることがわかります。最終的には、これらの変数はコード内のテスト変数の代わりに$ _POSTから取得されます。 – user1970839

答えて

0

変更

$pgarray[] = "\$city"; 
$pgarray[] = "\$country"; 

$pgarray[] = $city; 
$pgarray[] = $country; 

そして

$runthis = "pg_execute(\$dbconnect, 'q1', array($pgarray))"; 

TO

TO

$result = pg_execute($dbconnect, 'q1', $pgarray); 

そして、問題を解決したか、私のバグのコードを言うべきですか?

関連する問題