I am new in PHP. this is the first time i do programming. i would like to built a search engine to search the database in mysql. Can anybody give me the example of this code? Thanks you.

    Depending upon how you set up your database, it isn't really the PHP code that searches the database. It's the database engine 😉

    Basically, you'd have a basic form with an input box for search terms. These are passed to a PHP script that constructs a query that looks like:

    SELECT * FROM `tablename` WHERE `field` LIKE '%$userinput%'

    Now, that's very basic searching. You'd have to have fulltext indexing on the columns you want searchable. You also might want to "rank" some of the results. This is a more in-depth item, and I'd suggest you look into it after you get the basics down.

      ok. here my search engine will be an input box for search term, one select field and i would like to build 2 submit button. the submit button is given the option to user so can minimize the searching area. How could i make it? I have only one table to search here.Can u help me?

        Well, we here will help you as much as you can, but in part of helping you, we need you to be as clear as possible.

        2 submit button. the submit button is given the option to user so can minimize the searching area.

        I'm not sure what you mean by this. Do you mean like searching specific columns inside of a database table? That would best be done with a drop-down (<select>) element listing what columns to search.
        Or are you talking about "minimizing" the area the search takes up and let the user know when it's done searching?

        Sorry I don't understand you, but it was difficult to figure what you meant from what you wrote.

          i am sorry to make u lost. Actually what i mean here is i have one text field and 2 drop-down select element listing what column to search. So can u help me?

            Sure. But it's best if you start to put things together on your own first, and when you run into trouble, ask for help.

            So start with your basic form and listing what columns to search in your drop-downs. Then move on to the PHP page.

            In the PHP page, you'd want to grab the search term(s) and areas to search in. From there, you'd construct your database query (see my first post) and execute that query. Take the results, and display them as you please on your site.

            Start on your own, see what you come up with. From that, when you run into issues you can't figure out, or if you have questions, ask here.

              thank your reply. Actually i had try to code by myself .the below is my code.When i run it, itt wat the error that Undefined index: search .
              How do i defined the search? Can u give me a help?

              <form method="post" action="search.php">
              <input type="text" name="search" size=25 maxlength=25>
              <input type="Submit" name="Submit" value="Submit">
              </form>
              <?
              
              //connect to server 
              mysql_pconnect("localhost","root","password"); 
              //select the db 
              mysql_select_db("inventory"); 
              //get the results and store them in $result 
              $search=$_POST["search"];
              
              $result = mysql_query ("SELECT * FROM listofitem WHERE NamaPeralatan LIKE '%$search%'"); 
              //run the while loop 
              while($r=mysql_fetch_array($result)) 
              { 
              //grab the result 
              $NamaPeralatan=$r["NamaPeralatan"]; 
              //print it out 
              echo $NamaPeralatan; 
              echo "<br>"; 
              } 
              
              ?>

              [Mod Edit]Added PHP tags for readability

                Not really sure. If it's in the form, the name attributes are the keys in the $_poSt array. Not sure why it's doing that. Have you tried leaving the action of the form blank?

                  Write a Reply...