1

I have one problem for Where clause in PHP - MongoDB.

i search in this forum and obtain some information.

My code is :

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

The Where clause is completely ignored.

This static code work:

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

2 Answers 2

1

$whereClause = eval("\$str = \"$condizioneMongoDB\";");

it is equal to $str = "array('chrom'=>'chr7' )",
but you want $str = array('chrom'=>'chr7')

You should to omit quotes for this.

Also, you need to return value from eval to use it, or you can use variable from eval directly.

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

Or:

$condizioneMongoDB = "array('chrom'=>'chr7' )";
eval("\$whereClause = $condizioneMongoDB;");
$retval = $collection->distinct("p_id", $whereClause);
Sign up to request clarification or add additional context in comments.

3 Comments

i don't know why but if i write var_dump($whereClause); i obtain NULL
I add explanation about this. You didn't return anything from eval.
the second option seems to work perfectly thank you very much
0

this line is looks wrong

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

Why are you putting array in double quotes? Change it like following

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

5 Comments

thank's for answer . I obtain this string by form . The line with eval transform in array
$collection->distinct is taking second argument as array not string so passing array in string it is not working any more
I transform in array with this line $whereClause = eval("\$str = \"$condizioneMongoDB\";");
after eval array is not display from assigned variable
yes sorry in this post i simplify the answer but i get in input a string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.