Call to a Member Function mysqli_fetch_object() on a non-object

Asked

Viewed 102 times

0

I’m creating a system of Likes and currently he was connecting with Mysql, then now I’m migrating to Mysqli, but is giving an error and I can not solve at all :(

php connection.

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

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

php function.

<?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;
}
?>
  • 2

    mysqlI_connect, switches to small letter... What error gives?

  • I think only changing mysqli to mysqli already works, as Sergio said, because the mysqli command works the same way as mysql, but it accepts a wider range of services and options

  • tip* Don’t put your connection parameters where everyone can see, always make it look like you’re going to use the test pattern on the pc. Ex: ('localhost', 'root', ', 'local_db')

  • 1

    @Murilogambôa thanks for the tip, I put the i in minusculo but the same error keeps appearing :(

  • 1

    Where is the error?

1 answer

3

Your problem is why 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(), is like this:

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

When I should be 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 the functions:retornar_likes() and verificar_clicado(). In addition you are mixing the object-oriented version code with the procedural style. Link to reference: http://www.w3schools.com/php/php_ref_mysqli.asp

Browser other questions tagged

You are not signed in. Login or sign up in order to post.