1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Search engine for my web page

Discussion in 'Programming & Webmastering' started by Aleksander, Jun 14, 2011.

  1. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.81/day)
    Thanks Received:
    304
    Hi guys!
    I created a simple search engine, but i am a beginner so don't know what is the problem with it.
    I created the search, but the problem is, whatever i write on it.... it shows everything.
    I know the LIKE action, but i dunno how to use it, can anyone help on here?
    The code i have written is this:
    Code:
    $result = mysql_query("SELECT * FROM student LIKE '%id%'");
    Also, if i delete the like function everything i type doesn't matter, it shows everything on the database
     
    Last edited: Jun 14, 2011
  2. Jizzler

    Jizzler

    Joined:
    Aug 10, 2007
    Messages:
    3,442 (1.30/day)
    Thanks Received:
    644
    Location:
    Geneva, FL, USA
    Code:
    $result = mysql_query("SELECT * FROM studenti [B]WHERE [I]column[/I][/B] LIKE '%[I]something[/I]%'");
    Could do something like this for a single box name search:
    Code:
    SELECT * FROM studenti WHERE CONCAT(firstname, ' ', surname) LIKE '%[I]partialname[/I]%'
    From your example, seems like you'd like to search every field? This is of course possible. What information do you have in the students table?
     
  3. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.81/day)
    Thanks Received:
    304
    In the student table i have:
    id, name, surname, school, age, city, address, e-mail, password
    What is the meaning of CONCAT?????
     
    Last edited: Jun 14, 2011
  4. Jizzler

    Jizzler

    Joined:
    Aug 10, 2007
    Messages:
    3,442 (1.30/day)
    Thanks Received:
    644
    Location:
    Geneva, FL, USA
    http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat

    Concat is short for concatenation, or gluing strings together.

    In my example I did a LIKE against the full name (name + surname). This is similar to:

    Code:
    SELECT * FROM studenti WHERE name LIKE '%string%' OR surname LIKE '%string%'
    The difference is that the first example could return results from searches like "jill s" or "hobar d" where as the second one would need to be a partial of either the name or surname.

    Searches can be designed dozens of different ways. Could simply concat all the fields you want and search against it:

    Code:
    SELECT * FROM studenti WHERE CONCAT_WS(' ', name, surname, school, CAST(age AS CHAR), city, address, e-mail) LIKE '%string%'
    # I assumed that age may be an integer type
    Lacks the versatility of a Google or Yahoo search, but it's a start. From one input box a user could search for "tirana" or "18" or "joe" or anything else and possibly get results. What they couldn't do is search for all of them at the same time. Not without more coding.
     
    Aleksander says thanks.
  5. theJesus

    theJesus

    Joined:
    Jul 20, 2008
    Messages:
    3,972 (1.73/day)
    Thanks Received:
    863
    Location:
    Ohio
    I'm not a coder (only took a simple "programming logic and problem-solving" class), but wouldn't it be possible to set up some sort of "if" or "case" statement to allow for various types of searches based on input (like if it's in quotes or has boolean operators such as AND, OR, etc.). Or maybe there is a built-in or just simpler way of handling boolean operators in queries? Or another way would be to have it perform different types of searches based on some checkboxes and/or radio buttons. Just some ideas.
     
  6. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.81/day)
    Thanks Received:
    304
    @Jizzler
    Is it possible to search for Aleks and it shows my name?
    Or the person must put in ALL the name to find it?
     
  7. Jizzler

    Jizzler

    Joined:
    Aug 10, 2007
    Messages:
    3,442 (1.30/day)
    Thanks Received:
    644
    Location:
    Geneva, FL, USA
    Yup. Things like city and school could be drop-down boxes to narrow down results. And with a little work to recognize keywords, quotes, etc, the script could build a query that more accurately returns results.

    By coincidence I'm coding up a search page for the company I'm contracting at right now. It's one dirty $%&*$%* database and at this time it can't be redesigned. I'm already up to 250 lines just to build the queries and it's only about half-way done! Also have several mapping files so users can choose one value and the search will include all variations and make use of regex patterns where needed. Much of it could be avoided if the db was well-normalized.


    Yes. Because of the "%", it would also work with "le", "ek", "al", "ale", "lek" and any single letter in your name.

    Depending on how you want your search to operate, you could certainly have multiple fields.
     
    theJesus and Aleksander say thanks.
  8. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.81/day)
    Thanks Received:
    304
    Ok. The big problem i always have is the "PARSE ERROR"
    Parse error: syntax error, unexpected T_STRING in C:\Program Files (x86)\EasyPHP-5.3.6.0\www\regjistrohu.php on line 2
    So this is ALL the code:
    Code:
    <?php
    $connection mysql_connect("localhost","root","");
    if(!$connection)
    	{
    	die('Connection could not be done: ' . mysql_error());
    	}
    
    mysql_select_db("geoinformatic", $connection);
    
    mysql_query($connection);
    $id = $_POST['id'];
    $emri = $_POST['name'];
    $mbiemri = $_POST['surname'];
    $sql="INSERT INTO student (id, name, surname)
    VALUES
    (´$_POST[id]´,´$_POST[name]´,´$_POST[surname]´)";
    
    if (!mysql_query($sql, $connection))
      {
      die('Error: ' . mysql_error());
      }
    echo "New registration added";
    
    $result = mysql_query("SELECT * FROM student");
    
    while($row = mysql_fetch_assoc($result))
      {
      echo $row['id'] . ' ' . $row['name'] . ' ' . $row['surname'];
      }
    
    mysql_close($connection);
    ?> 
    
    Where the HELL is the problem here?
     
    Last edited: Jun 14, 2011
  9. Zyon

    Zyon New Member

    Joined:
    Mar 18, 2011
    Messages:
    264 (0.20/day)
    Thanks Received:
    29
    Location:
    Australia
    Line 2

    $connection = mysql_connect("localhost","root","");

    mysql_connect function should be returning a connection which is then assigned to $connection, just need the = sign.
     
    Jizzler says thanks.
  10. Aleksander

    Joined:
    Dec 2, 2009
    Messages:
    3,254 (1.81/day)
    Thanks Received:
    304
    There was an error with my local server.
    Anyway, i fixed all the errors
     

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page