-2

Can Someone please Help ?? I am asked to pick up the team, teamid, pennants, and worldseries columns from the champs and teamstats tables. and print the number of baseball teams in the champs table. I have 2 files as following. I keep getting warnings saying Warning:mysqli_num_rows()expects parameter 1 to be mysqli_result, null given in /Users/

<?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.
?>
N.Ali
  • 61
  • 1
  • 9
  • 2
    Possible duplicate of [mysqli\_fetch\_array()/mysqli\_fetch\_assoc()/mysqli\_fetch\_row() expects parameter 1 to be resource or mysqli\_result, boolean given](http://stackoverflow.com/questions/2973202/mysqli-fetch-array-mysqli-fetch-assoc-mysqli-fetch-row-expects-parameter-1) – Mayank Pandeyz May 01 '17 at 03:30
  • 1
    why `@mysqli_query (` and not `mysqli_query (` – Jason Joslin May 01 '17 at 03:31
  • your querry failed, debug that –  May 01 '17 at 03:34
  • 1
    When you ask a question about an error **ALWAYS** include the **error log**. Add `error_reporting(E_ALL); ini_set('display_errors', 1);` at the top of your `php` script, what does it return? – Pedro Lobito May 01 '17 at 03:54
  • **WARNING**: Do not suppress errors when calling methods with the `@` operator. If something goes wrong you want to know about it and will need to take corrective action, display a useful message for the user, log the problem, or all that and more. It also makes debugging issues like this a whole lot more complicated if you ignore errors that are trying to point out serious problems. – tadman May 01 '17 at 03:55

2 Answers2

0

mysqli_query for successful queries it will return TRUE, FALSE on failure, you need to check if the query was successful, in your code check if ($result)

UPDATE

This happens because you use $dbc which is declared in another file 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');
?>

Then in the second php file you require the file like require ('a9mysqli_connect.php'); and this file doesn't exists. running the code as it is i got the following error.

enter image description here

The error is clear you are passing a variable that doesn't exists to mysqli_query, you need to require the file correctly.

<?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!!!';
}
Junius L
  • 15,881
  • 6
  • 52
  • 96
0
  1. See mysqli_query on php.net under "Description":

For non-DML queries (not INSERT, UPDATE or DELETE), this function is similar to calling mysqli_real_query() followed by either mysqli_use_result() or mysqli_store_result().

  1. See mysqli_store_result on php.net under "Return Values":

Also possible reason for this function returning FALSE after successfull call to mysqli_query() can be too large result set (memory for it cannot be allocated). If mysqli_field_count() returns a non-zero value, the statement should have produced a non-empty result set.

I also wrote a left join sql query, maybe you can use it. You used the JOIN keyword. I used LEFT JOIN. But I tested also with JOIN. It worked, so your query is ok.

Description: Read all teams (from teams table) and, for each of them, also the corresponding champs details (pennants and worldseries from champs table). But only the teams who's team_id also in champs exists (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
;