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
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
Link to comment
Share on other sites

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