• My PHP Pages

    This is a complete and free PHP programming course for beginners. Everything you need to get started is set out in section one below.

Jun 30, 2013

Posted by Unknown
3 comments | 5:55 AM


Today lesson is about how to insert data to mysql table using php mechanisms. In last two lessons we learned about how to make a connection and how to create a database and tables. if you have any problems with those things you better read last posts before go through this.


There is no much different between creating table it is just sql string. Lets look at the code.

//make connection
$con = mysql_connect("localhost","root","123");
//check connection
if(!$con)
{
die('Could not connect to the database:'.mysql_error());
}

//select database
$db=mysql_select_db("my_database",$con);
 if(!$db)
{
die('Could not connect to the database:'.mysql_error());

}
//making sql query string. This table has two columns as name and telephone number
$sql="INSERT INTO my_table('Rajika Nanayakkara',071999999)";

//Execute the mysql query and check is execution process is succeeded
if(mysql_query($con,$sql))
{
echo "Insert Successfully..! ";
}
else
{
echo "Error occurred while inserting data :".mysql_error($con); 
}

//close connection
mysql_close($con);





Read More..

Jun 29, 2013

Posted by Unknown
No comments | 5:57 AM

In the last lesson we learned about how to make a connection to mysql database. To create either database or table first we need to make a connection to the database server.


Please read last post if you don't know how to create the connection to mysql server.

Creating  a Database

We have to use new method to execute sql queries.
mysql_query(connection,sql query sting)
  • connection- return value of mysql_connect() function
  • Sql query string - sql query that you need to execute

mysql_query() function returns a Boolean value if query execute successfully it will return true value else false value. Lets look at the code


//make connection
$con = mysql_connect("localhost","root","123");
//check connection
if(!$con)
{
die('Could not connect to the database:'.mysql_error());
}
//making sql query string
$sql="CREATE DATABASE my_database";

//Execute the mysql query and check is execution process is succeeded
if(mysql_query($con,$sql))
{
echo "Database Created..!";
}
else
{
echo "Error occurred while creating database :".mysql_error($con); 
}

//close connection
mysql_close($con);




Creating a table

Difference between creating table and database is just sql query sting value and you have to select database to create table using mysql_select_db() function. lets make simple code..



//make connection
$con = mysql_connect("localhost","root","123");
//check connection
if(!$con)
{
die('Could not connect to the database:'.mysql_error());
}

//select database
$db=mysql_select_db("my_database",$con);
 if(!$db)
{
die('Could not connect to the database:'.mysql_error());

}
//making sql query string
$sql="CREATE TABLE my_table(name varchar(10),phone int)";

//Execute the mysql query and check is execution process is succeeded
if(mysql_query($con,$sql))
{
echo "Table Created..!";
}
else
{
echo "Error occurred while creating table :".mysql_error($con); 
}

//close connection
mysql_close($con);


Try to make your own database and create some simple tables using it. in next post i will explain how to insert data using php..

Cheers 




Read More..

Jun 28, 2013

Posted by Unknown
No comments | 8:29 AM


Lets connect mysql database using php. there is some special php functions for this..

mysql_connect(host,username,password)

Host - host is optional its host name of mysql database. most of the time its localhost

username - mysql username

password - mysql password


There is a another function to do this.

mysqli_connect(host,username,password,dbname)

Note : it is mysqli_connect()  this is new method php introduced "i" stands for improvements . there is number of improvements in this functions. you can find them in php documentation 


Close mysql connection

mysql_close() or mysqli_close()


Select Database

mysql_select_db(database_name,connection_string);

Database name - Default database to connect with.

Connection string - Connection string is output of  mysql_connect()

Ok now lets make simple connection to mysql database using this two methods...



//make connection
$con = mysql_connect("localhost","root","123");
//check connection
if(!$con)
{
die('Could not connect to the database:'.mysql_error());
}
//select database
$db=mysql_select_db("my_db",$con);
 if(!$db)
{
die('Could not connect to the database:'.mysql_error());

}

//close connection
mysql_close($con);





Read More..

Jun 23, 2013

Posted by Unknown
10 comments | 9:45 AM

Today we gonna talk about how to work with radio buttons using php. a sample radio button image is bellow. Lets create a sample HTML code.




File name : radio.html

<html>
<head>
<title>radio form</title>
</head>

<body>

<form name="radiobtnform" method="post" action="process.php">

<form name="radiobtnform" method="post" action="process.php">
<label>Gender : </label>
<input type="radio" name="gender" value="male"/>Male
<input type="radio" name="gender" value="female"/>Female
<input type="submit" name="sendgender" value="Send"/>
</form>

</send>
</html>

Ok.. now we have done with html file. today i'm going to get form data to separate php file. so as we have talked in previous Form lesson i should set php file name to action attribute value. you can see that i have set action attribute value as process.php.

File name : process.php

<?php

if(isset($_POST['sendgender']))
{
$selected_radio = $_POST['gender'];
echo "You are : ".$selected_radio;
}

?>






Read More..

Jun 21, 2013

Posted by Unknown
3 comments | 9:43 AM


Today we talk about how to work with forms in PHP.. lets make a simple form using HTML



<html>
<head>
<title>my form</title>
</head>
<body>
<form name="myform" action="" method="post">
<lable> Enter Your Name : </lable>
<input type="text" name="name"/>
<input type="submit" name="submit_btn" value="Hit on me !"/>
</form>
<!-- PHP Part begins after this line -->



Note- There is a two very impotent attributes in form tag


  • Action attribute
This action attributes use for set where to send data once form is submitted. if we keep action attribute blank it will send form data to self page or if we use some page name it will send form data to that page.


  • Method attribute
There is two method attribute values that we can use one is 'post' other once is 'get'. both methods are use to send data but they uses different methods. if we use 'post' method it will send data using HTTP port translation or if we use get method it will send data using URL variables



How to catch submitted data using PHP

There is two methods to catch data as well. if we use 'post' method to send data we have to use $_POST array to catch data or if we use 'get' method to send data we have to use $_GET array here.

I used 'post' method before so i will use $_POST array here..

<?php

if(isset($_POST['submit_btn']))
{
$name = $_POST['name'];
echo "Your Name is :". $name;
}
?>

Note : isset() method is use for identify that variable is set and is not null



Completed Code..

<html>
<head>
<title>my form</title>
</head>
<body>
<form name="myform" action="" method="post">
<lable> Enter Your Name : </lable>
<input type="text" name="name"/>
<input type="submit" name="submit_btn" value="Hit on me !"/>
</form>

<!-- PHP Part begins after this line -->
<?php

if(isset($_POST['submit_btn']))
{
$name = $_POST['name'];
echo "Your Name is :". $name;
}
?>
<!-- PHP part ends -->

</body>
</html>

Note : If we use a value for action attribute we have to place the php code in relevent php page that we use as action attribute value. as a example if you set your action attribute value as action="process.php" you have to place your php code inside process.php file..






Read More..

Jun 20, 2013

Posted by Unknown
No comments | 12:04 AM

Today lession is how to work with PHP arrays. An array is a variable which can store number of values at once


<?php
$phones = array("nokia","sony","samsung");

echo "Phones : ".$phone[0].",".$phone[1]." and ".$phone[2];


?>

result - Phones : nokia , sony and samsung


We can count number of values in the array using count() function.


<?php
$phones = array("nokia","sony","samsung");

echo "Phones : ".$phone[0].",".$phone[1]." and ".$phone[2];

echo "<br/> Number of values in this array is : ".count($phones);
?>

result - Phones : nokia , sony and samsung
            Number of values in this array : 3


Lets create an Associative arrays

There is no much difference between associative array and normal array but when we work with normal arrays, we retrieve data using index number. but if we use associative array we can use name keys to retrieve data. 

Lets see how to create an associative array.



<?php

$age = array("kasun"=>50,"nimal"=>55,"dasun"=>57)

echo "kasun is : ".$age['kasun']."years old";
?>



Read More..

Jun 17, 2013

Posted by Unknown
No comments | 4:08 AM
PHP also has if else , while , do while and for statements.
lets see how to write a simple if else statement. i am not going to talk in depth about this statements  since php statements are same as other programming languages


<?php

$today =  date_format(date_create(date("y-m-d")), 'l');

echo "Today is : ".$today."<br>";

if ($today != 'sunday' or $today != 'saturday')
{
  echo "Have a nice day !";
}
else
{
echo "Have a nice weekend !";
}


?>

Please don't mind about data function that i have used. i will explain about data functions later.
you can try to write codes using other statments. if any problem please contact me through email - rajika4ever@gmail.com




Read More..

Jun 14, 2013

Posted by Unknown
1 comment | 8:45 PM



Variables are use for storing values, like string numbers or arrays. when a variable declared, it can be used over and over again in your script.

Note : All Variables in PHP start with a $ sign symbol

example : $my_variable = 100;

-----------------------------------------------------------------------------------------------------------

PHP is loosely typed language 


In PHP, a variable dose not need to be declared before adding a value to it.

<?php
$mytxt = "My PHP Page";
$x = 100;
?>


in the example above, you see that you do not have to tell PHP which data type the variable is, PHP automatically converts the variable to the correct data type , depending on its assigned value.


Naming Rules for variables 


A variable name must start with a letter or an underscore "_" (ex : $_myval or $myval)

A variable name can only contain alpha-numeric characters and underscores ( a-z , A-Z , 0-9 and _ )

A variable name should not contain spaces.


String Variables in PHP

When you store string data in variable , you should use " " marks. ( ex : $mystring = "your name" )


The concatenation operator


The concatenation operator (.) is used to put two string values together.

<?php
$txt1="My Name is : ";
$txt2="Rajika";
echo $txt1.$txt2;
?>

Output : My Name is : Rajika




Read More..

Jun 13, 2013

Posted by Unknown
No comments | 11:34 AM

                                                            Video Tutorial in sinhala
                             For more sinhala video tutorials visit http://myphpvideo.blogspot.com




Comments in PHP

          In PHP we use // to make single line comments and  /* your comment goes here */ to make a large comment block.

example : 

<?php

//This is a single line comment

/*
This is 
a comment
block or in other word
multiline comment 
*/




Read More..

Jun 11, 2013

Posted by Unknown
17 comments | 12:08 PM

                                                   Video Tutorial in sinhala
                             For more sinhala video tutorials visit http://myphpvideo.blogspot.com



How to start coding with PHP

Normally we are writing PHP codes inside HTML so there should be a way to identify HTML and PHP code separately so when we start writing PHP codes always we need to use " <?PHP " at the start and " ?>" mark at the end.

Our first PHP program


<head>
<title>My first php program</title>

<body>

<?php

echo "This is my first PHP program !";

?>

</body>

</head>


Note : echo keyword is using for print..




Read More..

Posted by Unknown
No comments | 11:56 AM

What You Should Already Know

Before you continue you should have a basic understanding of the following:
  • HTML
  • JavaScript
If you want to study these subjects first, find the tutorials on our Home page.

What is PHP?

  • PHP stands for PHP: Hypertext Preprocessor
  • PHP is a widely-used, open source scripting language
  • PHP scripts are executed on the server
  • PHP is free to download and use
lampPHP is simple for beginners.PHP also offers many advanced features for professional programmers.


What is a PHP File?

  • PHP files can contain text, HTML, JavaScript code, and PHP code
  • PHP code are executed on the server, and the result is returned to the browser as plain HTML
  • PHP files have a default file extension of ".php"

What Can PHP Do?

  • PHP can generate dynamic page content
  • PHP can create, open, read, write, and close files on the server
  • PHP can collect form data
  • PHP can send and receive cookies
  • PHP can add, delete, modify data in your database
  • PHP can restrict users to access some pages on your website
  • PHP can encrypt data
With PHP you are not limited to output HTML. You can output images, PDF files, and even Flash movies. You can also output any text, such as XHTML and XML.

Why PHP?

  • PHP runs on different platforms (Windows, Linux, Unix, Mac OS X, etc.)
  • PHP is compatible with almost all servers used today (Apache, IIS, etc.)
  • PHP has support for a wide range of databases
  • PHP is free. Download it from the official PHP resource: www.php.net
  • PHP is easy to learn and runs efficiently on the server side

Read More..

Posted by Unknown
No comments | 11:48 AM

 PHP: Hypertext Preprocessor

Read More..