Error in mysql query in php

Asked

Viewed 146 times

1

I make an appointment at DB mysql and it works normal, then I’m going to do the second consultation and you give me the following mistake:

Warning: mysql_fetch_object(): supplied argument is not a Valid Mysql result Resource in C: xampp htdocs

Follow the PHP code below:

<php>
$open = new OpenDB();
$open->conectarNovamente($_SESSION['usuario'], $_SESSION['senha']);    

$hoje = date('Y-m-d');

$sql = "call proc_rel_controle_diario(NOW(),0,0)";  // ADD 1 dia 
$r = mysql_query($sql);
$obj = mysql_fetch_object($r);
$dia1 = $obj->total_geral;

$sql = "call proc_rel_controle_diario(NOW() + INTERVAL 1 DAY,0,0)";  // ADD 1 dia 
$r = mysql_query($sql);
$obj = mysql_fetch_object($r);
$dia2 = $obj->total_geral;
  • mysql Workbench works the same way and works both ways, only in php.

  • 1

    The problem is that the mysql_* functions do not work properly with stored procedures the recommended is you replace them with mysqli or Pdo. why we should not use the mysql functions_*

  • I always used mysql_* in Precedent, I never had problems, even so I used the mysqli_* recommendation and gave another error Warning: mysqli_fetch_object() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs

  • On the lines you have mysql($sql), leave so to display the error: $r = mysql_query($sql) or die(mysql_error()); and put the error message.

2 answers

2


Yes, use Mysqli and if you can also use object-oriented, your code would look like this.

$con = new mysqli(@$_SESSION['servidor'], $_SESSION['usuario'], $_SESSION['senha'], "local");

$hoje = date('Y-m-d');

$sql = "call proc_rel_controle_diario(NOW(),0,0);";  // ADD 1 dia 
$r = $con->query($sql);
$obj = $r->fetch_array();
$dia1 = $obj->total_geral;
echo $dia1;

$sql = "call proc_rel_controle_diario(NOW() + INTERVAL 1 DAY,0,0);";  // ADD 1 dia 
$r = $con->query($sql);
$obj = $r->fetch_array();
$dia2 = $obj->total_geral;
echo $dia2;

More informations about Mysqli

0

solved the problem, need to open another connection for the other operation, just do not know why.

Thanks lost for the tip, I will use mysqli_* because I didn’t even know about the mysql discontinuation_*.

the code went like this

$open = new OpenDB();
$con = mysqli_connect(@$_SESSION['servidor'],$_SESSION['usuario'],$_SESSION['senha'],"local");

$hoje = date('Y-m-d');

$sql = "call proc_rel_controle_diario(NOW(),0,0);";  // ADD 1 dia 
$r = mysqli_query($con,$sql);
$obj = mysqli_fetch_object($r);
$dia1 = $obj->total_geral;
echo $dia1;

$con = mysqli_connect(@$_SESSION['servidor'],$_SESSION['usuario'],$_SESSION['senha'],"local");
$sql = "call proc_rel_controle_diario(NOW() + INTERVAL 1 DAY,0,0);";  // ADD 1 dia 
$r = mysqli_query($con,$sql);
$obj = mysqli_fetch_object($r);
$dia2 = $obj->total_geral;
echo $dia2;

Browser other questions tagged

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