简体   繁体   中英

SQL query doesn't give results in PHP through MySQLi

I have this SQL query founded here on stackoverflow and adjusted to match my needs, that works in phpMyAdmin:

$queryClassifica = "SELECT Squadra, 
SUM( CASE WHEN PuntiCasa > PuntiTrasferta THEN 2 ELSE 0 END ) punti, 
COUNT( CASE WHEN PuntiCasa >0 AND PuntiTrasferta >0 THEN 1 END ) giocate, 
COUNT( CASE WHEN PuntiCasa > PuntiTrasferta THEN 1 END ) vinte, 
COUNT( CASE WHEN PuntiTrasferta > PuntiCasa THEN 1 END ) perse, 
SUM( PuntiCasa ) PuntiCasa, 
SUM( PuntiTrasferta ) PuntiTrasferta, 
SUM( PuntiCasa ) - SUM( PuntiTrasferta ) diff_canestri
FROM ( 
SELECT SquadraCasa Squadra, PuntiCasa, PuntiTrasferta
FROM calendario
UNION ALL 
SELECT SquadraTrasferta, PuntiTrasferta, PuntiCasa
FROM calendario
)a
GROUP BY Squadra
ORDER BY Punti DESC , diff_canestri DESC";

In my PHP page I submit the same query, but it doesn't work, if I check the rows number it's always 0.

$result = mysqli_query($con, $queryClassifica);
$row_cnt = $result->num_rows;

And of course, if I fetch the result of the query, the array is empty. Where's the mistake?

It looks like you're combining both object orientated style as well as procedural style. If your code is written in object orientated style:

$result = $mysqli->query($queryClassifica);
$row_cnt = $result->num_rows;

$result->close();
$mysqli->close();

or procedural style:

$result = mysqli_query($link,$queryClassifica);
$row_cnt = mysqli_num_rows($result);

mysqli_free_result($result);
mysqli_close($link);

I'd say that you've most likely written it procedural however try both.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM