Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Sep 2012
    Posts
    106
    Thanks
    22
    Thanked 0 Times in 0 Posts

    Question What's wrong with these functions?

    What's wrong with the functions mysql_num_rows() and mysql_fetch_array() in the php code below:

    Code:
    <?php
    
    if(isset($_POST['submit']))
    {
    if(isset($_GET['go']))
      {
         if(preg_match("/[A-Z | a-z]+/", $_POST['name']))
    	    {
              $name=$_POST['name'];
    
              // Include database connection settings
              include('config.inc'); 
    
             //-select the database to use
             $mydb=mysql_select_db("a3763404_posts");
    
            //-query the database table
            $sql="SELECT Title, Post, Date FROM entries WHERE Title LIKE '%" . $name . "%' OR Post LIKE '%" . $name ."%' OR Date LIKE '%" . $name ."%'LIMIT $startResults, $resultsPerPage";  
    
             //-run the query against the mysql query function
             $result=mysql_query($sql);
    
            //-count results
    
            $numrows=mysql_num_rows($result);
    
            echo "<p>" .$numrows . " Results found for " . stripslashes($name) . "</p>"; 
    
            $numberOfPages = 100;
            $resultsPerPage = 20;
            $startResults = ($page - 1) * $resultsPerPage;
            $numberOfRows = $numrows;
            $totalPages = ceil($numberOfRows / $resultsPerPage);
    	 
    	    $halfPages = floor($numberOfPages / 2);
            $range = array('start' => 1, 'end' => $totalPages);
            $isEven = ($numberOfPages % 2 == 0);
            $atRangeEnd = $totalPages - $halfPages;
    
               while ($output = mysql_fetch_array($query))
                 { 
          
    	           $query = mysql_query("SELECT * FROM entries LIMIT $startResults, $resultsPerPage") or die ('Error: '.mysql_error ()); 
                   echo '<a href="selected_post.php?Index='.$output['Index'].'">' .$output['Title'].'</a><br>'; 
                 }
    
    
                     if($isEven) $atRangeEnd++;
    
                     if($totalPages > $numberOfPages)
                        {
    	                  if($page <= $halfPages)
    		              $range['end'] = $numberOfPages;
    	                  elseif ($page >= $atRangeEnd)
    		              $range['start'] = $totalPages - $numberOfPages + 1;
    	                 else
    	                     {
    		                   $range['start'] = $page - $halfPages;
    		                   $range['end'] = $page + $halfPages;
    		                   if($isEven) $range['end']--;
    	                     }
                        }
    
                      if($page > 1)
    	              echo '<a href="?page='.($page - 1).'">&laquo;</a>&nbsp';
    
                      for ($i = $range['start']; $i <= $range['end']; $i++)
                           {
    	                      if($i == $page)
    		                  echo '<strong>'.$i.'</strong>&nbsp;';
    	                      else
    		                  echo '<a href="?page='.$i.'">'.$i.'</a>&nbsp;';
                           }
                     if ($page < $totalPages)
    	             echo '<a href="?page='.($page + 1).'">&raquo;</a>&nbsp;';
            }
    	else
            {
               echo "<p>Please enter a search query</p>";
            }	
      }
    }
    ?>
    The PHP code is supposed to receive input from a search form. The php should search for the inputted text in three columns on a table in a database: Title, Post and Date. If it finds matches, it lists only the value in the title column of the rows in which the word being searched has been found. If the searches are many, the first twenty appear on page 1, the next 20 on page 2 and so on. If no matches are found, the php code echoes "No matches found."

    I have tried coming up with the following code but it isn't working as I'd want it to. First, I excluded the Date column from the searchable areas because the code was generating errors when I included Date column. Secondly, the php is listing every title of rows in my table.

    I'm getting the following warnings when I run the php code

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/a3763404/public_html/search.php on line 102
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a3763404/public_html/search.php on line 117
    Please help. Thank you.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,864
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Change
    PHP Code:
             $result=mysql_query($sql); 
    to
    PHP Code:
             $result=mysql_query($sql) or die(mysql_error(). 'query:' $sql); 
    and checl the output.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •