Click to See Complete Forum and Search --> : [RESOLVED] Search Database and display the whole row
lordadel
Mar 23rd, 2008, 12:20 PM
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
dclamp
Mar 23rd, 2008, 01:11 PM
well you can use SELECT:
$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:
$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
k1ll3rdr4g0n
Mar 23rd, 2008, 02:39 PM
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/en/pattern-matching.html
So....
SELECT * FROM `family` WHERE FirstName LIKE '%john%'
lordadel
Mar 23rd, 2008, 02:56 PM
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
lintz
Mar 23rd, 2008, 06:23 PM
dclcamps post gives examples of both :D
k1ll3rdr4g0n
Mar 23rd, 2008, 06:43 PM
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.
dclamp
Mar 23rd, 2008, 09:57 PM
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/en/pattern-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 :)
penagate
Mar 24th, 2008, 12:02 AM
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 (http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html).
lordadel
Mar 24th, 2008, 12:09 AM
Thanks Guys :)
rep given :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.