Learn PHP: An Introductory Tutorial for beginners

Script Writing :: Simple introductory tutorial for PHP scripting

Section 1:
For start learning PHP, you must have installed PHP on your system. With this you
need Apache and Mysql. Then you can start your PHP learning path from basic to advance.
For installing PHP, MySQL and other related software on your system, XAMPP will be the easiest. It will install some of the items you may not need generally but that is good for learning, you will find useful.

XAMPP is a very easy to install Apache Distribution for Linux, Solaris, Windows and Mac OS X. The package includes the Apache web server, MySQL, PHP, Perl, a FTP server and phpMyAdmin.

Download XAMPP.

Let's start from very basic PHP scripts. Here you will get a glimpse of PHP basic programming.
Here you have a form, which will submit your inputs to another page (page2.php).

page1.php


<html>
<head>
<title>Page Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<!-- Form start -->
<form name="frm1" method="post" action="page2.php" >
 Name: <input type="text" name="name"  value="" maxlength="100" size="20" />
 <br />
 Age:    <input type="text" name="age"  value="" maxlength="5" size="3" />
 <br />
 <input type="submit" name="submit1" value="" />
</form>
<!-- End  Form-->
</body>
</html>
In page2.php, you will get all the inputs from page1.php. You will know quickly how
to get input from users and show back to users. You will also learn how to use PHP
comments and HTML comments with how to separate and mix PHP and HTML code. Just check line
by line, all the codes in this page.

page2.php


<html>
<head>
<title>Page Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
// Get all your input here
echo '<pre>';
print_r($_POST);
echo '</pre>';
?>
Your Name is: <?php echo $_POST['name']; ?> and age is <?php echo $_POST['age'];?>.
<?php
/*
Now you can see how I separated and mixed HTML code with PHP code. '<?php' is start and
'?>' is end tag for PHP. All the lines under these tag will be checked by PHP parser.
echo is like print in other language. Print is also available in PHP.
$_POST is for getting all the post-ed data. In first page you have used
method="post", so you will use $_POST for getting your input.
For method ="GET", you will need $_GET super global.
*/
?>
<br>
In PHP "// comments" is used for single line of comments. "/* comments */" is used
for multiple line comments. In HTML <!-- html comments --> is used for HTML comments.
</body>
</html>
Now you have idea of basic PHP programming. You just practiced simple PHP scripting.

Now come here if you are not familiar with any programming language or you want to
learn more. Instead you should note it for reading after this tutorial.
PHP Introductory Scripting - Get more basics from here

You will get User Contributed Notes! section on each page there.
No need to learn those this time and continue with the rest.

::Section 2::
PHP and MySQL
Now create a table name tblone. After that you can use the form to submit data.
For this you need user-password of a MySQL Server.


Run this query in phpmyadmin or run this query after logging from mysql client.
MySQL client- On DOS prompt:
>mysql -u user -p
>use databaseName
OR
>create database 'databaseName';
after this run below query.
--------------
MySQL create table query:
Create Table: CREATE TABLE `tblone` (
`name` varchar(100) NOT NULL default '',
`age` smallint(6) NOT NULL default '0',
PRIMARY KEY  (`name`)
);
page3.php

<?php
$host = 'localhost';
$user  = 'mysqluser';
$pwd  = 'mysqlpwd';
$dbName = 'database';
$conn = mysql_connect ($host, $user, $pwd) or die ('Connection Failed.<br>');
$db = mysql_select_db ($dbName) or die ('DB selection failed.');
if (count($_POST) > 0) {
$name = $_POST['name'];
$age    = $_POST['age'];
$sql = "INSERT INTO tblone (name, age) values ('$name', $age)";
$res = mysql_query($sql);
if (!$res) echo mysql_error();
if ($res and mysql_affected_rows() > 0) $msg = 'Data Inserted';
}
?>
<html>
<head>
<title>Page Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<!-- Form start -->
<form name="frm1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" >
Name: <input type="text" name="name"  value="" maxlength="100" size="20" />
<br />
Age:    <input type="text" name="age"  value="" maxlength="5" size="3" />
<br />
<input type="submit" name="submit1" value="" />
</form>
<!-- End  Form-->
</body>
</html>
Now if you have successfully created table and inserted data using page3.php then
run this page.
This page will give you all the data you entered in mysql.

page4.php

<html>
<head>
<title>Page Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
$host = 'localhost';
$user  = 'mysqluser';
$pwd  = 'mysqlpwd';
$dbName = 'database';
$conn = mysql_connect ($host, $user, $pwd) or die ('Connection Failed.<br>');
$db = mysql_select_db ($dbName) or die ('DB selection failed.');
$sql2 = "SELECT name, age from tblone WHERE 1";
$res2 = mysql_query($sql2) or die (mysql_query());
if ($res2 and mysql_num_rows() > 0) {
while ($row = mysql_fetch_assoc($res2)) {
echo $row['name'] . '    ' . $row['age'];
}
}
?>
</body>
</html>
I assume you got a good taste of PHP basics from here and I wish you will very soon grab all the details of PHP.
Learn CSS
  • # 1 - by the_nerd_guy

    Hey great tutorials. Thanks for sharing.

    PHP Tutorials

  • # 2 - by the_nerd_guy

    Hey great tutorials. Thanks for sharing.PHP Tutorials

Comments are open for an year period. Please, write here on Facebook page.