Search for notes by fellow students, in your own course and all over the country.
Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.
Title: BEGINNER FOR mysql and php
Description: easy to lean mysql and php tutorial
Description: easy to lean mysql and php tutorial
Document Preview
Extracts from the notes are below, to see the PDF you'll receive please use the links above
Create dynamic sites with PHP &
MySQL
Presented by developerWorks, your source for great tutorials
ibm
...
1
...
Introduction and installation
3
3
...
Add new records
10
5
...
Delete, edit, and search data
14
7
...
com/developerWorks
Section 1
...
When we are finished, you will know how dynamic sites
work and how they serve the content, and you will be ready to serve your own dynamic
content from your site
...
Ashraful
Anam, at russell@bangla
...
Md
...
Having conquered the
Windows platform, he recently changed his interest to Linux and immediately fell in love with
it
...
He can be reached at russell@bangla
...
Create dynamic sites with PHP & MySQL
Page 2
Presented by developerWorks, your source for great tutorials
ibm
...
Introduction and installation
The need for dynamic content
The Web is no longer static; it's dynamic
...
Think of an e-shop that has 1,000
products
...
Ouch!!!
Wouldn't it be easier to have only one page that created and served the content on the
fly from the information about the products stored in a database, depending on the
client request?
Nowadays sites have to change constantly and provide up-to-date news, information,
stock prices, and customized pages
...
PHP PHP is a robust, server-side, open source scripting language that is extremely
flexible and actually fun to learn
...
MySQL SQL is the standard query language for interacting with databases
...
MySQL is also cross platform
...
com/developerWorks
Installing Apache server routines
First we will install the Apache server routines in the Linux environment
...
If someone else is hosting
your site, ask the administrator to install them for you
...
First download the Apache archive,
apache_x
...
xx
...
gz (the latest I downloaded was apache_1
...
14
...
gz) from the
Apache site and save it in /tmp/src directory
...
x
...
tar
...
Change to the directory that has been
created:
# cd apache_x
...
xx
Now to configure and install apache, type the commands:
#
...
If you want to install Apache
to a different directory, replace /usr/local/apache with your directory in the prefix
...
You might want to change the default server name to something of real value
...
conf file (located at /usr/local/apache/conf) and find the line starting
with ServerName
...
To test your install, start up your Apache HTTP server by running:
# /usr/local/apache/bin/apachectl start
You should see a message like "httpd started"
...
You should see a nice welcome page
...
com/developerWorks
Installing MySQL
Next comes MySQL
...
Download the source from the MySQL site and save it in /tmp/src
...
22
...
tar
...
#
#
#
#
#
#
cd /tmp/src/
gunzip -dc mysql-x
...
xx
...
gz | tar xv
cd mysql-x
...
xx
...
Now you need to create the grant tables:
# scripts/mysql_install_db
Then start the MySQL server:
# /usr/local/bin/safe_mysqld &
And test your installation by typing:
mysql -uroot -p
At the password prompt, just press Enter
...
Commands end with ; or \g
...
22
...
mysql>
If you see this, you have MySQL running properly
...
Type status to see the MySQL server status
...
Installing PHP
We will follow a similar procedure to install PHP
...
x
...
tar
...
x
...
/configure --with-mysql=/usr/local/mysql --with-apxs=/usr/local/apache/bin/apxs
make
make install
Copy the ini file to the proper directory:
# cp php
...
ini
Open httpd
...
x, use:
#
#AddType application/x-httpd-php
...
com/developerWorks
#AddType application/x-httpd-php-source
...
x, use:
#
AddType application/x-httpd-php
...
phtml
AddType application/x-httpd-php-source
...
Type the following code in a text editor
and save it as test
...
php,
and then view it with your browser
...
If you don't, then PHP was not installed
properly
...
Make sure there is a section "MySQL" in the php info; if not,
MySQL connectivity will not work
...
com/developerWorks
Section 3
...
Fire up your text
editor and type the following code:
echo "Hello World";
?>
Save the file as first
...
The page shows "Hello World"
...
You will only see the text Hello World
...
Notice the
...
tells
PHP to stop processing
...
Your first database
Now that we have PHP running properly and have created our first script, let's create our first
database and see what we can do with it
...
Here we have assumed that you are
root user
...
If you are hosting your site
through a hosting company, you probably don't have permission to run mysqladmin
...
Next we will create tables in this database and enter some information
...
Type:
mysql
You should see something like:
Welcome to MySQL monitor
...
Your MySQL connection id is 5 to server version 3
...
34
Type 'help' for help
...
com/developerWorks
nick varchar(12),
email varchar(35),
salary int,
PRIMARY KEY (id),
UNIQUE id (id)
);
INSERT INTO personnel VALUES ('1','John','Lever','John', 'john@everywhere
...
com','66000');
This creates a table with 5 fields and puts some information in it
...
Save the following text as viewdb
...
But what is this code
doing and how is it generated? Let's examine the code
...
In
Create dynamic sites with PHP & MySQL
Page 8
Presented by developerWorks, your source for great tutorials
ibm
...
The string after $ is the name of
that variable
...
So we declare the variable
$db and create a connection to the mysql database with the statement
"mysql_connect("localhost", "root", "")"
...
Replace them with
your own username and password if they are different
...
Next we select the database with which we want to interact with the lines
"mysql_select_db("learndb",$db);" which means we wish to use the database "learndb"
located by the pointer variable $db
...
We run this query with the PHP command mysql_query() and save the result returned by the
database to the variable $result
...
We use the function mysql_fetch_array() to extract
each row from $result and assign them to variable $myrow
...
Then we output the data contained in each row
...
We have used the while() loop here, which means as long as or while there are data to be
extracted from $result, execute the lines within those brackets {}
...
Viewing the PHP code and the HTML source from the
browser side-by-side may help you easily understand the procedure
...
Create dynamic sites with PHP & MySQL
Page 9
Presented by developerWorks, your source for great tutorials
ibm
...
Add new records
Creating an HTML form
So now you can view records stored in your MySQL database and display them in your
browser using PHP
...
Assuming that you know about
HTML forms, let's code a page that will do just that
...
html:
}
?>
This creates a script that shows the form when there is no input or otherwise enters the
information into the database
...
So it will create the variable $submit if the form is posted
...
If it exists and contains a value then we have
to enter the posted values into the database; otherwise, we have to show the form
...
Try inserting some new
data into the database and check to see if it really works by viewing them with
viewdb
...
Create dynamic sites with PHP & MySQL
Page 11
Presented by developerWorks, your source for great tutorials
ibm
...
Get a better view
Passing variables
Let's take a different view now and consider how information can be passed to another
PHP page
...
What are query strings? Change the line method="post" to
method="get" in our script input
...
Now try submitting new data into the database
with it
...
But look at the URL
...
php?first=Rick&last=Denver&nickname=Mike&email=j@xyz
...
Now the information is passed as a string in the URL instead of posting
directly
...
When PHP receives a query string like ?first=John
it automatically creates a variable named $first and assigns the value from the query
string to it
...
Viewing individual rows
So now we will create a script that will display the information of a particular row in our
database defined by the variable $id
...
php
...
php?id=2 (here we have passed the variable
$id=2 through the query string)
...
$db = mysql_connect("localhost", "root", "");
mysql_select_db("learndb",$db);
$result = mysql_query("SELECT * FROM personnel WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
echo "First Name: "
...
$myrow["lastname"];
echo "
Nick Name: "
...
$myrow["email"];
echo "
Salary: "
...
But can't multiple rows contain the same values of id? Generally a column can
contain any value, the same or different
...
We immediately modify our previous viewdb
...
php so that it can call view
...
$db = mysql_connect("localhost", "root", "");
mysql_select_db("learndb",$db);
$result = mysql_query("SELECT * FROM personnel",$db);
echo "
" ... php?id=" ... "\">View"; } echo " |
?>
Viewing this page will show a list of names and corresponding nicknames
...
Take your mouse over the hyperlink and look what it points to
...
php?id=3 and the links in each row will
be different
...
It will bring up our previously coded view
...
How is this achieved?
Let's take a look at our code viewdb2
...
Look at line 11, where all the real stuff takes
place
...
) all around the line
...
) is a concatenating operator in PHP, which means it concatenates the two strings on its
two sides, which in turn means that if we write echo "Hello"
...
In our example we use the concatenate operator to generate a line like:
mysql_query("DELETE FROM personnel WHERE id=$id",$db);
echo "Information Deleted";
?>
Once again we modify our previous viewdb2
...
php to add this new
feature
...
$db = mysql_connect("localhost", "root", "");
mysql_select_db("learndb",$db);
$result = mysql_query("SELECT * FROM personnel",$db);
echo "