Jump to content

displaying $num_rows before rows appear?


87dave87

Recommended Posts

Hi,  I have a search results page and I want to display $num_rows (so I can put Found 'x' downloads), but my code which has If statements from when it is started is below where I want the $num_rows to appear.

e.g. http://www.emulators.cc/  -  type 'atari' into the search box at the top and click go... this will display the results but I can't get the 'emulators found' part to work.

This is because im not declaring the sql query first, although ive tried to do this...

The code for the 'found 'x' emulators is:-

[code]
<?php
$database="database"; 
mysql_connect ("localhost", "user", "pass"); 
@mysql_select_db($database) or die( "Unable to select database");
$num_rows = mysql_num_rows($sql);
echo "<p class='footer'>"; 
echo $num_rows;
echo " emulators found</p>"; 
?>
[/code]

Code for the results to be displayed is: -

[code]
        <?php
if(isset($_POST["emusearch"]) && strlen(trim($_POST["emusearch"])) > 0){
    $database="database"; 
    mysql_connect ("localhost", "user", "pass"); 
    @mysql_select_db($database) or die( "Unable to select database"); 
    $sql = "select emulator, version, os, platform, details from windows_atari2600 where emulator LIKE '%".mysql_real_escape_string($_POST["emusearch"])."%' OR platform LIKE '%".mysql_real_escape_string($_POST["emusearch"])."%' order by platform asc, emulator asc"; 
    if(!$rs = mysql_query($sql)){ 
        echo "Error<br>".mysql_error()."<br>".$sql; 
            exit(); 
  }
    if(!mysql_num_rows($rs)){ 
        echo "<tr>"; 
echo "<meta http-equiv='refresh' content='0; URL=http://www.google.com/foundnorecordspage'>";
        echo "</tr>\n"; 
    }else{ 
        while ($get_info = mysql_fetch_row($rs)) {   
            echo "<tr>";   
            foreach ($get_info as $field) echo "<td>$field</td>\n"; 
            echo "</tr>\n"; 
        } 
    } 
}else{
    echo "<meta http-equiv='refresh' content='0; URL=http://www.google.com/searchcriteriarequired'>";
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/25109-displaying-num_rows-before-rows-appear/
Share on other sites

ok, modified from your code above, give this a try:

[code]<?php
if (isset($_POST['emusearch']) && strlen(trim($_POST['emusearch'])) > 0){
  $database="database"; 
  mysql_connect ("localhost", "user", "pass"); 
  @mysql_select_db($database) or die( "Unable to select database"); 
  $sql = "select emulator, version, os, platform, details from windows_atari2600 where emulator LIKE '%".mysql_real_escape_string($_POST["emusearch"])."%' OR platform LIKE '%".mysql_real_escape_string($_POST["emusearch"])."%' order by platform asc, emulator asc";

  if(!$rs = mysql_query($sql)){ 
      echo "Error<br>".mysql_error()."<br>".$sql; 
      exit(); 
  }

  if(!mysql_num_rows($rs)){ 
      echo "<tr>";
      echo "<meta http-equiv='refresh' content='0; URL=http://www.google.com/foundnorecordspage'>";
      echo "</tr>\n"; 
  }
  else{
      $num_rows = mysql_num_rows($rs);
      echo "$num_rows results found<br>\n<br>\n";
      while ($get_info = mysql_fetch_row($rs)) {   
        echo "<tr>";   
        foreach ($get_info as $field){
            echo "<td>$field</td>\n"; 
        }
            echo "</tr>\n";   
      } 
  }
}
else{
    echo "<meta http-equiv='refresh' content='0; URL=http://www.google.com/searchcriteriarequired'>";
}
?>[/code]

Regards
Huggie
Oh I see what you mean, my bad...

Change this:

[code]<?php
$database="database"; 
mysql_connect ("localhost", "user", "pass"); 
@mysql_select_db($database) or die( "Unable to select database");
$num_rows = mysql_num_rows($sql);
echo "<p class='footer'>"; 
echo $num_rows;
echo " emulators found</p>"; 
?>[/code]

To this:

[code]<?php
$database="database"; 
mysql_connect ("localhost", "user", "pass"); 
@mysql_select_db($database) or die( "Unable to select database");
$sql = "select emulator, version, os, platform, details from windows_atari2600 where emulator LIKE '%".mysql_real_escape_string($_POST["emusearch"])."%' OR platform LIKE '%".mysql_real_escape_string($_POST["emusearch"])."%' order by platform asc, emulator asc";
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
echo "<p class='footer'>"; 
echo $num_rows;
echo " emulators found</p>"; 
?>[/code]

Regards
Huggie

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.