php - Call to a member function bindParam on a non-object -
this question has answer here:
- call member function on non-object [duplicate] 8 answers
- reference - error mean in php? 29 answers
i'm trying different sql statement in case user submit search form. i'm trying code below, i'm getting error , i'm not understanding why, because code seems gode me.
do see here can cause of these errors?
errors im getting:
call member function bindparam() on non-object in $readnews->bindparam
notice: undefined variable: readnews in $readnews->bindparam(':begin', begin, pdo::param_int);
fatal error: call member function bindparam() on non-object in f:\xampp\htdocs\projeto\admin\posts\noticias.php on line 53
my code:
$pag = (empty($_get['pag']) ? '1' : $_get['pag']); $maximo = 5; $begin = ($pag * $max) - $max; if (isset($_post['search'])) { $search = $_post['search']; if(!empty($search) && $search != 'title:') { $readnews = $pdo->prepare("select * news title concat('%', :search, '%') order data desc limit :begin, :max"); $readnews->bindparam(':search', $search); } } else { $readnews = $pdo->prepare('select * news order data desc limit :begin, :max'); } $readnews->bindparam(':begin', begin, pdo::param_int); $readnews->bindparam(':max', $max, pdo::param_int); $readnews->execute(); $resultreadnews = $readnews->rowcount();
you not handling 1 case: when isset($_post['search'])
true
, !empty($search) && $search != 'title:'
false. in case $readnews
parameter not initialized , statement not prepared, explains both errors.
Comments
Post a Comment