CATEGORY: MySQL
MySQL Help

QUESTION:
How do I work with a MySQL database using PHP?

ANSWER:
FIRST, if NONE of the following makes any sense to you... you should NOT be using MySQL. It is a VERY ADVANCED feature, and if you are not an experienced programmer, then we suggest that you get one first.

1.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 .phtml. Then within the document itself the section that you'd like to be the PHP code, you begin it with . For instance:



These are the products I sell:



mysql_connect(localhost, username, password);
$result = mysql(mydatabase, "select * from products");

$num = mysql_numrows($result);

$i = 0;


while($i < $num) {

echo "
n";
echo "
n";
echo mysql_result($result,$i,"prodid");

echo "
n ";
echo mysql_result($result,$i,"name");

echo "
n ";
echo mysql_result($result,$i,"price");

echo "
n";
echo "

n";
$i++;}

?>





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 webpage will not see your password.


2.When using a CGI script to pull information from a form which has been submitted by a browser you must have the first line of the script have this command on it (Much like perl scripts):


#!/usr/local/bin/php
############################################################

Please be advised that this is a SUPPLEMENTAL Online Manual.

The PRIMARY Online Manual is inside your Online Control Panel... to access it, just click Documentation link at very bottom of Online Control Panel screen.

You should always consult the Online Manual inside your control panel first... and only consult this SUPPLEMENTAL Online Manual as a last resort.

if after consulting the Control Panel Documentation, you still need assistance, please CONTACT SUPPORT

top