0

ive started updating my code from mysql to pdo, and im not very bright and get easily mad. which ive gotten now. i dont understand why this isnt working, and id like anyones help why. the mysql code is commented out of the section (it works, but want pdo). the pdo code isnt working however and i cant understand why.

can someone help me?

$result_bajs = "SELECT * FROM comments WHERE movie_id = :id ORDER BY post_date DESC";

$stmt2 = $pdo->prepare($result_bajs);
$stmt2 = execute(array(
    ':id' => $id
));
$result2 = $stmt2->fetchAll();
while($row = $result2){
//$result2 = mysql_query($result_bajs, $connection) or die (mysql_error());

$ip = $_SERVER['REMOTE_ADDR'];
//while ($row = mysql_fetch_array($result2)) {
   $comment = $row['comment'];
   $author = $row['author'];
   $post_date = $row['post_date'];
saknar namn
  • 139
  • 1
  • 1
  • 10
  • `$stmt2 = execute()` you just overwrote your statement variable `$stmt2` with a boolean result of `execute()`. Use a different variable! – Michael Berkowski Apr 22 '14 at 14:27
  • And replace the `while()` loop with a `foreach()` since you already fetched all rows.. `foreach($result2 as $row){...}` you just need to iterate the 2D array. – Michael Berkowski Apr 22 '14 at 14:29
  • 1
    and call execute from its object `$var = execute(...)` is calling `execute()` as function, which I doubt is – Royal Bg Apr 22 '14 at 14:32
  • If you find it "not working", you need to be able to see what's going wrong. _Always_ when developing code, display errors on screen with error reporting maximized. `error_reporting(E_ALL); ini_set('display_errors', 1);` Here you would have seen a fatal error with an undefined function `execute()` since it wasn't called as `$stmt2->execute()` – Michael Berkowski Apr 22 '14 at 14:47

1 Answers1

1

You're overwriting your variables. Also the code is very unclear. Maybe this code will help you understand and write your queries better:

$SQL = $pdo->prepare("SELECT * FROM comments WHERE movie_id = :id ORDER BY post_date  DESC");
$SQL->execute(array(':id' => $id));
$row = $SQL->fetchAll();
user2298995
  • 2,045
  • 3
  • 19
  • 27