Thursday, January 13, 2011

PHP MYSQL - Tutorial 1 : Install PHP, MYSQL , connect and view the contents of a table

Most of you already experienced by now, its not that easy to install apache, php and mysql
and get started with PHP quickly. you may face more problems especially on windows.
For this, there is wonderful free tool called XAMPP.

XAMPP is an easy to install Apache distribution containing MySQL, PHP and Perl
THis is available for Win,Linux, Solaris, Mac OS at : http://www.apachefriends.org/en/xampp.html

When I used Xampp, the only issue I faced was "apache was not started" , but it was due to a the port 80
was already in by other service in my system.In that case, change the port in httpd.conf
or stop the service( if you do not need it now like tomcat etc.,)

Okay, let me assume that PHP is installed.
This tutorial series is all about connecting to MYSQL Database , and performing varios database operations

First step obviously is to create a table.
You can install SQLYOG( excellent MySQL client) to do the qureying on the MYSQL Database Server installed by Xampp.
or since this is a basic tutorial , you can even type the commands in mysql command line.

Lets get into code now :



Step 1 : Let me create a database and table

create database eshop;
create table cust (cid integer(3), UserName varchar(30), LastName varchar(50), Details varchar(100));

Step 2: Add some data to the table.

 insert into cust values (1,'Sachin','Ramesh','cricketer in Mumbai')

 Step 3 : Getting PHP and MYSQL together.

We will structure the site in this order.

1. index.php viewing the table contents and as the front end;
2. addcust.php to add entries to the table;
3. updatecust.php to edit the table content;
4. delcust.php to delete entries from the table;


step 3a. : index.php ( view the contents of the table)

<html> 
<head><title>My Customers </title> 
</head> 
<body bgcolor=#ffffff> 
<h2>Data from customers table </h2> 


<? 
mysql_connect (localhost, username, password) or die ("Problem connecting to DataBase"); 
$query = "select * from cust"; 
$result = mysql_db_query("eshop", $query); 


if ($result) { 
echo "Found these customers in the database:<br><p></p>"; 
echo "<table width=90% align=center border=1><tr> 
<td align=center bgcolor=#00FFFF>ID</td> 
<td align=center bgcolor=#00FFFF>User Name</td> 
<td align=center bgcolor=#00FFFF>Last Name</td> 
<td align=center bgcolor=#00FFFF>Details</td> 


</tr>"; 


while ($r = mysql_fetch_array($result)) { 


$cid = $r["cid"]; 
$user = $r["UserName"]; 
$last = $r["LastName"]; 
$det = $r["Details"]; 


echo "<tr> 
<td>$cid</td> 
<td>$user</td> 
<td>$last</td> 
<td>$det</td> 
</tr>"; 

echo "</table>"; 


} else { 
echo "No data."; 



mysql_free_result($result); 
include ('sitelinks.txt'); 
?> 


</body> 
</html>



lets begin with html tags to create the html tructure.
When we want to escape from html and move to PHP we open a PHP section with
<? This tells the webserver to treat the following text as PHP syntax and not plain html.
To close the PHP section we used ?> tag.

The mysql_connect() command tells PHP to establish a connection to the MySQL server.
If the connection is established successfully the script will continue,
if not it will print the die message "Problem connecting to DataBase"


We placed the query we want to perform on the MySQL in $query, and then the script executes
it by using mysql_db_query command:

$result = mysql_db_query("eshop", $query);

while the "eshop" varible is the name of the database and $query is the needed MySQL query.
We used the MySQL command select (as described above) to get all the data from the table:
Few words about $result the function,
the function returns a positive MySQL result identifier to the query result, or false on error.
This is not the result itself but just an identifier that can be later translated to the information we need.

$query = "select * from cust";

Notice that we printed some html tags to build the html table structure using the echo command.
Only printed text from PHP command will be inserted as html - not the PHP commands themselves.
Another command we used is the while instruction, which uses the following syntax:
The if and while loops are more self-explanatory
we can print it inside the html table with:

echo "<tr>
<td>$idx</td>
<td>$user</td>
<td>$last</td>
<td>$text</td>
</tr>";

Now we can free the MySQL connection and free some resources by using the mysql_free_result($result); function.
Another useful feature of PHP is the ability to include text files inside the script.
Let's assume we have a repeating set of code (e.x. set of links to other pages),
we can use the include function and save us some coding and time.
Furthermore, if we want to change the code all we need to do is change
the included file and it will affect all the pages we included the file.

Here we created a text file called sitelinks.txt that holds the entire link menu we want to use on every page:
<p></p>
<ul>
<li><a href="index.php">Home</a>
<li><a href="addcust.php">Add a new entry to the DataBase</a>
<li><a href="updatecust.php">Edit an entry</a>
<li><a href="delcust.php">Delete an entry from the DataBase</a>
</ul>

The include syntax is: Include ('included_text_file');
Now we can close the PHP section with ?> and the html page with </body></html>.


Hope you like this. In the next tutorial we will add, update and delete records .

No comments:

Post a Comment

subversion video