SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: Search facility

  1. #1
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Search facility

    Hey,

    I am trying to do a search through my database. I have the following code:-

    PHP Code:
            <?php
            
            
    include("../conn.php");
                    
            
    $search $_POST['c_name'];

            
    $sql mysql_query("SELECT * FROM tbl_job_reviews WHERE company LIKE '%search%' LIMIT 0, 30 ");        
            
    $result mysql_query($sql);
            
    $numrows mysql_num_rows($result);

            
    //error message (not found message)
            
    $XX "No Record Found";

            echo 
    "<p>You searched for <span style='color:#009966'>$_POST[c_name] $_POST[loc]</span> - 2 reviews found</p>";
         
             echo 
    "<br/>";

            while (
    $row mysql_fetch_assoc($result))
            {
            
    $variable1=$row["company"];
            
    $variable2=$row["job_location"];
            
    $variable3=$row["town_location"];
            
            echo 
    "$variable1, and this print the variable2.";
            } 
            
            if (!
    $variable1)
            {
            echo 
    $XX;
            }     
            
    ?>
    But this does not seem to work, i get the following errors:-

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /domains/judgethejob.com/http/vinny/search/index.php on line 232

    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /domains/judgethejob.com/http/vinny/search/index.php on line 238
    Any ideas?

    Regards
    Billy

  2. #2
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes
    if you are experienced an error like this, you should echo mysql_error() to see what is going wrong with database.

  3. #3
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I get this error message when echoing mysql_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 'Resource id #5' at line 1
    I think the syntax in my SQL looks fine, i dont know what this error is referring to, sql is shown below:-

    PHP Code:
            $search $_POST['c_name'];

            
    $sql mysql_query("SELECT * FROM tbl_job_reviews WHERE company LIKE '%$search%'");        
            
    $result mysql_query($sql) or mysql_error();
            
    $numrows mysql_num_rows($result); 
    Can u see anything wrong?

  4. #4
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    looks like this error message belongs to another query

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put the SQL query into it's own variable, and then echo that variable to see what query is actually getting executed.
    Also try executing the SQL directly using phpmyadmin (or similar), to make sure it works.

    for example -
    Code PHP:
    $search = $_POST['c_name'];
     
    $sql_query = "SELECT * FROM tbl_job_reviews WHERE company LIKE '%$search%'";
    echo $sql_query;
     
    $sql = mysql_query($sql_query);
    $result = mysql_query($sql) or mysql_error();
    $numrows = mysql_num_rows($result);

    From memory, I think I've previously had issues with using LIKE and wildcards.

  6. #6
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hehe
    This wrong example may explain an error message mentioned above
    Newbies thinks alike

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I just noticed the big fault in that code.
    I was about to ask what you were on about Shrapnel!

    There's two calls to mysql_query.

    This code should work a bit better-
    Code PHP:
    $search = $_POST['c_name'];
     
    $sql = "SELECT * FROM tbl_job_reviews WHERE company LIKE '%$search%'"; 
    // echo $sql; // uncomment if needed for diagnostics
    $result = mysql_query($sql) or mysql_error();
    $numrows = mysql_num_rows($result);

  8. #8
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah and second call will produce error mentioned above,
    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 'Resource id #5' at line 1
    So, because it is quite hard to put Resource type into POST variable, I presume that this error message belongs to another query, not shown to us.

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shrapnel_N5 View Post
    So, because it is quite hard to put Resource type into POST variable, I presume that this error message belongs to another query, not shown to us.
    Would that not depend on how php would echo a mysql resultset back into another mysql_query?
    Infact, doesn't php echo resource id when you try echoing a mysql resultset?
    (I'd try it, but not got any suitable DB or scripts set-up to try just now)

    Edit - Curiousity got the better of me.
    Echoing a mysql_result set, does produce "Resource id #xx", where xx is a number, which would explain the above error message.

  10. #10
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it is resource type that mysql_query returns.
    And it becomes "Resource ##" when converted to string

  11. #11
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Never mind guys,

    I have changed the code to this:

    PHP Code:
              $name $_POST['c_name']; 
              
    $location $_POST['loc']; 
                        
              
    $sql="SELECT * FROM tbl_job_reviews WHERE company LIKE '%" $name .  "%' OR job_location LIKE '%" $location ."%'"
              
    $result=mysql_query($sql); 
              
    $numrows mysql_num_rows($result); 
    Regards

  12. #12
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You tried to pass the mysql resource to mysql_query function which originally needs a sql query in it as the first parameter. That's why it was giving error.
    PHP Code:
    $sql mysql_query($sql_query);
    $result mysql_query($sql) or mysql_error(); // where the $sql is the mysql resource returned by mysql_query 
    To be safe from such errors, try to look the function at php manual once and its example.
    But TYPO is always there for even an experienced too.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


Bookmarks

Posting Permissions

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