Call to a member function mysqli_fetch_object () on a non-object [closed]

0

I'm creating a system of likes and it's currently connecting to Mysql, so now I'm migrating to Mysqli, but it's giving an error and I can not solve it at all:

connection.php

<?php
$servidor = 'localhost';
$usuario = 'root';
$senha = '';
$banco = 'meu_banco';

$con = mysqlI_connect($servidor, $usuario, $senha, $banco);
?>

function.php

<?php
function get_artigos(){
    $artigos = array();
    $selecionar = "SELECT artigo_id, titulo, likes FROM artigos";

    while ($row->mysqli_fetch_object($selecionar)){
        $artigos[] = array(
            'id_artigo' => $row->artigo_id,
            'titulo' => $row->titulo,
            'likes' => $row->likes
        );
    }

    return $artigos;
 }


function verificar_clicado($id_artigo, $id_usuario){
    $id_artigo = (int)$id_artigo;
    $id_usuario = (int)$id_usuario;
    $verificar = "SELECT like_id FROM likes WHERE user_id = '$id_usuario'      AND artigo_id = '$id_artigo'");
    return (mysql_num_rows($verificar) >= 1) ? true : false;
}


function adicionar_like($id_artigo, $id_usuario){
    $id_artigo = (int)$id_artigo;
    $id_usuario = (int)$id_usuario;
    $atualizar_likes_post = "UPDATE artigos SET likes = likes+1 WHERE   artigo_id = '$id_artigo'";

    if($atualizar_likes_post){
        $inserir_like = "INSERT INTO likes (user_id, artigo_id) VALUES    ('$id_usuario','$id_artigo'");
        if($inserir_like){
            return true;

        }else{
            return false;

        }
    }
}

function retornar_likes($id_artigo){
    $id_artigo = (int)$id_artigo;
    $selecionar_num_likes = "SELECT likes FROM artigos WHERE artigo_id =   $id_artigo";
    $fetch_likes = mysqli_fetch_object($selecionar_num_likes);
    return $fetch_likes->likes;
}
?>
    
asked by anonymous 24.12.2016 / 13:40

1 answer

3

Your problem is because you are calling the function: mysqli_fecth_object() , with a   parameter that should be passed to the function: mysqli_query() . for example in function getArtigos() , looks like this:

$selecionar = "SELECT artigo_id, titulo, likes FROM artigos";
while($row->mysqli_fetch_object($selecionar)){ }

When it should look like this:

$selecionar = "SELECT artigo_id, titulo, likes FROM artigos";
$resultado = mysqli_query($con, $selecionar);
while($row = mysqli_fetch_object($resultado)){ /*seu código aqui.*/ }

You are also repeating this error in functions: retornar_likes() and verificar_clicado() . You are also mixing the code of the object-oriented version with the procedural style. Reference link: link

    
24.12.2016 / 18:46