[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
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
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%'
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
Re: Search Database and display the whole row
dclcamps post gives examples of both :D
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.
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 :)
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.
Re: Search Database and display the whole row
Thanks Guys :)
rep given :)