admin Site Admin
Joined: 23 Jan 2003 Posts: 256 Location: India
|
Posted: Wed Feb 19, 2003 7:38 pm Post subject: Connecting to MySQL with PHP |
|
|
How do I work with a MySQL database using PHP?
To merely display the information in your database without the use of a form to call a php script you simply create your HTML document as you would any other web page but instead of the extension of .htm or .html you need to name the file with the extension .php. Then within the document itself the section that you'd like to be the PHP code, you begin it with <?php and end it with ?>. For instance:
<P>These are the products I sell:</P>
<TABLE BORDER="1">
<?php
mysql_connect(localhost, username, password);
$result = mysql(mydatabase, "select * from products");
$num = mysql_numrows($result);
$i = 0;
while($i < $num) {
echo "<TR>n";
echo "<TD>n";
echo mysql_result($result,$i,"prodid");
echo "</TD>n<TD>";
echo mysql_result($result,$i,"name");
echo "</TD>n<TD>";
echo mysql_result($result,$i,"price");
echo "</TD>n";
echo "</TR>n";
$i++;}
?>
</TABLE>
Thus having the loop in the php program create a table with the products listed. NOTE your username and password for the database are not written in the file when it's displayed on the Internet so users viewing the source of your web page will not see your password. |
|