A PHP Script to Search a MySQL Database




Here’s the special discount link for Rob’s course: http://johnmorrisonline.com/cwdc Check out my web developer resources here: …

source


26 responses to “A PHP Script to Search a MySQL Database”

  1. Hi John, I love this video and I hope you can help me take it a step farther; what if we wanted our search results to come back as a dynamic link that could be clicked and generate a new page that has more information on what you searched? Cheers

  2. error occurs. I'm sure there are no any typo mistakes. Checked the codes twice.

    Warning: Missing argument 1 for search::search(), called in /my/directory/index.php on line 4 and defined in /my/directory/class-search.php on line 13

    Fatal error: Call to a member function real_escape_string() on a non-object in /my/directory/class-search.php on line 14

    Does anyone know the fix?

  3. Hi John. Thank you for this video and script. I have used it, its really good. I like the way it is developed using OOP. Its a great script. The best i found so far to support a PHP search script i was looking for. I had 2 notice messages which i resolved, realising the 2 variables were not yet being processed until form submission. I also modified the script to add in a user message, should a search query return no results. I did this by adding an echo statement into the > if statement, > ! $query->num-rows, before the > return false; statement. I echo a message into a <div> tag, which i can then target on my front end using maybe Jquery append(). The only question that i have about the whole script is regarding the search form itself and the, Input type > search, and the > "value" parameter. What is the reason for echoing the > Search Term, back into the > "value" within the search input. Could you explain the reason for that ? I was able to remove the "value" from the input type, if its not necessary, but i'm trying to understand what the reason for it may be. It would be great if you could just clarify this. Its the only part that's left me scratching my head. Thanks again for such a great script.

  4. I'm getting the following errors when I pull up the "search.php" in Chrome. I just get an HTTP 500 error when I open in IE.
    PHP Notice: Undefined variable: search_term in on line 32
    PHP Notice: Undefined variable: search_results in on line 37

    I'm using MS Webmatrix to write the actual code, and I created my database on Wamp. I'm relatively new to PHP so any help or suggestions would be greatly appreciated.

  5. Excellent tutorial and code – very easy to follow. I have adapted and changed the functionality a little. Results to a DataTable, etc. One thing I haven't figured out – how would you add a "no results" message? I'm looking at the "if ( ! $query->num_rows ) {
    return false;" block, but so far all my attempts haven't worked.

  6. First I want to thank you for a great instruction video. It's close to what I want to achieve on a section of my own web page, though I'm trying to tweak it a bit to my own purposes, even if I'm next to a total php-noob.

    Quick question: I want my list of results to display data from a different db column from the one I'm searching in, for instance I want to search in a "Country"-column, but have the results listed as corresponding titles from a "Titles" column. What part of the code do I need to change and how?

  7. All I want to see is the result such as:

    [routing_number] => 012345
    [document_type] => Petition for Degrees
    [document_desc] => Testing uploading an image of this document

    How can I omit this:
    Array
    (
    [count] => 3
    [results] => Array
    (
    [0] => stdClass Object
    (
    [routing_number] => 012345
    [document_type] => Petition for Degrees
    [document_desc] => Testing uploading an image of this document
    )

    [1] => stdClass Object
    (
    [routing_number] => 123456
    [document_type] => Invitation
    [document_desc] => Testing uploading images to this document
    )

    [2] => stdClass Object
    (
    [routing_number] => 456789
    [document_type] => Invitation
    [document_desc] => This is to test uploading of image of the document
    )

    )

    )

  8. Can someone please help me resolve this error when I run the class search form
    Notice: Trying to get property of non-object line 58

    // Check results
    if ( ! $query->num_rows ) {
    return false;
    }
    Thank You

  9. +John Morris I tried expanding upon the query in the "search" class to look for the search terms in multiple tables of my database like so:

    SELECT *.files, *.users 
    FROM files, users
    WHERE username LIKE '%{$sanitized}%'
    OR name LIKE '%{$sanitized}%'
    OR file LIKE '%{$sanitized}%'
    OR title LIKE '%{$sanitized}%'
    OR author LIKE '%{$sanitized}%'
    OR description LIKE '%{$sanitized}%'
    OR category LIKE '%{$sanitized}%'
    OR keywords LIKE '%{$sanitized}%'
    OR bio LIKE '%{$sanitized}%'

    But I get the error: Notice: Trying to get property of non-object

    Not all columns are in both tables and if I limit the query to one table, say the files table, and remove the OR statements for columns that are not found in the files table, it works as expected.

    How can I expand upon this to search in multiple tables?

  10. I'm a complete noob when it comes to PHP so I would like a little help here. I'm using your code that you provided, which part of the code to I change to that it pulls information from the database I created on phpMyAdmin?

Leave a Reply