How to search database using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ehpratah
    New Member
    • Mar 2012
    • 35

    How to search database using php

    hi i have a problem i need to search my database for the registration number..the problem is i have 2 tables in that database such as cd and sd.. how can i search it?

    i already have an idea its using textbox and combobox and a submit button but i dont how to implement it..please help me guyz thanks

    here is the form

    Code:
     <form method="post" action="searcherresults.php">
     
      <select name="table" size="1">
     <option value="cd">cd</option>
     <option value="sd">sd</option>
     </select>
     <input type="text" name="search" size=25 maxlength=25>
     <input type="Submit" name="Submit" value="Submit">
     </form>

    and this searchresult.ph p

    Code:
    <?php
    
     $search=$_POST['search'];
     $table=$_POST['table'];
    
     
    
    
    
    									$dbhost = 'localhost';
    									$dbuser = 'root';
    									$dbpass = '';
    
    									$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
    
    									$dbname = 'msic';
    									mysql_select_db($dbname);
     $sql = mysql_query ("SELECT * FROM '%{$table}%' WHERE Rnum LIKE '%{$search}%'");
    while ($row = mysql_fetch_array($sql)){
        echo 'ID: '.$row['Idko'];
        echo '<br/> Registration Number: '.$row['Rnum'];
        echo '<br/> Name: '.$row['Fname'];
    	echo ' '.$row['Lname'];
    	echo '<br/>Contact Number: '.$row['Contact'];
    	echo '<br/>Rate: '.$row['Rate'];
    	echo '<br/> Pick Up location: '.$row['Origin'];
    	echo '<br/>Pick Up Date: '.$row['Pickday'] . $row['Pickmonth'] ;
        echo '<br/>Pick time: '.$row['Pickhr'];
    	echo ' '.$row['Pickmin'];
    	echo '<br/>Return Date: '.$row['Rday'] . $row['Rmonth'] ;
    	echo '<br/>Return time: '.$row['Rhr'] ;
    	echo ' '.$row['Rmin'];
    	echo '<br/> Destination: '.$row['Destination'];
    	echo '<br/> Vehicle Type: '.$row['Vehicle'];
        echo '<br/><br/>';
        }
     
     ?>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    as a matter of fact, you search in a database by using the database (resp. its language SQL). PHP is only there to transform the user input into SQL and the returned DB data into (readable) HTML.

    Comment

    • ehpratah
      New Member
      • Mar 2012
      • 35

      #3
      have any idea how can i search my database with a use of dropdown menu, combo box?i'm really stuck in here dude..

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        searching a DB from scratch is a lot of code, you know? and depending on how each part looks (you have at least HTML, CSS, PHP, SQL (not to forget the DB layout) and possibly JavaScript), it can become quite a handful.

        Comment

        • ehpratah
          New Member
          • Mar 2012
          • 35

          #5
          i've already made a bit of coding and im having an error
          Code:
          Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\msicrevised\searcherresults.php on line 20

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            that means that your query failed due to an SQL syntax error.

            Comment

            • ehpratah
              New Member
              • Mar 2012
              • 35

              #7
              can you point it out where i made the mistake..and can you give me a an alternative solution.thanks

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                the error is made on line 20. detailed info you can get from mysql_error().

                Comment

                • ehpratah
                  New Member
                  • Mar 2012
                  • 35

                  #9
                  i already review my code i cant kind point out where i made the mistake...can you help me debug it.. thanks help is much appreciated

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    can you help me debug it
                    as I said, use mysql_error()

                    Comment

                    • ehpratah
                      New Member
                      • Mar 2012
                      • 35

                      #11
                      the error that i encounter when i put mysql_error() is

                      Code:
                      Parse error: syntax error, unexpected T_LOGICAL_OR in C:\xampp\htdocs\msicrevised\searcherresults.php on line 19

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        a little bit more code would be incredibly helpful ...

                        Comment

                        • ehpratah
                          New Member
                          • Mar 2012
                          • 35

                          #13
                          actually i really dont have any idea what to do next whit that error that i am encountering:(

                          Comment

                          • ehpratah
                            New Member
                            • Mar 2012
                            • 35

                            #14
                            is their a problem with this part?

                            Code:
                             $sql = mysql_query ("SELECT * FROM '%{$table}%' WHERE Rnum LIKE '%{$search}%'");

                            Comment

                            • Dormilich
                              Recognized Expert Expert
                              • Aug 2008
                              • 8694

                              #15
                              that’s correct.

                              edit: table names must not be string quoted and can’t contain wildcard characters.

                              Comment

                              Working...