ERROR: Object of class mysqli_result could not be converted to int

Asked

Viewed 454 times

0

Hello! I am developing a site to buy a game that has a page to record the information of each purchase, among them, the amount of games purchased.

I wanted that, during the saving of the information, the amount of games purchased would be subtracted from the stock.

My attempt:

<?php

    $nome               = $_POST['NOME'];
    $cpf                = $_POST['CPF'];
    $email              = $_POST['EMAIL'];
    $quantidade         = $_POST['QUANTIDADE'];
    $pagamento          = $_POST['PAGAMENTO'];


    $strcon = mysqli_connect('localhost','root','', 'ecoventura') or die('Erro ao conectar ao banco de dados');
    $jogos = mysqli_query($strcon, "select JOGOS from estoque");
    $novaquan = $jogos - $quantidade; //ESSA É A LINHA DO ERRO

    $sql = "INSERT INTO compra (NOME, CPF, EMAIL, QUANTIDADE, PAGAMENTO) VALUES ('$nome','$cpf', '$email', '$quantidade', '$pagamento')"; 
    $modifica = "UPDATE  `ecoventura`.`estoque` SET `JOGOS` = '$novaquan'";

    mysqli_query($strcon,$sql) or die("Erro ao tentar cadastrar registro" . mysqli_error($strcon));
    mysqli_query($strcon,$modifica) or die("Erro ao tentar modificar estoque" . mysqli_error($strcon));
    mysqli_close($strcon);
    /*echo '<script type="text/javascript">
            alert("Salvo com Sucesso !");
            window.history.go(-1);
        </script>';*/


?>

The form even saves the information, but the games column has a record and apparently $games contain the amount of records and not the content of it.

The error presented is this:

Notice: Object of class mysqli_result could not be converted to int in C: Easyphp5.2.10 www ecoventura salva-form.php on line 13

How can I take the contents of the record and not its quantity?

  • The return of function mysqli_query is not a number, so it makes no sense to do an algebraic operation with $jogos, as it did. You selected the games column from all the stock table records, what would be the expected result for $jogos - $quantidade?

  • Ah, so that’s why you’re giving it -4. Games are getting 1 because there’s only one record. How do I get the contents of this record and not the amount of records?

  • 1

    Wrong. You’re not taking the amount, you’re taking the content, you’re just using the return wrong. See documentation, function return mysqli_query is an instance of mysqli_result, then study the documentation to check how to work with this object.

  • So, I don’t know if I got it right but reading the documentation it seems that that mysqli_num_rows ( mysqli_result $result ) : int takes the contents of the record, but I tried to use it and it didn’t work. Why?

  • 1

    Neither. As the function name itself says, num rows returns the amount of records returned in the query. Returns number of Rows in the result set. What you need are the functions fetch.

1 answer

0


The problem is actually between lines 10 and 11 of your code.

    (linha 10) $jogos = mysqli_query($strcon, "select JOGOS from estoque");
    (linha 11) $novaquan = $jogos - $quantidade; //ESSA É A LINHA DO ERRO

On line 10, $games return FALSE if you do not find any data for the query or an array of objects, so the ideal would be to first treat this result and then run the PHP function mysqli_num_rows() to return the number of rows for the query result:

   if ($result = mysqli_query($link, "select JOGOS from estoque")) {

        /* determine final result */
        $TotalDeJogosEncontrados = mysqli_num_rows($result);
        $novaquan = $TotalDeJogosEncontrados - $quantidade;

        /* return a row from result */
        $row = mysqli_fetch_assoc($result);

        /* close result set */
        mysqli_free_result($result);
    }
  • The return is not false when it finds no data, nor array when it finds it. In both situations the return is an instance of mysqli_result. False is only returned in cases of failure during SQL execution.

  • It is in the PHP documentation that mysqli_result Returns FALSE in the event of a failure, so much so that if it does not result, it does not enter if. Now regarding the return, I really expressed myself as soon as it returns an instance of mysqli_result.

  • In that case not finding records is considered flawed? I do not remember this detail.

  • I tried using your code and added another record in the games column, which now has two records. I gave a var_dump() and realized that the $Totaldegamessntrados is returning the amount of records in the column. Since this column will always have only one record I would like to get the content from it. How do I do this?

  • 1

    Wow to take the contents of a mysqli_result, return a line and just use mysql_fetch_assoc. $Row = mysqli_fetch_assoc($result); $Gameodata = $Row["GAMES"];

Browser other questions tagged

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