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 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2012
    Posts
    87
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Exclamation Why am I getting these error and warning?

    I'm getting the following warning and error when I run my php file:
    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/a3763404/public_html/search.php on line 102
    Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-20, 20' at line 1
    The concerned PHP code is:

    Code:
    <?php
    
    
    $name=$_POST['name'];
    if ($name="")
     {
      echo "Oops! You didn't type anything to search. Please insert something to search";
     }
    else
     {
      // 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);
    
    $query = mysql_query("SELECT * FROM entries LIMIT $startResults, $resultsPerPage") or die ('Error: '.mysql_error ()); 
    
    while ($output = mysql_fetch_array($query))
    { 
        echo '<a href="selected_post.php?Index='.$output['Index'].'">' .$output['Title'].'</a><br>'; 
    }
    
    $halfPages = floor($numberOfPages / 2);
    $range = array('start' => 1, 'end' => $totalPages);
    $isEven = ($numberOfPages % 2 == 0);
    $atRangeEnd = $totalPages - $halfPages;
    
    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;';
    }
    ?>

  • #2
    New Coder
    Join Date
    Sep 2012
    Posts
    87
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Exclamation RE: Why am I getting these error and warning?

    The PHP code is meant to receive input from a search form. The php in the search.php file (see below) should search for the inputted text in three columns on a table: Title, Post and Date. If it finds matches, it lists only the titles 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 have not understood Arcticwarrio statement that "Date is a reserved word you'll need backsticks when referencing it `date`". What am I meant to do with the Date word?

    Please help. Thank you.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,590
    Thanks
    0
    Thanked 644 Times in 634 Posts
    You can't start retrieving results from position -20 in the resultset. the first entry in the results is position 1 so you are 21 entries before the first one with your query.

    The line setting the start positiion to -20 would be:

    $startResults = ($page - 1) * $resultsPerPage;

    presumably $resultsPerPage is 20 and $page is 0 giving -20. If that is the case then the start position should be set using:

    $startResults = ($page * $resultsPerPage) +1;
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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