Sunday, September 15, 2013

Taking data from MySQL

Retrieving data here related to the use of the SELECT query. Here is an example for
displays the first record of the table example.

<? php
mysql_connect ("localhost", "admin", "1admin") or die (mysql_error ());
mysql_select_db ("test") or die (mysql_error ());
$ result = mysql_query ("SELECT * FROM example")
or die (mysql_error ());
/ / Save the record to the variable $ data
$ record = mysql_fetch_array ($ result);
/ / Display the data from the $ record for each field
echo "name:". $ record ['name']. "
";
echo "He:". $ record ['age'];
?>
 
Output script above is:
His name: sapient
Age: 20
The above command will only display the first record of the table example. then,
how to display more than one record?
To display more than one record, we use looping. Consider the following example
Here you are.
 
<? php
mysql_connect ("localhost", "admin", "1admin") or die (mysql_error ());
mysql_select_db ("test") or die (mysql_error ());
$ result = mysql_query ("SELECT * FROM example")
or die (mysql_error ());
while ($ record = mysql_fetch_array ($ result))
{
echo "name:". $ record ['name']. "
";
echo "He:". $ record ['age']. "
";
}
?>

Looping while above will continue to run as long as the record is still there to be read. Results from script
above is
His name: sapient
Age: 20
His name: Surti
Age: 30
 
Note:
To make it easier for you in the administration and manage the MySQL database,
highly recommended to install phpMyAdmin which can be downloaded via the official website at http://phpmyadmin.sourceforge.net/
news sources from :Rosihan Ari Yuana

No comments:

Post a Comment