Error with select in mysql

Asked

Viewed 97 times

1

I am having an error log that does not happen frequently. On average 1 time per month. The problem is that I am not identifying the error. I’ll put my select on so you can see if there’s something wrong.

$result = $mysqli->query("
    SELECT SUM(a.qtd)
    FROM 
        produtos_pedidos a, 
        pedidos b
    WHERE 
        a.id_pedido = b.id 
        and (b.status = 0 or b.status = 4 or b.status = 7)
        and a.id_produto = $produto
        and b.pdv = 'n'
");
$row = mysqli_fetch_array($result);
$qtd_penhora = (float) $row['SUM(a.qtd)'];

The error log and this one:

[09-Jan-2017 16:56:55 America/Sao_Paulo] PHP Warning:  mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /var/www/webroot/ROOT/Funcoes/Movimentacao.php on line 35

The line that occurs and error is this one:

$row = mysqli_fetch_array($result);

There’s something wrong with the select?

  • Put your query into a variable. And it gives an echo $query. Take the return and run it in your DBMS. See if it returns results or not. What you can not do is error. Apparently right. I don’t know how you’re making the connection.

  • well I’ll test that you said.

2 answers

1

Means your query is not running correctly.

Place these lines before fetch_array() to find out what the error is:

if (!$result)
   die ($mysqli->error);
  • in which case it will return an error on the screen. and I disabled it on the server, how do I return the error in the log?

  • die works like Exit, prints a message and finishes the execution. PHP will not consider an error, so the message will be displayed on the screen.

-1

Try it like this, using the fetch_all:

$mysqli->query("
    SELECT SUM(a.qtd)
    FROM 
        produtos_pedidos a, 
        pedidos b
    WHERE 
        a.id_pedido = b.id 
        and (b.status = 0 or b.status = 4 or b.status = 7)
        and a.id_produto = $produto
        and b.pdv = 'n'
");
$row = $mysqli->fetch_all(MYSQLI_ASSOC);
  • what fetch_all does differently?

  • So, I’ve always used this way (when I don’t use framework like Laravel or codeigniter). It simply brings the data in array form, where the indexes are the column names. You can use the following parameters: MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH. Where the mysqli_assoc do as I say, mysqli_num makes index numbers, and mysqli_both brings the two (numbers and names).

Browser other questions tagged

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