How to use PDO on Eval()

Asked

Viewed 55 times

0

I would like to know how to use PDO with the function Eval(), since the way I use is generating error.

$id = "return $bd->fetchAll();";
eval("$id");
eval("return $bd->fetchAll();");

MISTAKES:

 Notice: Undefined property: PDOStatement::$fetchAll in 
    Parse error: syntax error, unexpected ')' in
  • To call methods within a string, use { }. change the two lines to: $id = "return {$bd->fetchAll()};"; eval("return {$bd->fetchAll()};");

  • Still keeps generating error...

  • 1

    Try putting in variables. Ex: eval("return \$bd->fetchAll();")

  • 3

    What is the purpose of using the eval()?

  • Now generates no error but also returns no data, Empty... eval("return \$conn->fetchAll(PDO::FETCH_CLASS);"); without Eval returns normal the querys...

  • obviously it would not return any given because it only makes the return of the object.. By the way, saw the above question? What is the reason for use of Eval ?

  • 1

    documentation evil ;)

  • Goal that is a class, where I won’t keep putting is "Fecht" does it, is fetchAll does it.. and so on... from there to facilitate my life, I pass the method by string...

  • But then it returns the object and I give a var_dump to check what was returned... With Eval comes empty, typing the code returns all the data...

  • 1

    Solved like this: $id = "return \$conn->fetchAll(PDO::FETCH_CLASS)"; eval("\$id;");

  • Regardless of your solution, there’s a 99.99% chance that you’re making terrible use of eval. If you explain in detail the problem you are trying to solve with this (it may be in another question), I’m sure the staff will help you find a better solution without eval.

  • As I said above, I created a class where one would fetch or fechAll or other PDO methods... I would not like to put one by one... there in the method I created, say, I write: $this->pega('fetch'); then he would add the fetch method, $this->pega('fetchAll'); then use the fetchall dowry... there are various and various methods in the PDO, the would go bad. Imagine that after fetch all, I would like to count the number of values found in the search... com Val, I could call more methods, without specifying in my class, If fetch, do it, if Fetchall does it .

  • Then I’d put right what I want: $this->pega('return $conn->fecthAll(); if($conn->rowcount() >= 1)'){faz isso} ... I could add a lot more to my code...

Show 8 more comments

1 answer

0

Heed

I don’t know why you want to do this, but I already warn in advance that using eval is almost always considered a bad practice.

But I’ll answer anyway.

Answer

For this to work, you have to escape the siphon $ with a bar \

Thus:

eval("return \$bd->fetchAll();");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.