Results 1 to 7 of 7

Thread: Easy SQL query

  1. #1

    Easy SQL query

    Hi - hopefully this is an easy query for any experts out there!

    I have a text entry on a form where someone enters a surname they want to search for and when they press the submit button the page should display the results. The page is linked to a table in a phpmyadmin database with following headings - MemberID, Title, FName, LName. The action element of the form is to go to a php page with the following code:

    <?php
    include "sqlsearchmem.inc";
    echo "<table border=1>";
    for($i=0;$i<1;$i++){
    echo "<tr>";
    $row=mysql_fetch_row($result);
    for($j=0;$j<1;$j++){
    echo"<td>";
    echo"$row[$j]";
    echo"</td>";
    }
    echo"</tr>";
    }
    echo"</table>";
    ?>

    The include file has the SQL which I am struggling with. I have played around with a few variations - do I need to use $_POST somewhere? I need it to search the database for whatever surname is entered in the form and then return the results - can anyone help? Thanks

  2. #2
    What exactly is in the sqlsearchmem.inc file?

    I'm not sure what you're having trouble with. I've also noticed that you're 'for' loops aren't going to reitterate because they stop when $i and $j equal 1. Have a look at this code, maybe it'll help you.

    PHP Code:

    // Connect to MySQL Server and select databse.

    // Grab the input from the HTML form
    $lastname $_POST["lastname"];

    $query "SELECT * FROM people"// People would be your table containing all this information. 

    $result mysql_query($query); // Query the database

    $num_rows mysql_num_rows($result); 

    $i 0;
    while(
    $i $num_Rows)
    {
      if(
    mysql_result($result,$i,"LName") == $lastname)
      {
         echo 
    "First Name: ";
         echo 
    mysql_result($result,$i,"FName");
         echo 
    "<br />Last Name: $lastname<br />Title: ";
         echo 
    mysql_result($result,$i,"Title");
         echo 
    "<br />Member ID: ";
         echo 
    mysql_result($result,$i,"MemberID");
         echo 
    "<br /><br />";

        }
        
    $i++;

    [COLOR=dark-blue]Matt Carpenter[/COLOR]
    Offer your clients a brandable FTP upload utility

  3. #3
    Thanks Matt

    This works fine

    Jim

  4. #4
    Is it necessary to always use my-sql database with PHP? please suggest your reply will be appreciated.

  5. #5
    Quote Originally Posted by nancysmith View Post
    Is it necessary to always use my-sql database with PHP? please suggest your reply will be appreciated.
    i'm not exactly sure, but it isn't necessary always, cause i edited a lot of php files without database in it..

    hope it helps

  6. #6
    i will certainly try this code

  7. #7
    Thanks for the sql stuff, I was not too good in sql but I'm learning it in forums mainly.

  8. Newsletters

    Subscribe Now & Get The WHT Quick Start Guide!

Related Posts from theWHIR.com

Posting Permissions

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