I’m writing this article for those who are not familier with database connection using PHP or other language. One of my best friend Akash has told me to help him with this topic. Its a special matter. Because he is a student of Mechanical Engineering.
Though he is trying to know about the basic of HTML, CSS using Macromedia Dreamwever MX but his cursity about PHP made me surprised.
First you should know the neccessity of database. In present websites there are many kind of use of database. Such as User Login, Comment Box, Voting system, Registration form etc. When a Website is not just for browsing but also for connectivity between user and administrator of that site, then it need database.
For example: One user want to be a member of a site with his information and he will get his data back requesting them again. For this he need to be registered.
First his registration form is inserted into database with is ID. When he requests or get Login , then he can go to his page or some secured page by the verification of his information stored in database. His user ID and Password also stored in database and when he insert his ID & PASS in a login form then neccessary PHP code send this data to database and some code compare that data with stored data. When it is true then some other functions will be executed.
Requriments:
***** I want to imagine that Akash has enough idea About
1.MySQL Query
2.Functions and Logics that he used in C language previous semester
3.Executing codes
4. Database (MySQL or Oracle)
5. Web Server ( Apache or anythig)
6. PHP
For testing your application in your PC. You need Software where you can test that application using localhost. That means your PC acts as both Server and Client.
Software:
WAMP (Windows-Apache-MySQL-PHP)
LAMP (Linux-Apache-MySQL-PHP)
XAMPP
For WAMP or LAMP you should put your files in the www directory and for XAMPP in the htdocs directory. Open your browser and type the address just like:
First Example:
Open your browser and type the address http://localhost/phpmyadmin
This is the Graphical interface of administrating MySQL Database. Then create a database named nuhil and create a table which has the column that were used in the INSERT INTO query below on this example.
This example will show you how PHP helps to connect MySQL database. Just Begin…
For the page to insert data. Save this file as dbtest.php
<?php
if (isset($_POST['firstname']) && isset($_POST['lastname']) && isset($_POST['username'])) // ‘firstname’ is the name of the field of the form where
{ //user puts is firstname. Then a if condition for
// checking if that field is filled up
$ret = add_to_database(); // function to insert data to database
if (!$ret)
{
print “Error”;
}
else
{
print “Thank you!”;
}
}
else {
write_form(); // Prints the form using HTML code
}
function write_form() {
//$self=$_SERVER['PHP_SELF'];
echo<<<EOT
<form name=”form1″ method=”POST” action=”$_SERVER[PHP_SELF]“> // this data will be submitted in this page using $_SERVER[PHP_SELF]
<table width=”90%” border=”0″ align=”center” cellspacing=”1″>
<tr bgcolor=”#666666“>
<td>First Name </td>
<td><input type=”text” name=”firstname”></td>
</tr>
<tr bgcolor=”#666666″>
<td>Last Name </td>
<td><input type=”text” name=”lastname”></td>
</tr>
<tr bgcolor=”#666666″>
<td>User Name </td>
<td><input type=”text” name=”username”></td>
</tr>
<tr bgcolor=”#666666″>
<td>Email</td>
<td><input type=”text” name=”useremail”></td>
</tr>
<tr bgcolor=”#666666″>
<td>Web URL </td>
<td><input type=”text” name=”userweb”></td>
</tr>
<tr bgcolor=”#666666″>
<td>Country</td>
<td><input type=”text” name=”country”></td>
</tr>
<tr bgcolor=”#666666″>
<td>Sex</td>
<td><select name=”sex”>
<option value=”1″>Male</option>
<option value=”2″>Female</option>
</select></td>
</tr>
<tr bgcolor=”#666666″>
<td colspan=”2″><div align=”center”>
<input type=”submit” >
</div></td>
</tr>
</table>
</form>
EOT;
}
function add_to_database()
{
$firstname = trim($_POST['firstname']); // send the data of ‘firstname’ field to $firstname variable and clean up the
$lastname = trim($_POST['lastname']); // blank space using trim function
$username = trim($_POST['username']);
$email = trim($_POST['useremail']);
$web = trim($_POST['userweb']);
$country = trim($_POST['country']);
$sex = trim($_POST['sex']);
mysql_connect(“localhost”, “root”) or die(“Could not connect”); // Connetion to database using Hostname and Password
mysql_select_db(“nuhil”);
$sql = “INSERT INTO `site_users` ( // Insert Query to insert data in the table named site_users
`user_id`,
`first_name`,
`last_name`, // Column names of the table
`username`,
`user_email`,
`user_web`,
`country`,
`sex`)
VALUES (”,’$firstname’,'$lastname’,'$username’,'$email’,'$web’,'$country’,'$sex’)”; // Datas were stored in these variable
mysql_query($sql);
mysql_close();
return true;
}
?>
To show the inserted data. Save this file as show.php
<?php
$host=”localhost”;
$user=”root”;
$db=”nuhil”;
$link=mysql_connect($host,$user) or die(“Nooo”. mysql_error());
if($link) {
mysql_select_db($db) or die (“could not select $db”. mysql_error());
}
$result=mysql_query(“SELECT * FROM site_users”); // SELECT Query for retriving data from the database
$num_rows= mysql_num_rows($result);
print “$num_rows <br/>”;
print “<table border=1>”;
while($a_row=mysql_fetch_row($result)) {
print “<tr>”;
foreach($a_row as $field) {
print “<td> $field </td>”;
}
print “<td/><a href=\”edit_user.php?id=”.$a_row[0].”\”>Edit<a/></td>”;
print”</tr>”;
}
mysql_close($link);
?>
You are always welcome to me for further information and clear concept as i can.
Thank u very much for helping green horn peoples like me…:D
thnx… But I’m sure that you are not familier to most of the lines used to bulid the files. So dont be afraid. Try to learn line by line…
Hi!
I’m just new to php,& found your article useful, can you just tell me where to start to learn php – mysql … fundamentally?
Hi Dipali,
As you are new in PHP-MySQL i think you should begin from the begining. You can take help from internet based learning sites as standard learning method. For very begining you should take some faster help from a book written in your mother language. Then quickly you should go into a progressive leve book such as Beginners PHP written by 5 authors. Its all…..
Friend, post more php script in your wordpress.your php document is so small.if you post more and more script than we can learnt from them.
Hi,
Use WAMP Server
Automatically install all necessary application!!
Trust me, I have Checked few of PHP local servers and
came to conclusion that WAMP Server IS the best out there.
Easy to install, Intuitive to work with.
Great software for all PHP developers out there.
If you using Skype while working on developing
Make sure to start WHAP before Skype (and not the other way around).
That the only “bug” I have found.
Strongly recommend to use it!
hi…
I guess there are some flaws in your code. You have placed ‘?’ in place of double quotes.Please correct me if i am wrong.
Also, when i run dbtest.php…i get the codes after “EOT”.All the codes in the main page.May be i missed something or its coding problen. I dunno. plz help to fix it.
@rose
please see the red & underlined parts above of the script. I guess you have copied and paste the whole code in a note pad editor. Then the modern editor took the “> symbol as unknown character and when you ran that script in localhost, the PHP replaced that unknown symbol with a ?>.
That ” used in that script does not belong with modern keyboard.
Again The problem happened to EOT is that, the previous “> replaced with a ?>. so PHP took that as a closing tag. So, the EOT is acting as a normal text where as the real closing tag is on the last.
Please copy and paste the code in Macromedia Dreamweaver. You will face no problem.
Thnx