Date: |
July
15, 2001 |
Subject: |
Online
Programmer Ezine, Volume 2, No.6 |
Article: |
Jump into PHP and MySQL web programming
- Part I |
In Unix web programming world, PHP and MySQL play a big rule.
In fact most of web programming projects use either Perl or
PHP.
As for web database applications, MySQL is used for most of
small and medium sized projects. Most Unix hosting services
provide MySQL service as a part of their hosting plans. (Our
computeruni.com sponsor hostab.com provides such services with
very reasonable prices).
In this article we first start working with MySQL then we
will work on PHP database programming.
**MySQL**
In this section we will assume that you have a Unix type
server available (any type with MySQL installed on it).
MySQL has a console text mode client program. To run the
client type ‘mysql’ at command prompt.
$ mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 3.23.38
Type 'help;' or '\h' for help. Type '\c' to clear the buffer
mysql>
Now we will try to create a database in our sql server.
mysql> create database teldb;
Query OK, 1 row affected (0.03 sec)
mysql>
Now that our database is created we can create our telephone
number table in this database. Before we can do this we must
change active database (current database) to “teldb”. “\u”
command changes active database.
mysql> \u teldb
Database changed
mysql> create table teltbl
-> (no varchar(10),
-> firstname varchar(20),
-> lastname varchar(20),
-> location varchar(20),
-> comment varchar(50))
-> ;
Query OK, 0 rows affected (0.02 sec)
mysql>
In fact we have executed the following SQL query.
create table teltbl
(no varchar(10),
firstname varchar(20),
lastname varchar(20),
location varchar(20),
comment varchar(50));
If you are not familiar with SQL you need to learn it to be
able to work with SQL servers of any type. We will have some
tutorial articles on SQL query language in next issues of
our e-zine.
After above step our table is ready for data entry. We will
use another SQL query to insert information into our table.
mysql> insert into teltbl
-> values('2204646','siamak','sarmady','new york','author')
-> ;
Query OK, 1 row affected (0.00 sec)
mysql>
You can see entered data in our table with below query.
mysql> select * from teltbl;
+---------+-----------+----------+----------+---------+
| no | firstname | lastname | location | comment |
+---------+-----------+----------+----------+---------+
| 2204646 | siamak | sarmady | new york | author |
+---------+-----------+----------+----------+---------+
1 row in set (0.00 sec)
mysql>
Now we quit MySQL and go to next step.
mysql> quit
Bye
In this step we have a ready database and table and we can
start programming our web application.
Our script will be written in a file with .php extension
and will be placed in html root of server.
<body>
<?php
$db = mysql_connect("127.0.0.1", "root");
mysql_select_db("teldb",$db);
$result = mysql_query("SELECT * FROM teltbl",$db);
printf("Phone No: %s<br>\n", mysql_result($result,0,"no"));
printf("First Name: %s<br>\n", mysql_result($result,0,
"firstname"));
printf("Last Name: %s<br>\n", mysql_result($result,0,
"lastname"));
printf("Address: %s<br>\n", mysql_result($result,0,
"location"));
printf("Comment: %s<br>\n", mysql_result($result,0,
"comment"));
?>
</body>
</html>
Now if you try to browse above .php file you will be able to
see a table of entries on your browser.
In next issue of ezine we will write more scripts for adding
records, searching, deleting etc.
If you have any questions about PHP, you visit our PHP
programming forum and ask your question. Both I and other
users will reply to your questions.
http://op.htmsoft.com/forum
|
| |