|
You last visited: Today at 16:18
Advertisement
Error on my Website someone can help me?
Discussion on Error on my Website someone can help me? within the Web Development forum part of the Coders Den category.
02/20/2021, 11:45
|
#1
|
elite*gold: 0
Join Date: Oct 2020
Posts: 43
Received Thanks: 1
|
Error on my Website someone can help me?
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:
HTML Code:
<?php
$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();
while($dat = $qry->fetch(PDO::FETCH_ASSOC))
{
$id = $dat['id'];
$title = $dat['title'];
$text = $dat['text'];
$img = $dat['image'];
$imgalt = $dat['alttext'];
$date = $dat['date'];
if(strlen($text) > 390)
{
$text = substr($text, 0, 390);
$text = $text . "...";
}
$text = nl2br($text);
$date_array = explode("-",$date);
$date = $date_array[2].".".$date_array[1].".".$date_array[0];
echo
'<li>
<div class="image"><a href="./post.php?id='.$id.'"><img alt="'.$imgalt.'" src="./images/news/general/'.$img.'" /></a></div>
<div class="info">
<h2><a href="./post.php?id='.$id.'">'.$title.'</a></h2>
<div class="date_n_author">'.$date.'</div>
<p>'.$text.'</p>
<a href="./post.php?id='.$id.'" class="read_more2">Read more</a> </div>
<div class="clear">
</div>
</li>';
}
?>
Someone can hep me pls?
|
|
|
02/20/2021, 13:41
|
#2
|
elite*gold: 0
Join Date: Jan 2015
Posts: 7
Received Thanks: 3
|
From where comes $web?
|
|
|
02/21/2021, 08:18
|
#3
|
Moderator
elite*gold: 26
Join Date: Dec 2010
Posts: 2,577
Received Thanks: 1,795
|
#moved
|
|
|
02/23/2021, 13:33
|
#4
|
elite*gold: 95
Join Date: Nov 2009
Posts: 646
Received Thanks: 44
|
Quote:
Originally Posted by -Nástý
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:
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
|
#5
|
elite*gold: 50
Join Date: Apr 2011
Posts: 11,087
Received Thanks: 2,410
|
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
|
#6
|
elite*gold: 95
Join Date: Nov 2009
Posts: 646
Received Thanks: 44
|
Quote:
Originally Posted by False
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
|
#7
|
elite*gold: 50
Join Date: Apr 2011
Posts: 11,087
Received Thanks: 2,410
|
Quote:
Originally Posted by Legithos
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
|
|
|
All times are GMT +1. The time now is 16:18.
|
|