Error on my Website someone can help me?

02/20/2021 11:45 -Nástý#1
I have this error, i don't know why...

Fatal error: Call to a member function bindValue() on a non-object in C:\xampp\htdocs\index.php on line 81


My Code:



Someone can hep me pls?
02/20/2021 13:41 Contra1234#2
From where comes $web?
02/21/2021 08:18 stotterer09#3
#moved
02/23/2021 13:33 Legithos#4
Quote:
Originally Posted by -Nástý View Post

Code:
$sql = 'SELECT id, title, text, image, alttext, date FROM news WHERE hot = :hot ORDER BY date DESC LIMIT';
$qry = $web->prepare($sql);
$qry->bindValue(':hot', 0);
$qry->execute();
Your error is on bindValue. Try this:

[Only registered and activated users can see links. Click Here To Register...]

Code:
$server   = "mysql:dbname=DBNAME;host=LOCALHOST";
$user = "root";
$pass = "";

$pdo = new PDO($server,$user,$pass);

$stmt = $pdo->prepare("SELECT id, title, text, image, alttext, date FROM news WHERE hot = :hot ORDER BY date DESC LIMIT");
$stmt->execute([
    "hot" => 0
]);

while($dat = $stmt->fetch()){

      $id = $dat['id'];
      $title = $dat['title'];
      $text = $dat['text'];
      $img = $dat['image'];
      $imgalt = $dat['alttext'];
      $date = $dat['date'];


      ... and so on ...

}

?>
You dont need to bind a value with bindValue(), you can do it with ->execute([ "value" => $value ]);
02/24/2021 09:57 False#5
It seems the variable $web is not a instance of PDO.
Just do a var_dump of that variable and check what happens, also you should remove the ":" on the first bindValue parameter.
You dont have to rewrite the code as Legithos suggested.
02/24/2021 13:19 Legithos#6
Quote:
Originally Posted by False View Post
It seems the variable $web is not a instance of PDO.
Just do a var_dump of that variable and check what happens, also you should remove the ":" on the first bindValue parameter.
You dont have to rewrite the code as Legithos suggested.
The ":" are fine if you use named placeholder like he did in this case.
02/25/2021 09:32 False#7
Quote:
Originally Posted by Legithos View Post
The ":" are fine if you use named placeholder like he did in this case.
I know, but it also works without ":" which makes the code cleaner ;)