|
An Introduction to PHP and MySQL
Setting up
your own Windows e-commerce site may seem like a daunting task at first.
There is quite a bit to know, though with a basic understanding of the
underpinnings and a few good tools, youll be on your way in no
time at all. I recommend the CGI version of PHP for Windows users. Its stable and you can easily plug it into your Apache Web server. Downloading the source code offers many more customization options, but we wont cover that here because you may run into difficulties with compiling it. Troubleshooting those problems is outside the scope of this article. A basic knowledge of an object-oriented programming language like C++ will make understanding PHP easy. I chose JavaScript for client-side scripting and PHP for the server-side the control structures in JavaScript and PHP are similar to those in C++. We will create a database in MySQL, populate the database, and run a few short queries on the database using PHP. Installing
and Configuring Apache The ServerName does not have to be localhost, and if you happen to have a real host you can supply its name instead. The commented area above ServerName explains your options in detail. Save the file and start Apache from the start menu. After starting Apache, open your Web browser and type "http://localhost/" in the location bar to view the default installation page and verify that it's working. If you used a real ServerName instead of the default localhost, type that name in the location bar instead. Return to
the httpd.conf file and search for a line starting with: Change the
whole group so it resembles the following: <Directory
"C:/website"> Installing
PHP Extract the contents to C:php4 Rename the file called "php.ini-dist" to "php.ini" and move it to C:WINDOWS or wherever the rest of your *.ini files live. Take the two files "Msvcrt.dll" and "php4ts.dll", and put them in C:WINDOWSSYSTEM , or wherever you usually put your *.dll files. Go back to the Apache httpd.conf and make a few modifications to tell Apache what to do with *.php or *.phtml files (and how to do it). With httpd.conf
open in your text editor, search for: Add another
ScriptAlias line to the end, just like this: Now find
a section that looks like this: Change the
section should now look something like this: Search for
the following line: You need
to include an Action line for your new file types, so that they are
automatically sent through the PHP parser. To do this add the following
line: Save your httpd.conf file and start Apache again. There's one
good way to test your installation: use the phpinfo() function. Open
a text editor and type this: Save this file as phpinfo.php, and put it in C:website, then fire up your Web browser and go to "http://localhost/phpinfo.php" where you should see a long page of variables and their values. The phpinfo() function produces this page, which shows you your environment and your settings. This tells you that both Apache and PHP are installed and functioning correctly. Installing
MySQL Connecting
to and creating a database DOS mysqladmin u root create roll This will create a database called roll. PHP If(mysql_create_db
(roll, $link)) else Obviously, youll have to provide the database name and the link to MySql obtained using the mysql_connect() function: $link = mysql_connect (localhost, root); To close the link we will use the mysql_close() function. mysql_close($link); Unless you have logged in using a user account that does not have administrative privileges, this should work without any problems. The dump file used for the database will contain the following statements: create table
student A dump file
is an SQL script file. It contains well-formed SQL statements; it is
preferable to the command line because it is easier to debug and reload
if it contains errors. Dump files should be placed in MySQLs bin
directory. Load that up to the database by typing the following: Now you should have a database thats ready to use. I suggest using a text editor that will preserve line wrapping like Notepad. Lets start with some simple PHP statements. PHP code can be inserted just about anywhere into your HTML code, or replace HTML altogether. First of
all you will need to connect to your database so we will use the following
statement: The statement
was written under the assumption that you are logged in as the root
user. Now that you are connected, you will need to select a database
with the following statement: Retrieving Data From A Database Lets write a simple query: $result = mysql_query(SELECT * FROM customer, $link); The result of the query will be stored in the variable $result. To run a query on the database we can also use the mysql_db_query() function. The first parameter is the database name, second is the query string in SQL format, and the third is the link to the database. mysql_db_query(databaseName, $Query, $link) Again, we can use an if/else control structure to display errors during execution, which can be helpful for debugging. if (mysql_db_query(databaseName,
$Query, $link)) else Below is the content of a PHP file. Save that in your DocumentRoot directory (mine is C:website) and fire up the web browser. You should be able to see a table that displays the content of our database. <html><body> <?php $link = mysql_connect("localhost", "root"); mysql_select_db("roll",$link); $Result = mysql_query("SELECT * FROM student",$link); echo "<table
border=1>"; while ($Row
= mysql_fetch_row($Result)) echo "</table>"; ?> Now you have a basic understanding of how to create, populate, and retrieve information from a MySQL database using PHP. Feel free to modify the PHP file and insert it into or delete it from the database. For more information on other options to try, theres plenty of online documentation available at www.php.net and other sites on the Web.
|
|
_____________________________________________________________________________________________________________ |