Jump to content

Number of rows


dudejma

Recommended Posts

I keep getting this error:

 

mysql_num_rows(): supplied argument is not a valid MySQL result resource

 

I don't know what I should do. I've tried changing a couple of things but it still doesn't work. I have a lot of code, so if you want some of it, tell me which part and I'll try my best. Thanks!

Link to comment
Share on other sites

<?php

require 'include/sql.php';

$pilotid = $_GET['pid'];

$sql = "SELECT * FROM pireps WHERE pilotID='$pilotid' ORDER BY id DESC";
$sql2 = "SELECT pilotID, fname, lname, rank, vatsimID, hours FROM users WHERE pilotID='$pilotid'";

$result = mysql_query($sql)
or die ("An error has occured. Please contact thew webmaster with the following error: " . mysql_error());
$result2 = mysql_query($sql2) 
or die ("An error has occured. Please contact thew webmaster with the following error: " . mysql_error());

$num = mysql_num_rows($sql);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Virtual American Airlines</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="css/styles.css" />
</head>
<body>
<!-- Begin Wrapper -->
<div id="wrapper">
  <!-- Begin Header -->
  <div id="header"><img src="http://virtualamerican.org/images/screenshot.jpg" /></div>
  <!-- End Header -->
  <!-- Begin Navigation -->
   <div id="navigation"><?php include 'include/menu.php'; ?></div>
  <!-- End Navigation -->
  <!-- Begin Faux Columns -->
  <div id="faux">
    <!-- Begin Left Column -->
    <div id="leftcolumn"></div>
    <!-- End Left Column -->
    <!-- Begin Content Column -->
    <div id="content">
      <center><font size="5" color="red"><b>Pilot </font><font size="5" color="blue">Logbook</b></font></center>
  <br />
      <table align="center" border="0" width="100%" cellpadding="2px" cellspacing="0px" style="background-color:#D8D8D8">
  <?php
  while ($row2 = mysql_fetch_array($result2)) {
  echo '<tr>';
  echo '<td style="float:left"><b>' . $row2['pilotID'] . '</b></td>';
  echo '<td style="float:right">VATSIM ID: ' . $row2['vatsimID'] . '</td>';
  echo '</tr>';
  echo '<tr>';
  echo '<td style="float:left">' . $row2['fname'] . ' ' . $row2['lname'] . '</td>';
  echo '<td style="float:right">Total Flights: ' . $num . '</td>';
  echo '</tr>';
  echo '<tr>';
  echo '<td style="float:left">' . $row2['rank'] . '</td>';
  echo '<td style="float:right">Total Hours: ' . $row2['hours'] . '</td>';
  echo '</tr>';
  echo '<tr>';
  echo '<td style="float:left">';
  if ($row2['rank'] == 'New Hire') {
  echo '<img src="images/newHire.jpg">'; 
  } elseif ($row2['rank'] == 'First Officer') {
  echo '<img src="images/cap5th.jpg">';
  } elseif ($row2['rank'] == 'Captain 4th Class') {
  echo '<img src="images/cap4th.jpg">';
  } elseif ($row2['rank'] == 'Captain 3rd Class') {
  echo '<img src="images/cap3rd.jpg">';
  } elseif ($row2['rank'] == 'Captain 2nd Class') {
  echo '<img src="images/cap2nd.jpg">';
  } elseif ($row2['rank'] == 'Captain 1st Class') {
  echo '<img src="images/cap1st.jpg">';
  } elseif ($row2['rank'] == 'ATP Captain') {
  echo '<img src="images/ATP.jpg">';
  } elseif ($row2['rank'] == 'Senior ATP Captain') {
  echo '<img src="images/SATP.jpg">';
  }
  echo '</td>';
  echo '</tr>';
  }
  echo '</table>';
  ?>
  <p><br /></p>
  <table align ="left" border="0" width="100%" cellpadding="2px" cellspacing="2px">
  <tr bgcolor="#B2B2B2">
  <th>Flight #</th>
  <th>From</th>
  <th>To</th>
  <th>Flight Time</th>
  <th>Status</th>
  </tr>
  <?php
  while ($row = mysql_fetch_array($result)) {
  echo '<tr bgcolor="#D8D8D8" align="center">';
  echo "<td width=\"65px\">" . $row['flight'] . "</td>";
  echo "<td width=\"340px\">" . $row['from'] . "</td>";
  echo "<td width=\"60px\">" . $row['to'] . "</td>";
  echo "<td>" . $row['flightTime'] . "</td>";
  if ($row['status'] == 1) {
  	echo "<td width=\"100px\" style=\"color: green;\">Approved</td>";
  } elseif ($row['status'] == 2) {
  	echo "<td width=\"100px\" style=\"color: blue;\">Declined</td>";
  } elseif ($row['status'] == 0) {
  	echo "<td width=\"100px\" style=\"color: red;\">Pending</td>";
  } 
  echo "</tr>";
  }
  echo "</table>";
  ?>
      </div>
    <!-- End Content Column -->
    <!-- Begin Right Column -->
<!-- Events Table -->
    <div id="rightcolumn">	</div>

  <!-- End Faux Columns -->
  <!-- Begin Footer -->
<div id="footer"><?php include 'include/footer.php'; ?></div>
  <!-- End Footer -->
</div>
<!-- End Wrapper -->
</body>
</html>

Link to comment
Share on other sites

The associated query is failing, therefore mysql_num_rows doesn't receive a result resource to work with. You need to figure out why the query is failing and remedy that situation. In reality, you should make sure the query is successful before allowing anything to act on the result resource anyhow. You wouldn't want to echo all the information below to the browser on a live site, rather you'd log it and echo a generic, "Sorry, no soup for you" error message to the user.

 

$query = "SELECT fields FROM table WHERE field = 'value'";
if( !$result = mysql_query($query) ) {
     echo "<br>Query: $query<br>In file: " . __FILE__ . "<br>On line: " . __LINE__ . "<br>Failed with error: " . mysql_error() . '<br>';
} else {
     echo mysql_num_rows($result);
     // or whatever other code you need to execute contingent on a successful query.
}

Link to comment
Share on other sites

this is most likely being caused because you have not checked if your $_GET variable is set..

 

$pilotid = $_GET['pid'];

if(isset($pilotid)){
$sql = "SELECT * FROM pireps WHERE pilotID = '$pilotid' ORDER BY id DESC";
$sql2 = "SELECT pilotID, fname, lname, rank, vatsimID, hours FROM users WHERE pilotID = '$pilotid'";

$result = mysql_query($sql)
or die ("An error has occured. Please contact thew webmaster with the following error: " . mysql_error());
$result2 = mysql_query($sql2) 
or die ("An error has occured. Please contact thew webmaster with the following error: " . mysql_error());

$num = mysql_num_rows($sql);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.