/    Sign up×
Community /Pin to ProfileBookmark

Uncaught Error: Call to a member function fetch_object() on boolean

Hi everyone.

I’m not expert PHP developer.
I have one WebService write on PHP and I have one error in this line

`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);`

Just after execute
`$stmt = $this->db->query(‘CALL tabla_posiciones(@par_cliente,@par_sucursal,@par_torneo,@par_jornada)’) or die(mysqli_error($this->db));
`

What’s wrong?

This the complete code …

`
<?php

// Helper method to get a string description for an HTTP status code
// From http://www.gen-x-design.com/archives/create-a-rest-api-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’, ‘futchoco’, ‘987654’, ‘futchoco_futsoft’);
/* verificar la conexión */
if (mysqli_connect_errno()) {
printf(“Conexión fallida: %sn”, 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));

$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();

?>
`

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 03.2019 — What database interface are you using? MySQLi, PDO, other? If PDO, the method would be $stmt-&gt;fetchObject().
Copy linkTweet thisAlerts:
@ginerjmSep 03.2019 — What line number did the error message point out?
Copy linkTweet thisAlerts:
@Joak190867authorSep 04.2019 — @NogDog#1608342

Hi

I'm using MySQL
Copy linkTweet thisAlerts:
@NogDogSep 04.2019 — Oh...the error said "Call to a member function fetch_object() **on boolean**". That normally means there was something syntactically wrong with the SQL, or some other error that caused the request to return a Boolean false instead of a statement object. As to why it's not hitting your or die(...) I'm not sure. Maybe try this instead?
<i>
</i>$stmt = $this-&gt;db-&gt;query('CALL tabla_posiciones(@par_cliente,@par_sucursal,@par_torneo,@par_jornada)');
if($stmt == false) {
die(mysqli_error($this-&gt;db);
}
Copy linkTweet thisAlerts:
@Joak190867authorSep 05.2019 — @NogDog#1608369 When I run the Store Procedure at MySql editor, I don't have any error (file1.jpg)[upl-image-preview url=https://www.webdeveloper.com/assets/files/2019-09-05/1567698575-486534-file1.png]

So, I put your suggestion and I have gotten the same error

[05-Sep-2019 15:44:27 UTC] PHP Fatal error: Uncaught Error: Call to a member function fetch_object() on boolean in /home/public_html/WebService/gettablapos.php:128<br/>
Stack trace:<br/>
#0 /home/public_html/WebService/gettablapos.php(170): RedeemAPI-&gt;redeem()<br/>
#1 {main}<br/>
thrown in /home/public_html/WebService/gettablapos.php on line 128


The file2.jpg show you the line 128

[upl-image-preview url=https://www.webdeveloper.com/assets/files/2019-09-05/1567699177-210075-file2.jpeg]
Copy linkTweet thisAlerts:
@NogDogSep 05.2019 — Hmm...I've only used stored functions with PostgreSQL, but poking around the web seems to indicate that it's not as easy in MySQL? I see a couple examples where they have their stored function write to temporary table, and then query that temp table. :yuck: For instance: https://www.tutorialspoint.com/how-to-call-a-stored-procedure-using-select-statement-in-mysql
Copy linkTweet thisAlerts:
@ginerjmSep 05.2019 — So - this says that $stmt is holding a value of False and not the query results. Can you post the relevant code (not commented out stuff) prior to line 128 so we can see a bit more detail about what is being executed?
Copy linkTweet thisAlerts:
@NogDogSep 05.2019 — BTW: I just realized that's why the or die() wasn't triggered. The -&gt;query() is not returning a result set, so no statement object, but it's not failing, so it's not returning False -- it's returning True, the same as it would for a successful insert or update query. :)
Copy linkTweet thisAlerts:
@Joak190867authorSep 06.2019 — @NogDog#1608416 Hi NogDog.

Where can I find example to returning a result set?

Regards.
Copy linkTweet thisAlerts:
@Joak190867authorSep 06.2019 — @ginerjm#1608413 Hi, the rest of code

[upl-image-preview url=https://www.webdeveloper.com/assets/files/2019-09-06/1567813554-82806-file1.jpeg]

[upl-image-preview url=https://www.webdeveloper.com/assets/files/2019-09-06/1567813563-590102-file2.jpeg]
Copy linkTweet thisAlerts:
@NogDogSep 07.2019 — Can you show us the MySQL stored function definition?
Copy linkTweet thisAlerts:
@ginerjmSep 09.2019 — Still don't see the code. Just some very tiny images of stuff......
Copy linkTweet thisAlerts:
@NogDogSep 09.2019 — > @ginerjm#1608528 Still don't see the code. Just some very tiny images of stuff......

The problem is that they are calling query() with a MySQL CALL request to a stored function. That does not return a result set, just a Boolean true/false depending on whether MySQL was able to run the command. Therefore they do not get a statement object, just a Boolean true, and thus the error message.
Copy linkTweet thisAlerts:
@Joak190867authorSep 09.2019 — @NogDog#1608529 Hello

Just the SP insert records to temp table, here the code (NOTE: The last SELECT is only to check if there are records into temp table)

begin<br/>
DECLARE vn_equipo INT;<br/>
DECLARE vn_jornada INT;<br/>
DECLARE vn_numjueg INT;<br/>
DECLARE vn_equipo_vs INT;<br/>
DECLARE vn_jj INT DEFAULT 0;<br/>
DECLARE vn_marcador1 INT;<br/>
DECLARE vn_marcador2 INT;<br/>
DECLARE vn_jg INT DEFAULT 0;<br/>
DECLARE vn_je INT DEFAULT 0;<br/>
DECLARE vn_jp INT DEFAULT 0;<br/>
DECLARE vn_gf INT DEFAULT 0;<br/>
DECLARE vn_ge INT DEFAULT 0;<br/>
DECLARE vn_puntos INT DEFAULT 0;<br/>
DECLARE done INT DEFAULT 0;<br/>
DECLARE vn_penales INT DEFAULT 0; <br/><br/>
/* Cursor de Tabla Temporal <EM>*/<br/>
DECLARE barre_temporal CURSOR FOR<br/>
SELECT id_equipo<br/>
FROM tabla_pos;<br/>
/*</EM> Cursor para barrer los partidos */<br/>
DECLARE barre_partidos CURSOR FOR<br/>
SELECT a.id_jornada, a.id_juego, b.cal_penales<br/>
FROM encuentro as a, calendario as b<br/>
WHERE a.id_cliente = par_cliente AND a.id_sucursal = par_sucursal AND<br/>
a.id_torneo = par_torneo AND a.id_jornada &lt;= par_jornada AND a.id_equipo = vn_equipo AND<br/>
b.id_cliente = a.id_cliente AND b.id_sucursal = a.id_sucursal AND<br/>
b.id_torneo = a.id_torneo AND b.id_jornada = a.id_jornada AND b.id_juego = a.id_juego AND<br/>
b.cal_estatus = 1;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;

DROP TEMPORARY TABLE IF EXISTS tabla_pos;<br/>
-- Crea tabla Temporal, con los equipos correspondientes según paramétros<br/>
-- Las columnas siguientes de "puntos", son las necesarias para el tratamiento de la promoción de JUMEX<br/>
CREATE TEMPORARY TABLE tabla_pos AS<br/>
SELECT id_equipo,0 as jj,0 as jg,0 as je,0 as jp,0 as gf,0 as ge,0 as puntos, 0 goles, 0 diferencia, 0 num_juego, 0 adeudo<br/>
FROM equipo<br/>
WHERE id_cliente = par_cliente AND id_sucursal = par_sucursal AND id_torneo = par_torneo AND equ_estatus = 'A';
<br/>
SET SQL_SAFE_UPDATES=0;<br/>
OPEN barre_temporal;<br/>
read_loop: LOOP<br/>
/* barre equipos de la temporal <EM>*/<br/>
FETCH barre_temporal INTO vn_equipo;<br/>
If done THEN<br/>
LEAVE read_loop;<br/>
END IF;<br/>
OPEN barre_partidos;<br/>
read_partidos: LOOP<br/>
/*</EM> Barre los partidos del equipo en curso <EM>*/<br/>
FETCH barre_partidos INTO vn_jornada, vn_numjueg, vn_penales;<br/>
If done THEN<br/>
LEAVE read_partidos;<br/>
End If;<br/>
/*</EM> Incrementa el contador de número de juegos <EM>*/<br/>
SET vn_jj = vn_jj + 1;<br/>
/*</EM> Determina el equipo contrario <EM>*/<br/>
SET vn_equipo_vs = (SELECT id_equipo<br/>
FROM encuentro<br/>
WHERE id_equipo &lt;&gt; vn_equipo and id_cliente = par_cliente and id_sucursal = par_sucursal and id_torneo = par_torneo and id_jornada = vn_jornada and id_juego = vn_numjueg) ;<br/>
/*</EM> Determina Marcadores de ambos equipos <EM>*/<br/>
SET vn_marcador1 = (SELECT IfNull(SUM(detalle_encuentro.denc_gol),0)<br/>
FROM detalle_encuentro<br/>
WHERE id_cliente = par_cliente AND<br/>
id_sucursal= par_sucursal AND<br/>
id_torneo = par_torneo AND<br/>
id_jornada = vn_jornada AND<br/>
id_juego = vn_numjueg AND<br/>
id_equipo = vn_equipo);<br/>
SET vn_marcador2 = (SELECT IfNull(SUM(detalle_encuentro.denc_gol),0)<br/>
FROM detalle_encuentro<br/>
WHERE id_cliente = par_cliente AND<br/>
id_sucursal= par_sucursal AND<br/>
id_torneo = par_torneo AND<br/>
id_jornada = vn_jornada AND<br/>
id_juego = vn_numjueg AND<br/>
id_equipo = vn_equipo_vs);<br/>
IF vn_marcador1 = vn_marcador2 Then<br/>
SET vn_je = vn_je + 1;<br/>
SET vn_puntos = vn_puntos + 1;<br/>
END IF;<br/>
If vn_marcador1 &gt; vn_marcador2 Then<br/>
SET vn_jg = vn_jg + 1;<br/>
IF vn_penales = 1 Then<br/>
/*</EM>La cancha no trabaja con empates, se definió en paneles el encuentro<br/>
el eequipo ganó <EM>*/<br/>
SET vn_puntos = vn_puntos + 2;<br/>
ELSE<br/>
SET vn_puntos = vn_puntos + 3;<br/>
END IF;<br/>
END IF;<br/>
If vn_marcador2 &gt; vn_marcador1 Then<br/>
SET vn_jp = vn_jp + 1;<br/>
IF vn_penales = 1 Then<br/>
/*</EM>La cancha no trabaja con empates, se definió en paneles el encuentro<br/>
y el equipo perdió<EM>*/<br/>
SET vn_puntos = vn_puntos + 1;<br/>
END IF;<br/>
END IF;<br/>
SET vn_gf = vn_gf + vn_marcador1;<br/>
SET vn_ge = vn_ge + vn_marcador2;<br/>
/*</EM> Sólo si la jornada es igual a la del párametro de entrada, se graban los marcadores*/<br/>
If vn_jornada = par_jornada Then<br/>
UPDATE tabla_pos<br/>
SET goles = vn_marcador1, num_juego = vn_numjueg<br/>
WHERE id_equipo = vn_equipo;<br/>
UPDATE tabla_pos<br/>
SET goles = vn_marcador2, num_juego = vn_numjueg<br/>
WHERE id_equipo = vn_equipo_vs;<br/>
END IF;<br/>
END LOOP; <br/><br/>
CLOSE barre_partidos;<br/>
UPDATE tabla_pos<br/>
SET jj = vn_jj, jg = vn_jg, je = vn_je, jp = vn_jp, gf = vn_gf, ge = vn_ge, puntos = vn_puntos<br/>
WHERE id_equipo = vn_equipo;<br/>
SET vn_jj = 0;<br/>
SET vn_je = 0;<br/>
SET vn_puntos = 0;<br/>
SET vn_jp = 0;<br/>
SET vn_jg = 0;<br/>
SET vn_gf = 0;<br/>
SET vn_ge = 0;<br/>
SET done = 0;<br/>
SET vn_penales = 0;<br/>
END LOOP;<br/>
CLOSE barre_temporal;
<br/>
/*<br/>
SELECT equipo.equ_nombre, tabla_pos.jj, tabla_pos.jg, tabla_pos.je,<br/>
tabla_pos.jp, tabla_pos.gf, tabla_pos.ge, (tabla_pos.gf - tabla_pos.ge) as 'dif', tabla_pos.puntos<br/>
FROM tabla_pos, equipo<br/>
WHERE equipo.id_cliente = par_cliente AND equipo.id_sucursal = par_sucursal AND<br/>
equipo.id_torneo = par_torneo AND equipo.id_equipo = tabla_pos.id_equipo<br/>
ORDER BY 9 DESC,8 DESC;<br/>
*/
<br/>
END
Copy linkTweet thisAlerts:
@NogDogSep 10.2019 — So what I think you need to do is run the CALL statement first via query(), and if that result is not false, then do another query() (or prepared statement?) to run your SELECT query against the temporary table tabla_pos; and _that_ should then return a statement object (assuming it runs okay) against which you can then loop for its results.
Copy linkTweet thisAlerts:
@Joak190867authorSep 10.2019 — @NogDog#1608535 Oks .... Thank you very much
×

Success!

Help @Joak190867 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 3.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...