警告:mysqli_num_rows()期望参数1为mysqli_result,在/ Users /中给出null

时间:2017-05-01 03:28:58

标签: php mysql mysqli

有人可以帮忙吗? 我被要求从champs和teamstats表中挑选团队,teamid,pennants和worldseries专栏。并在champs表中打印棒球队的数量。我有2个文件如下。我不停地收到警告:警告:mysqli_num_rows()期望参数1为mysqli_result,在/ Users /

中给出null
<?php //FIRST FILE: mysqli_connect.php

DEFINE ('DB_USER', 'happy');
DEFINE ('DB_PASSWORD', 'xxxx');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'baseball_stats');

// Make the connection:
 $dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR 
 die ('Could not connect to
 MySQL: ' . mysqli_connect_error() );

 // Set the encoding...
 mysqli_set_charset($dbc, 'utf8');
 ?>
 //// SECOND FILE 
 <?php

require ('a9mysqli_connect.php');

$query = "SELECT t.team AS T, t.team_id AS Ti, c.pennants AS P, 
c.worldseries AS W from `teamstats` as t join `champs` as c on 
t.team_id = c.team_id;";

 $result = @mysqli_query ($dbc, $query); 

 // Count the number of returned rows:
$num = mysqli_num_rows($result);
//Create an HTML table for displaying the query results:

if ($num > 0) {  // If it ran OK, display the records.
echo "<p>There are currently $num baseball teams.</p>"; 

echo '<table align="center">
<tr>
<td align="left"><b>Team</b></td>
<td align="right"><b>Team ID</b></td>
<td align="right"><b>Pennants </b></td>
<td align="right"><b>WorldSeries </b></td>
</tr>'
;
//Fetch and print each returned record:

while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC)) {
echo '<tr><td align="left">' . $row['T'] . '</td><td align= "left">' . 
$row['Ti'] . '</td><td align= "left">' . $row['P'] . '</td><td align= 
"left">' . $row['W'] . '</td></tr>'; 
 }
 echo '</table>';
mysqli_free_result ($result);
}
else { //If it didnot run OK.

 // Public message:
 echo '<p class="error"> There are currently no baseball teams in
 the database.</p>';
  echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $query . 
  '</p>';
 }
 mysqli_close($dbc); // Close the database connection.
?>

2 个答案:

答案 0 :(得分:0)

mysqli_query对于成功的查询,它将返回TRUE,失败时为FALSE,您需要在代码检查中检查查询是否成功 if ($result)

<强>更新

这是因为您使用在另一个文件$dbc

中声明的mysqli_connect.php
<?php //FIRST FILE: mysqli_connect.php

DEFINE('DB_USER', 'happy');
DEFINE('DB_PASSWORD', 'xxxx');
DEFINE('DB_HOST', 'localhost');
DEFINE('DB_NAME', 'baseball_stats');

// Make the connection:
$dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR
die ('Could not connect to
 MySQL: ' . mysqli_connect_error());

// Set the encoding...
mysqli_set_charset($dbc, 'utf8');
?>

然后在第二个php文件中,你需要像require ('a9mysqli_connect.php');这样的文件,这个文件不存在。运行代码,因为它有以下错误。

enter image description here

错误很明显,您将一个不存在的变量传递给mysqli_query,您需要正确地要求该文件。

<?php

require('mysqli_connect.php');
$query = "SELECT t.team AS T, t.team_id AS Ti, c.pennants AS P, 
c.worldseries AS W from `teamstats` as t join `champs` as c on 
t.team_id = c.team_id;";
$result = mysqli_query($dbc, $query); // remove @
if ($result) {
    // Count the number of returned rows:
    $num = mysqli_num_rows($result);
    //Create an HTML table for displaying the query results:

    if ($num > 0) {  // If it ran OK, display the records.
        echo "<p>There are currently $num baseball teams.</p>";

        echo '<table align="center">
                <tr>
                    <td align="left"><b>Team</b></td>
                    <td align="right"><b>Team ID</b></td>
                    <td align="right"><b>Pennants </b></td>
                    <td align="right"><b>WorldSeries </b></td>
                </tr>';
        //Fetch and print each returned record:

        while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
            echo '<tr><td align="left">' . $row['T'] . '</td><td align= "left">' . $row['Ti'] . '</td><td align= "left">' . $row['P'] . '</td><td align="left">' . $row['W'] . '</td></tr>';
        }
        echo '</table>';
        mysqli_free_result($result);
    } else { //If it didnot run OK.

        // Public message:
        echo '<p class="error"> There are currently no baseball teams in the database.</p>';
        echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $query .
            '</p>';
    }
    mysqli_close($dbc); // Close the database connection.
} else {
    echo 'Something wrong happened!!!';
}

答案 1 :(得分:0)

  1. 在“描述”下的php.net上查看mysqli_query
  2.   

    对于非DML查询(不是INSERT,UPDATE或DELETE),此函数是   类似于调用mysqli_real_query()后跟任一个   mysqli_use_result()或mysqli_store_result()。

    1. 请参阅php.net下的“返回值”下的mysqli_store_result
    2.   

      此函数之后返回FALSE的可能原因   成功调用mysqli_query()可能会导致结果集太大(内存   因为它无法分配)。如果mysqli_field_count()返回一个   非零值,该语句应该产生非空结果   集。

      我还写了一个左连接sql查询,也许你可以使用它。您使用了 JOIN 关键字。我使用 LEFT JOIN 。但我也测试了 JOIN 。它工作正常,所以你的查询还可以。

      <强> 描述 : 阅读所有团队(来自团队表),并为每个团队,还有相应的冠军细节(来自 champs 表的三角旗和世界系列)。但是只有team_id也在冠军队伍中的球队存在(ISNULL(c.team_id) = FALSE)。

      SELECT 
          t.team AS T,
          t.team_id AS Ti,
          c.pennants AS P,
          c.worldseries AS W
      FROM
          `teamstats` AS t
              LEFT JOIN
          `champs` AS c ON t.team_id = c.team_id
      WHERE
          ISNULL(c.team_id) = FALSE
      ;