Could not set variable with fetch_object

Asked

Viewed 27 times

1

I’m trying to make a simple query however, although Phpmyadmin perform the query, when I run the code Apache returns the message:

Notice: Undefined variable: rprd in D: xampp htdocs final views premorc.php on line 2723

My code is like this:

$data   = date("d-m-Y");
$partes = explode("-", $data);
$ano    = $partes[2];
$lj    = $_GET['emp'];
$linha  = $_GET['ans'];
$modal  = $_GET['modal'];

$periodo = "$ano%";

$sql = "SELECT 
  sjy_vendas.tipo, 
  Sum(sjy_vendas.qt) AS qt, 
  Sum(sjy_vendas.qtv) AS qtv, 
  Sum(sjy_vendas.rprd) AS rprd, 
  Sum(sjy_vendas.rsrv) AS rsrv, 
  Sum(sjy_vendas.tprd) AS tprd, 
  Sum(sjy_vendas.tsrv) AS tsrv, 
  Sum(sjy_vendas.vlr_compra) AS vlr_compra
  FROM sjy_vendas
  WHERE sjy_vendas.empresa = $lj 
  AND sjy_vendas.tipo = $linha
  AND sjy_vendas.dt_nf LIKE '$periodo'
  GROUP BY sjy_vendas.tipo";

$resultado = $MySQLi->query($sql) OR trigger_error($MySQLi->error, E_USER_ERROR);
while ($dados = $resultado->fetch_object()) {


$qtv    = $dados["qtv"];
$rprd   = $dados["rprd"];
}

if($modal == "X"){
    $anterior = $qtv;
} else if($modal = "F"){
    $anterior = $rprd; // Essa é a linha 2723
}
  • Meaning that your query did not return anything (it did not enter while) or maybe that if/Else should be inside while.

  • You are searching for an object, and accessing with an array, you will not search even, ve change where is "$data['qtv']" for "$data->qtv" no longer solves

  • Rray, thank you for the answer. To define the variable I only need the code that is already described.

  • Anthraxisbr thanks for the answer, but it is giving the same error. I have tried with fetch_array and gives the same error. This code is working on another page (with another query of course), but here is giving error.

  • Put $data = $result->fetch_object() out of while, and place var_dump($data), to see if the query is being made

  • Thanks @Anthraxisbr, that’s right. I was setting an object with an array. Now it’s working. Thanks.

  • But when I copied and pasted from the test page to production page continued giving the same error. :/

Show 2 more comments
No answers

Browser other questions tagged

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