Jump to content

mysql_num_rows


Sfrius

Recommended Posts

Hello,

        I cannot figure out why the following code is giving me problems.

 

Here it works.

 

$AssignedOpportunities = array();
$SQLstring = "SELECT opportunityID FROM $TableName " .
          " WHERE date_approved IS NOT NULL";
$QueryResult = @mysql_query($SQLstring, $DBConnect);
if (mysql_num_rows($QueryResult) > 0) {
     while (($Row = mysql_fetch_row($QueryResult)) !== FALSE)
          $AssignedOpportunities[] = $Row[0];
     mysql_free_result($QueryResult);
}

 

Here it doesn't work

 

 

$TableName = "opportunities";
$Opportunities = array();
$SQLstring = "SELECT opportunityID, company, city, " .
          " start_date, end_date, position, description " .
          " FROM $TableName";
$QueryResult = @mysql_query($SQLstring, $DBConnect); 
if (mysql_num_rows($QueryResult) > 0) {          //LINE CONTAINING THE ERROR
     while (($Row = mysql_fetch_assoc($QueryResult)) !== FALSE)
          $Opportunities[] = $Row;
     mysql_free_result($QueryResult);
}

 

Link to comment
https://forums.phpfreaks.com/topic/287524-mysql_num_rows/
Share on other sites

The line

$QueryResult = @mysql_query($SQLstring, $DBConnect); 

suppresses error messages from the query http://www.php.net/manual/en/language.operators.errorcontrol.php As a general rule suppressing errors is not a good idea so get rid of the @

Then, the next line gives an error because $QueryResult contains false.

To see the error when the query fails try:

$QueryResult = mysql_query($SQLstring, $DBConnect) or die (mysql_error($DBConnect)); 

http://www.php.net/manual/en/function.mysql-error.php which will print the error that caused your query to fail then stop.

Link to comment
https://forums.phpfreaks.com/topic/287524-mysql_num_rows/#findComment-1474997
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.