Jump to content


Photo

displaying $num_rows before rows appear?


  • Please log in to reply
10 replies to this topic

#1 87dave87

87dave87
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts

Posted 25 October 2006 - 11:29 PM

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

<?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 for the results to be displayed is: -

        <?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'>"; 
} 
?>


#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 October 2006 - 11:33 PM

You still need a query... Do you want it to show the total in emulators, or just the total found once searched?

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 87dave87

87dave87
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts

Posted 25 October 2006 - 11:39 PM

total found.

So if some types in 'stella', at the moment that gives 1, so it will display '1 emulator found'

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 October 2006 - 11:52 PM

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

<?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'>"; 
} 
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 87dave87

87dave87
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts

Posted 26 October 2006 - 12:01 AM

no... if you do a search now its getting an error for the num_rows??

#6 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 26 October 2006 - 12:04 AM

Sorry, typo on my part.  I corrected it now, can you copy and paste and try again.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#7 87dave87

87dave87
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts

Posted 26 October 2006 - 12:20 AM

it works, but as the bar is outside of my result code, I can't do anything with it still.

#8 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 26 October 2006 - 12:21 AM

What bar?

Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#9 87dave87

87dave87
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts

Posted 26 October 2006 - 12:23 AM

the yellow 'search results bar' with the php error... thats where im putting the 'found emulators' code.

#10 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 26 October 2006 - 12:34 AM

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

Change this:

<?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>";  
?>

To this:

<?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>";  
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#11 87dave87

87dave87
  • Members
  • PipPipPip
  • Advanced Member
  • 86 posts

Posted 26 October 2006 - 12:43 AM

k, just done a bit of editting to that and its working fine! thanks a lot... not sure how this has actually worked... but its great!  Ill study it later on.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users