|
-
Mar 23rd, 2008, 12:20 PM
#1
Thread Starter
Lively Member
[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
-
Mar 23rd, 2008, 01:11 PM
#2
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
-
Mar 23rd, 2008, 02:39 PM
#3
Fanatic Member
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%'
-
Mar 23rd, 2008, 02:56 PM
#4
Thread Starter
Lively Member
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
-
Mar 23rd, 2008, 06:23 PM
#5
Re: Search Database and display the whole row
dclcamps post gives examples of both
-
Mar 23rd, 2008, 06:43 PM
#6
Fanatic Member
Re: Search Database and display the whole row
 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.
-
Mar 23rd, 2008, 09:57 PM
#7
Re: Search Database and display the whole row
 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
-
Mar 24th, 2008, 12:02 AM
#8
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.
-
Mar 24th, 2008, 12:09 AM
#9
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|