Home » PHP, Tech Corner

PHP and MySQL Basics

1 May 2009 No Comments Posted By:Vivek

PHP stands for Hypertext Preprocessor.PHP is a powerful server side scripting language for creating dynamic and interactive websites.The definite advantage of PHP over ASP.NET is that it free and can easily be embedded in html.PHP supports many databases(MY SQL,Oracle to name a few) and is cross platform(works under different platforms).

What you need:

In order to code in PHP you will need to install

  • PHP
  • MySQL database
  • Apache server(commonly used web server) or

You can install the WAMP server,which is a package that includes PHP,Apache and the MySQL database.

Follow this link to know how to install WAMP

Basic PHP syntax
A PHP block starts with the <?php tag and ends with the ?> tag.

Your first PHP program:

<html>
<body>
<?php
$txt1= “Welcome to www.three2tango.com”;
echo $txt1;
?>
</body>
</html>

echo is used for output to the screen.
As shown above,a PHP variable starts with the $ sign.You would have noticed that the variable is not declared anywhere.PHP is a loosely coupled language and we do not need to declare the variable to use it.Based on the input to the variable,PHP converts the variable into the correct datatype.

In the example above PHP automatically converts txt1 to a string variable when “Welcome to www.three2tango.com” is assigned to it.

<html>
<body>
<?php
$txt1= “Welcome to www.three2tango.com”;
$txt2=”And You are studying PHP”;
echo $txt1.”".$txt2;
?>
</body>
</html>

The above example will output “Welcome to www.three2tango.com And You are studying PHP”
‘.’ is the concatenation operator in PHP.

The $_GET and $_POST variables:

The $_GET variable is used to collect values from a form with method=”get”.
The $_POST variable is used to collect values from a form with method=”post”.
In addition you can make use of the $_REQUEST variable to get the results from a form data sent with both GET and POST methods.

MySQL and PHP:

To use MySQL from the WAMP server,click on the WAMP server icon and select localhost.click on the “phpmyadmin” link to open the interface to the mySQL database.Here you can create the database and tables.

I created a database “test” and added a table “persons” with the fields firstname,lastname,city and address.

Suppose you want to insert data into the table and the display the data.
First you create a html page from where you can input the values.The following is the html page.

<html>
<body>
<form action=”insert.php” method=”post”>
Firstname: <input type=”text” name=”firstname” />
Lastname: <input type=”text” name=”lastname” />
City: <input type=”text” name=”city” />
Address:<input type=”text” name=”address”/>
<input type=”submit” />
</form>
</body>
</html>

Note that i have used the POST method to send form data to insert.php.The insert.php file retrives values from the form using the $_POST variable, and also displays the inserted data.

Here is the insert.php code

<?php
$mycon = mysql_connect(“localhost”,”root”,”");
if (!$mycon)
{
die(‘Could not connect: ‘ . mysql_error());
}

mysql_select_db(“test”, $mycon);

$sql=”INSERT INTO Persons (LastName,FirstName,City,Address)
VALUES
(‘$_POST[lastname]‘,’$_POST[firstname]‘,’$_POST[city]‘,’$_POST[address]‘)”;

if (!mysql_query($sql,$mycon))
{
die(‘Error: ‘ . mysql_error());
}
echo “1 record added”;

$result=mysql_query(“select * from persons order by firstname”);

echo “<table border=’1′>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>City</th>
<th>Address</th>
</tr>”;

while($row = mysql_fetch_array($result))
{
echo “<tr>”;
echo “<td>” . $row['FirstName'] . “</td>”;
echo “<td>” . $row['LastName'] . “</td>”;
echo “<td>” . $row['City'].”</td>”;
echo “<td>” . $row['Address'].”</td>”;
echo “</tr>”;
}
echo “</table>”;
mysql_close($mycon)
?>

Here the output of the above code:

ScreenShot

mysql_connect is used to conect to the database.
mysql_close closes the connection.
mysql_select_db connects to the database-in the above example,itr connects to the test database.
mysql_query performs the query on the selected database
mysql_fetch_array gets the result of the query row after row in an associative array

Related posts:

  1. Building a Login Form using PHP/MySQL
  2. Data Encryption in PHP using crypt()
  3. PHP Forms
  4. Cookies and Sessions in PHP
  5. Creating user defined functions and classes in PHP

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>