PHP MySQL |
Here we give an example of the PHP code that will read data out of a MySQL database and present it in a nice tabular format in HTML.
Assuming we have the following MySQL table:
Table Employee
Name | Salary |
Lisa | 40000 |
Alice | 45000 |
Janine | 60000 |
The PHP code needed is as follows (assuming the MySQL Server sits in localhost and has a userid = 'cat' and a password of 'dog', the database name is 'myinfo') :
$link = mysqli_connect("localhost","cat","dog","myinfo") or exit(); print "<p>Employee Information";
$result = mysqli_query($link,"select name, salary from Employee"); while ($row=mysqli_fetch_array($result))
print "</table>"; |
Output is
Employee Information
|
Below is a quick explanation of the code:
$link = mysqli_pconnect("localhost","cat","dog","myinfo") or exit(); |
This line tells PHP how to connect to the MySQL server.
$result = mysqli_query($link,"select name, salary from Employee"); |
This specifies the query to be executed.
while ($row=mysqli_fetch_array($result))
|
$row[0] denotes the first column of the query result, namely the "name" field, and $row[1] denotes the second column of the query result, namely the "salary" field. The . in the print statement is the concatenation operator, and acts to combine the string before and string after together. The print statement continuess until all 3 rows have been fetched.