2016-06-09 54 views
1

PHP中的Where子句有一個問題 - MongoDB。PHP中的哪裏子句ARRAY - MongoDB

我在這個論壇搜索並獲得一些信息。

我的代碼是:

$condizioneMongoDB = "array('chrom'=>'chr7')"; 
$whereClause = eval("\$str = \"$condizioneMongoDB\";"); 
$retval = $collection->distinct("p_id", $whereClause); 

where子句中完全被忽略。

這個靜態代碼工作:

$retval = $collection->distinct("p_id", array('chrom'=>'chr7')); 

回答

1

$ whereClause =的eval( 「\ $海峽= \」 $ condizioneMongoDB \ 「;」);

它等於$str = "array('chrom'=>'chr7')"
但你要$str = array('chrom'=>'chr7')

你應該省略引號這一點。

另外,您需要從eval中返回值才能使用它,或者您可以直接使用eval中的變量。

$condizioneMongoDB = "array('chrom'=>'chr7')"; 
$whereClause = eval("return $condizioneMongoDB;"); 
$retval = $collection->distinct("p_id", $whereClause); 

或者:

$condizioneMongoDB = "array('chrom'=>'chr7')"; 
eval("\$whereClause = $condizioneMongoDB;"); 
$retval = $collection->distinct("p_id", $whereClause); 
+0

我不知道爲什麼,但如果我寫var_dump($ whereClause);我獲得NULL – mydb

+0

我補充說明這一點。你沒有從'eval'返回任何東西。 –

+0

第二個選項似乎非常適合非常感謝你 – mydb

0

這條線是看在雙引號錯誤

$condizioneMongoDB = "array('chrom'=>'chr7')"; 

你爲什麼把數組?更改它像下面

$condizioneMongoDB = array('chrom'=>'chr7'); 
+0

感謝對答案。我通過表單獲得這個字符串。數組 – mydb

+0

$ collection-> distinct將第二個參數作爲數組而不是字符串,因此將數組傳遞給字符串它不再工作 –

+0

我使用這一行對數組進行轉換$ whereClause = eval(「\ $ str = \ 「$ condizioneMongoDB \」;「); – mydb