PHP Tutorials


I transferred to the new site.. myprogrammingtutorials.com
March 5, 2008, 10:53 pm
Filed under: TRANSFERRED TO NEW SITE

Please visit my new site instead of this..thx guys!!

Visit my new site —>> http://myprogrammingtutorials.com



Create tables and database in PHP/MySQL
March 4, 2008, 4:11 am
Filed under: PHP/MySQL

Create tables and database in PHP/MySQL
A database is composed of one or more tables.

Create a database
We use the CREATE DATABASE statement in MySQL to create a database.

Syntax:
CREATE DATABASE database_name

NOTE: We must use the mysql_query() function so that PHP will execute the given command like CREATE DATABASE database_name. This function is used to send a command or query to a MySQL connection.

In this example, we will be making a database named newdatabase:

<?php
$connect = mysql_connect(“localhost”,”bryan”,”777”);
if(!$connect)
{
die(“failed to connect: “ . mysql_error());
}

$makedatabase = mysql_query(“CREATE DATABASE newdatabase”,$connect);

if(!$makedatabase)
{
echo “Failed to create database: “ . mysql_error();
}
else
{
echo “Database created”;
}

mysql_close($connect);
?>

Creating a table in MySQL
We need to use the CREATE TABLE statement when making a table in MySQL.

Syntax:
CREATE TABLE mytable
{
column_name1 data_type,
column_name2 data_type,
…………..
}

Example:
This example will create an employee table and the 3 columns are emp_num, emp_name, and emp_age.

<?php
$connect = mysql_connect(“localhost”,”bryan”,”777”);
if (!$connect)
{
die(“Failed to connect: “ . mysql_error());
}

$makedatabase = mysql_query(“CREATE DATABASE newdatabase”,$connect);

if(!$makedatabase)
{
echo “Failed to create database: “ . mysql_error();
}
else
{
echo “Database created”;
}

mysql_select_db(“newdatabase”, $connect);

$create_table = “CREATE TABLE employee
(
emp_num int,
emp_name varchar(50),
emp_age int
)”;

mysql_query($create_table,$connect);
mysql_close($connect);
?>

NOTE: Before creating a table, you must first make sure that you have selected a database by using the function mysql_select_db().

Primary Keys

Every table should have a primary key. Primary keys are used to uniquely identify the rows in a table. There should be no duplicates in the values of the primary key. They value of primary can’t contain a null.

In the example below, we made the emp_id as the primary key. Primary key is often a number and used with AUTO_INCREMENT. AUTO_INCREMENT increases by 1 each time a new record is added. We should also use NOT NULL to make sure that the primary key will not have null values.

$create_table = “CREATE TABLE employee
(
emp_num int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(emp_num),
emp_name varchar(50),
emp_age int
)”;

mysql_query($create_table,$connect);

mysql_close($connect);



Connect PHP to a MySQL Database
March 4, 2008, 2:38 am
Filed under: PHP/MySQL

Connect PHP to a MySQL Database
MySQL is the most common database used with PHP.

Connecting PHP to a MySQL Database
You must first connect to a database so you can access and work with data in a database.

 

Syntax (Connect PHP to MySQL):
mysql_connect(servername, username, password);

Example (Connect PHP to MySQL):
In this example, I stored the connection in the $connect variable so we will be using it later and if it failed to connect then the die statement will be executed.

<?php
$connect = mysql_connect(“localhost”,”bryan”,”777”);
if(!$connect)
{
die(“Failed to connect: “ . mysql_error());
}
?>

Closing a connection
As soon as the script ends, the connection will be automatically closed but you can close it by using the mysql_close() function.

Here is an example:

<?php
$connect = mysql_connect(“localhost”,”bryan”,”777”);
if(!$connect)
{
die(“Failed to connect: “ . mysql_error());
}
mysql_close($connect);
?>



PHP $_COOKIE
March 4, 2008, 12:59 am
Filed under: PHP

PHP $_COOKIE
This variable is often used to identify a user. This is stored on the user’s computer so that every user will be identified if who are they. You can create and get the value of a cookie.

setcookie() function

This function is used to create a cookie. This should appear before the <html> tag.

Syntax:

setcookie (name, value, expire, path, domain);


Example (create a cookie):

<?php
setcookie(“firstname”, “Bryan”, time()+7200);
?> //this should be written before the <html> tag

Here is an example on how to retrieve the value of a cookie:
<?php
echo $_COOKIE[“firstname”];
?>

isset() function
This can be used to check if a cookie has been set.

Example (using isset() function):
<html>
<body>
<?php
if (isset($_COOKIE[“firstname”]))
{
echo “You are “ . $_COOKIE[“firstname”];
}
else
{
echo “Please input your firstname”;
}
?>
</body>
</html>


You can view all cookies by this:

<?php
print_r($_COOKIE);
?>

You can delete a cookie by setting its expiration date in the past like this:
<?php
setcookie(“firstname”,””,time()-7200);
?>



PHP $_REQUEST
March 4, 2008, 12:52 am
Filed under: PHP

PHP $_REQUEST
This variable contains the content of both $_GET, $_COOKIE, and $_POST.

This can get the values passed from the form with both the GET and POST methods.

Here are 2 examples:

<form action=”myphp.php” method=”POST”>
Firstname: <input type=”text” name=”firstname” />
Lastname: <input type=”text” name=”lastname” />
<input type=”submit” />
</form>

<?php
echo “Firstname: “ $_REQUEST[“firstname”]; . <br />;
echo “Lastname: “ $_REQUEST[“lastname”];
?>

OR

<form action=”myphp.php” method=”GET”>
Firstname: <input type=”text” name=”firstname” />
Lastname: <input type=”text” name=”lastname” />
<input type=”submit” />
</form>

<?php
echo “Firstname: “ . $_REQUEST[“firstname”] . “<br />”;
echo “Lastname: “ . $_REQUEST[“lastname”] ;
?>

 



PHP $_POST
March 4, 2008, 12:41 am
Filed under: PHP

PHP $_POST
This variable is used to get a value from a form. Unlike $_GET, $_POST is invisible to users and will not display the user’s input in the URL and has no limit.

Example:
<form action=”myphp.php” method=”POST”>
Firstname: <input type=”text” name=”firstname” />
Lastname: <input type=”text” name=”lastname” />
<input type=”submit” />
</form>

When the user clicks the submit button, it will not display the user’s input in the URL like the $_GET. It will simply look like this:

http://myphp.com/myphp.php

Now, we can use the two values passed from the form by using the $_POST variable:

<?php
echo “Firstname: “ $_POST[“firstname”]; . <br />;
echo “Lastname: “ $_POST[“lastname”];
?>

 

 



PHP $_GET
March 4, 2008, 12:28 am
Filed under: PHP

PHP $_GET
This variable is used to get a value from a form. Below is an example:

<form action=”myphp.php” method=”GET”>
Firstname: <input type=”text” name=”firstname” />
Lastname: <input type=”text” name=”lastname” />
<input type=”submit” />
</form>

When the user clicks the submit button, it will then display a different URL depending on the user’s input. I’ll use Bryan for first name and King for last name so the URL will look like this:

Http://www.myphp.com/myphp.php?firstname=Bryan&lastname=King

Now, we can use the two values passed from the form by using the $_GET variable:

<?php
echo “Firstname: “ . $_GET[“firstname”] . “<br />”;
echo “Lastname: “ . $_GET[“lastname”] ;
?>



PHP Functions
March 2, 2008, 1:30 pm
Filed under: PHP

PHP Functions

In this tutorial, i will teach you how to make functions in PHP and how to use it. A function is a block of code that we can call and use whenever we want.

Below is an example of a function that will display the text “learn PHP” when called:

<html>
<body>
<?php
function displayMessage()
{
echo “learn PHP.”;
}
displayMessage();
?>
</body>
</html>

Another example where we will be using the function we made:

<html>
<body>
<?php
function displayMessage()
{
echo “learn PHP.”;
}

echo “I want to “;
displayMessage();
?>
</body>
</html>

Output is:
I want to lean PHP

PHP Functions – Adding Parameters

We can also add parameter to our functions so it can have different outputs depending on the user input. A parameter is just like a variable. We should place the parameters inside the parentheses like this displayMessage($myMessage).

Here is an example where it will display our inputted message:

<html>
<body>
<?php
function displayMessage($myMessage)
{
echo $myMessage . “. <br />”;
}
echo “My message is “;
$displayMessage(“Welcome to my blog, if you have any questions just leave a comment”);
?>
</body>
</html>

Output is:
My message is Welcome to my blog, if you have any questions just leave a comment.

Here is another example where we will be having 2 parameters:

<html>
<body>
function displayMessage($myMessage, $punctuation)
{
echo $myMessage . $punctuation . “<br />”;
}
echo “My message is “;
displayMessage(“Please register here”,”.”);
?>
</body>
</html>

Output is:
My message is Please register here.

PHP Functions – Return Values
Functions can also return values.

Here is an example:

<html>
<body>
<?php
function fullName($fname, $lname)
{
$fullname = $fname . ” ” . $lname;
return $fullname;
}

echo”My full name is ” . fullName(“Bryan Joseph”,”King”);
?>
</body>
</html>

Output is:
My full name is Bryan Joseph King



PHP Looping
March 2, 2008, 8:58 am
Filed under: PHP

This is used to execute same codes at specified number of times.

There are four PHP looping statements:

  • while – will continue looping through the code as long as the condition is true
  • do…while – loops through the code then if the condition is true then it continues otherwise it will end the loop
  • for – loops through the code while the condition is true
  • foreach – loops through the code for each element in an array

The while statement
This will continue to execute the code as long as the condition is true. Below is the syntax:

while (condition)
code to be executed;

Below is an example:

It will continue to execute the code as long as the variable x is less than 3. Variable x will increase by 1 every time the code is executed.

<html>
<body>
<?php
$x = 0;
while($x<3)
{
echo “Display me 3 times. <br />”;
$x++;
}
?>
</body>
</html>

The do…while statement

Loops through the code then if the condition is true then it continues otherwise it will end the loop.
Below is the syntax:

do
{
code to be executed;
}
while (condition);

Below is an example:

It will continue to execute the code as long as the variable x is less than 3. Variable x will increase by 1 every time the code is executed.

<html>
<body>
<?php
$x=0;
while ($x<3)
{
echo “display me 3 times. <br />”;
$x++;
}
?>
</body>
</html>

The for statement
Loops through the code while the condition is true. Below is the syntax:

for (initialization; condition; increment)
{
code to be executed;
}

Below is an example:
It will display the text “Display me 3 times.” 3 times.

<html>
<body>
<?php
for($x=0;$x<3;$x++)
{
echo “Display me 3 times. <br />”;
}
?>
</body>
</html>

The foreach statement
This is used to loop through arrays. For every loop, the value of the current array is assigned to $value so on the next loop, you’ll be looking at the next element. Below is the syntax:

foreach (array as value)
{
code to be executed;
}

Below is an example:

<html>
<body>
<?php
$myarray = array(“Learn”, “PHP”,”Now”);

for each($myarray as $value)
{
echo $value . “<br />”;
}

?>
</body>
</html>



PHP String
March 2, 2008, 7:40 am
Filed under: PHP

PHP Strings
A String is used to store and manipulate a piece of text. This data type is used when we want to store character strings. In this tutorial, i am going to discuss some common functions and operators used to manipulate strings in PHP. Below is an example:

<?php
$txt = “Welcome.”;
echo $txt;
?>

Output:

Welcome.

The Concatenation Operator
There is only one string operator in PHP and this is the dot (.) operator and is used to combine to strings together. Below is an example:

<?php
$firsttext = “Learn”;
$secondtext = “PHP”;
echo $firsttext . ” ” . $secondtext;
?>

Output:

Learn PHP

The strlen() function
This is used to get the length of the string. Below is an example:

<?php
echo strlen(“Learn PHP”);
?>


Output:
9