来自复杂查询的MySQLi错误

时间:2017-07-09 11:16:26

标签: php mysql mysqli

我正在使用以下MySQL查询:

CREATE TEMPORARY TABLE 
temp_table
ENGINE=MyISAM 
AS (SELECT age, 
           performance, 
           wind,
           name,
           surname,
           dob,
           prov,
           pos,
           place,
           date, 
           @curRank := 
                 IF(@prevRank = performance, 
                    @curRank, 
                    @incRank) AS rank, 
           @incRank := @incRank + 1, 
           @prevRank := performance, 
           CONCAT(name,' ',surname) AS 'athlete' 
      FROM results p, 
           (SELECT @curRank :=0, 
                   @prevRank := NULL, 
                   @incRank := 1 ) r 
     WHERE event='60m' 
       AND gender='male' 
       AND age='senior' 
       AND year='2017' 
       AND wind<=2 
       AND wind<>'NW' 
     ORDER BY performance ASC);

SELECT @curRank := 
         IF(@prevRank = performance, 
            @curRank, 
            @incRank) AS rank,
       @incRank := @incRank + 1 temp1,
       @prevRank := performance temp2, 
       rank pos,
       MIN(performance) performance, 
       wind,
       athlete,  
       dob, 
       prov, 
       pos, 
       place, 
       date 
  FROM temp_table, 
       (SELECT @curRank :=0, 
               @prevRank := NULL, 
               @incRank := 1, 
               @s:=0) r 
 GROUP BY athlete 
 ORDER BY performance ASC LIMIT 0,500

我使用以下内容在PHP中测试结果:

<?php

$con=mysqli_connect("localhost","***","***","***");
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"CREATE TEMPORARY TABLE temp_table ENGINE=MyISAM AS (SELECT age, performance, wind,name,surname,dob,prov,placing,place,date, @curRank := IF(@prevRank = performance, @curRank, @incRank) AS rank, @incRank := @incRank + 1, @prevRank := performance, CONCAT(name,' ',surname) AS 'athlete' FROM results p, ( SELECT @curRank :=0, @prevRank := NULL, @incRank := 1 ) r WHERE event='60m' AND gender='male' AND age='senior' AND year='2017' AND wind<=2 AND wind<>'NW' ORDER BY performance ASC) SELECT @curRank := IF(@prevRank = performance, @curRank, @incRank) AS rank,@incRank := @incRank + 1 temp1,@prevRank := performance temp2, rank pos,MIN(performance) performance, wind,athlete, dob, prov, placing, place, date FROM temp_table, (SELECT @curRank :=0, @prevRank := NULL, @incRank := 1, @s:=0) r GROUP BY athlete ORDER BY performance ASC LIMIT 0,500");

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
    echo "<tr>";
    echo "<td>" . $row['rank'] . "</td>";
    echo "<td>" . $row['pos'] . "</td>";
    echo "<td>" . $row['athlete'] . "</td>";
    echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>

但是我收到以下错误:

警告:mysqli_fetch_array()要求参数1为mysqli_result,第17行/home/seefle0/public_html/test.php中给出布尔值

我可以做些什么来修复PHP中的查询?

1 个答案:

答案 0 :(得分:0)

添加条件:

if($result === FALSE) { 
    yourErrorHandlerFn(mysqli_error($con));
}

之前

while($row = mysqli_fetch_array($result))
{

您将收到错误。