Вы находитесь на странице: 1из 2

HOW TO LINK PHP AND DATABASE

1) Open Notepad application then create new file and name it with extension .php. For
example try.php. Place this file in the folder located in Wnmp\html.
2) Type this code in your file.
<?php
echo("Hello World!\n");
?>
Test your code by type in your browser "localhost/try.php". Your
webpage should look like this.

You have done a webpage.


3) Now create a database name addressbook using your MariaDB command prompt. Then
make one table call colleague with attribute and tuples below
ID
1
2
3

FIRSTNAME
ABU
LEE
MUTHU

LASTNAME
ALI
CHONG
SAMY

TELEPHONE
013-1234567
017-1234567
012-1234567

EMAIL
ali@gmail.com
chong@gmail.com
samy@gmail.com

4) Then replace code in your php file with code below:


<?php
$con=mysqli_connect("localhost","root","password","addressbook");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM colleague");

?>
<html>
<head>
<title> address book </title>
</head>
<body>
<h1>address book</h1>
<table border="1" cellpadding="2" cellspacing="3">
<tr>
<th>ID</th>
<th> FIRST TIME</th>
<th> LAST NAME </th>
<th> TELEPHONE </th>
<th> EMAIL </th>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['telephone'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<\tr>";
}
//mysql_free_result($result);
mysqli_close($con);
?>
</table>
</body>
</html>
Test your code.

Вам также может понравиться