PHP Fatal error: Uncaught Error

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
Joak190867
New php-forum User
New php-forum User
Posts: 1
Joined: Sat Aug 31, 2019 5:48 pm

Sat Aug 31, 2019 5:56 pm

Hi everyone

I´m not an expert developer on PHP, I have one WebService and I have gotten a error message ....

PHP Fatal error: Uncaught Error: Call to a member function fetch_object() on boolean in /home/public_html/MiScore/WebService/gettablapos.php:122

The line 122 it's

while ($row = $stmt->fetch_object())

After ... $stmt = $this->db->query('CALL tabla_posiciones(@par_cliente,@par_sucursal,@par_torneo,@par_jornada)') or die(mysqli_error($this->db));

could you help me please?




<?php

// Helper method to get a string description for an HTTP status code
// From http://www.gen-x-design.com/archives/cr ... -with-php/
function getStatusCodeMessage($status)
{
// these could be stored in a .ini file and loaded
// via parse_ini_file()... however, this will suffice
// for an example
$codes = Array(
100 => 'Continue',
101 => 'Switching Protocols',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => '(Unused)',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported'
);

return (isset($codes[$status])) ? $codes[$status] : '';
}

// Helper method to send a HTTP response code/message
function sendResponse($status = 200, $body = '', $content_type = 'text/html')
{
$status_header = 'HTTP/1.1 ' . $status . ' ' . getStatusCodeMessage($status);
header($status_header);
header('Content-type: ' . $content_type);
echo $body;
}


class RedeemAPI {
private $db;
// Constructor - open DB connection
function __construct() {
$this->db = new mysqli('localhost', 'futchoc1_admin', 'Futcho190867', 'futchoc1_futsoft');
/* verificar la conexi�n */
if (mysqli_connect_errno()) {
printf("Conexi�n fallida: %s\n", mysqli_connect_error());
exit();
}
$this->db->autocommit(FALSE);
$this->db->query("SET NAMES 'utf8'");
}

// Destructor - close DB connection
function __destruct() {
$this->db->close();
}
// Main method to redeem a code
function redeem() {
// Check for required parameters
if (isset($_POST["id_cliente"]) && isset($_POST["id_sucursal"]) && isset($_POST["id_torneo"]) && isset($_POST["id_jornada"])) {
// Put parameters into local variables
$cliente = $_POST["id_cliente"];
$sucursal = $_POST["id_sucursal"];
$torneo = $_POST["id_torneo"];
$jornada = $_POST["id_jornada"];

// Look up code in database
$stmt = $this->db->prepare('SET @par_cliente := ?');
$stmt->bind_param('i', $cliente);
$stmt->execute();

$stmt = $this->db->prepare('SET @par_sucursal := ?');
$stmt->bind_param('i', $sucursal);
$stmt->execute();

$stmt = $this->db->prepare('SET @par_torneo := ?');
$stmt->bind_param('i', $torneo);
$stmt->execute();

$stmt = $this->db->prepare('SET @par_jornada := ?');
$stmt->bind_param('i', $jornada);
$stmt->execute();


$stmt = $this->db->query('CALL tabla_posiciones(@par_cliente,@par_sucursal,@par_torneo,@par_jornada)') or die(mysqli_error($this->db));

//$stmt = $this->db->prepare('CALL tabla_posiciones(?,?,?,?)') or die(mysqli_error($this->db));
//$stmt->bind_param("iiii", $cliente, $sucursal, $torneo, $jornada);
//$stmt->execute();

//$stmt->bind_result($equ_nombre, $jj, $jg, $je, $jp, $gf, $ge, $dif, $puntos);
$arreglo = array() ;
$contador = 0;
while ($row = $stmt->fetch_object()) {
$arreglo[$contador] = array("equ_nombre"=>$row->equ_nombre,"jj"=>$row->jj,"jg"=>$row->jg,"je"=>$row->je,"jp"=>$row->jp,"gf"=>$row->gf,"ge"=>$row->ge,"dif"=>$row->dif,"puntos"=>$row->puntos);
$contador++;
}
$stmt->close();
// Bail if code doesn't exist
if ($contador <= 0) {
$cuantos = 0;
$datos[$cuantos] = array("equ_nombre"=>"S/N");
$return_array = array('Datos' => $datos,'status' => -1, 'mensaje' => 'No existen registros con los datos proporcionados');
sendResponse(200, json_encode($return_array));
return true;
}
$this->armarbol($arreglo);
return true;
}
$return_array = array('status' => 0, 'mensaje' => 'Al parecer no se enviaron los par�metros necesarios');
sendResponse(200, json_encode($return_array));
return true;
}

function armarbol(array $general)
{
$datos = array();
//echo "Ingreso a function ";
$cuantos = 0;
foreach ($general as $reg)
{

$datos[$cuantos] = array("equ_nombre"=>$reg["equ_nombre"],"jj"=>$reg["jj"],"jg"=>$reg["jg"],"je"=>$reg["je"],"jp"=>$reg["jp"],"gf"=>$reg["gf"],"ge"=>$reg["ge"],"dif"=>$reg["dif"],"puntos"=>$reg["puntos"] );
$cuantos++;
}


$return_array = array('Datos' => $datos, 'status' => 1, 'mensaje' => '');
sendResponse(200, json_encode($return_array));
}
}

// This is the first thing that gets called when this page is loaded
// Creates a new instance of the RedeemAPI class and calls the redeem method
$api = new RedeemAPI;
$api->redeem();
chorn
php-forum GURU
php-forum GURU
Posts: 614
Joined: Fri Apr 01, 2016 2:18 am

Sun Sep 01, 2019 10:59 pm

"fetch_object() on boolean" mostly relies on a failed query, because then you would get FALSE as a result, so on your development machine you should throw errors

Code: Select all

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
Post Reply