Results 1 to 9 of 9

Thread: [RESOLVED] Search Database and display the whole row

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    111

    Resolved [RESOLVED] Search Database and display the whole row

    Hello

    I'd like to know how can i search the database and display the whole row

    i mean like i have a table called family it has fields ( ID "primary key",FirstName,LastName,Age)

    i want to search the FirstName Field for john and display the FirstName and LastName and Age of each search result found in the FirstName Field

    How can i do this in PHP?

    I am sorry but i am a bit noew to mysql and i want to learn more

    Thanks
    Cheers

  2. #2
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Search Database and display the whole row

    well you can use SELECT:

    PHP Code:
    $sql "SELECT * FROM `family` WHERE FirstName='john' LIMIT 1";
    $query mysql_query($sql);
    $result =  mysql_fetch_array($query);

    echo 
    "First Name: " $result['FirstName'] . "<br>";
    echo 
    "Last Name: " $result['LastName'] . "<br>";
    echo 
    "Age: " $result['Age'] . "<br"
    That will select the first row with the name "john" in the database and display the info.

    If you want to search the db, and display all the results you can do this:

    PHP Code:
    $sql "SELECT * FROM `family` WHERE FirstName LIKE '%john%' LIMIT 1";
    $query mysql_query($sql);

    while(
    $result =  mysql_fetch_array($query)) {
         echo 
    "ID: " $result['ID'] . "<br>";
         echo 
    "First Name: " $result['FirstName'] . "<br>";
         echo 
    "Last Name: " $result['LastName'] . "<br>";
         echo 
    "Age: " $result['Age'] . "<br><hr>";

    There is a link in my signature with MySQL/PHP examples as well
    Last edited by dclamp; Mar 23rd, 2008 at 09:56 PM.
    My usual boring signature: Something

  3. #3
    Fanatic Member
    Join Date
    Oct 2004
    Posts
    751

    Re: Search Database and display the whole row

    Actually dclamp, he wants to search for every record with the first name of john so drop off the LIMIT 1.
    Also when you do pattern matching you use LIKE not =
    http://dev.mysql.com/doc/refman/5.0/...-matching.html

    So....
    SELECT * FROM `family` WHERE FirstName LIKE '%john%'
    My Projects: [ Instant Messagener Client/Server ] [ VBPictochat ]

    My Sites:
    [ Datanethost ]
    [ Helpdesk ]

    Remember if my post was helpful then Rate This Post.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    111

    Re: Search Database and display the whole row

    Thanks Guys for your replies

    but i'd like to know what does $result represent i mean does it represent an array for only 1 result? and how can i echo the other results?

    Thanks again
    Adel

  5. #5
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Search Database and display the whole row

    dclcamps post gives examples of both

  6. #6
    Fanatic Member
    Join Date
    Oct 2004
    Posts
    751

    Re: Search Database and display the whole row

    Quote Originally Posted by lordadel
    Thanks Guys for your replies

    but i'd like to know what does $result represent i mean does it represent an array for only 1 result? and how can i echo the other results?

    Thanks again
    Adel
    Yes $result is 1 record, this is why you loop through all of the records using while.
    My Projects: [ Instant Messagener Client/Server ] [ VBPictochat ]

    My Sites:
    [ Datanethost ]
    [ Helpdesk ]

    Remember if my post was helpful then Rate This Post.

  7. #7
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Search Database and display the whole row

    Quote Originally Posted by k1ll3rdr4g0n
    Actually dclamp, he wants to search for every record with the first name of john so drop off the LIMIT 1.
    Also when you do pattern matching you use LIKE not =
    http://dev.mysql.com/doc/refman/5.0/...-matching.html

    So....
    SELECT * FROM `family` WHERE FirstName LIKE '%john%'
    ah i knew that. i just copied and pasted the top code and edited a little for the bottom example

    changed
    My usual boring signature: Something

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Search Database and display the whole row

    dclamp's variable names are misleading. At the least, $query should be $resultset and $result should be $row. However, you should give them more descriptive names than that.

    If you need more advanced pattern matching than the LIKE operator you can also use the REGEXP operator. If you need to search large text fields you can use full-text search.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    111

    Re: Search Database and display the whole row

    Thanks Guys
    rep given

Posting Permissions

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



Click Here to Expand Forum to Full Width