PHP$SQL
PHP$SQL
• What is PHP?
PHP is a widely-used, open source scripting language
PHP scripts are executed on the server
PHP files can contain3n text, HTML, CSS, JavaScript,
and PHP code
PHP is an acronym for "PHP: Hypertext Preprocessor"
PHP code are executed on the server, and the result
is returned to the browser as plain HTML
PHP files have extension ".php“
What Can PHP Do?
<?php
echo "Hello World!";
?>
</body>
</html>
Comments in PHP
• Comments in PHP
• A comment in PHP code is a line that is not read/executed as part of the program. Its only purpose is to be read by someone
who is looking at the code
• <!DOCTYPE html>
<html>
<body>
<?php
// This is a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>
</body>
</html>
PHP Variables
• The if....else statement executes some code if a condition is true and another code
if that condition is false.
• Syntax
• if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
• example
• <?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
PHP - The if...elseif....else Statement
• The if....elseif...else statement executes different codes for more than two conditions.
• Syntax
• if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
}
• <?php
$t = date("H");
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
PHP - The switch Statement
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
Php loops
:
• Loops in PHP are used to execute the same block of code a specified number of times.
• we have the following looping statements:
• while - loops through a block of code as long as the specified condition is true
• do...while - loops through a block of code once, and then repeats the loop as long as the specified
condition is true
• for - loops through a block of code a specified number of times
• foreach - loops through a block of code for each element in an array
• The PHP while Loop
• The while loop executes a block of code as long as the specified condition is true
• Syntax
• while (condition is true) {
code to be executed;
}
• <?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
The PHP do...while Loop
• The do...while loop will always execute the block of code once, it will then check
the condition, and repeat the loop while the specified condition is true
• do {
code to be executed;
} while (condition is true);
• Example
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
•
?>
PHP for Loops
• The for loop is used when you know in advance how many times the script should run.
• Syntax
• for (init counter; test counter; increment counter) {
code to be executed;
}
• Parameters:
• init counter: Initialize the loop counter value
• test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If
it evaluates to FALSE, the loop ends.
• increment counter: Increases the loop counter value
• Example
• <?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
The PHP foreach Loop
• The foreach loop works only on arrays, and is used to loop through each
key/value pair in an array.
• Syntax
• foreach ($array as $value) {
code to be executed;
}
• Example
• <?php
$colors = array("red", "green", "blue", "yellow");
familyName(“bekele");
familyName(“yani");
familyName("Stale");
familyName("Kai Jim");
familyName("Borge");
?>.
PHP Functions - Returning values
• To let a function return a value, use the return statement:
• Example
• <?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}
• multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three
levels deep are hard to manage for most people
• PHP - Two-dimensional Arrays
• A two-dimensional array is an array of arrays
• a three-dimensional array is an array of arrays of arrays.
• Example
• $cars = array
(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
• For access s
• <?php
echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";
?>
);
PHP Forms
• The PHP superglobals $_GET and $_POST are used to collect form-data.
• The PHP $_GET and $_POST variables are used to retrieve information from forms, like
user input.
• The most important thing to notice when dealing with HTML forms and PHP is that
any form element in HTML page will automatically be available to your PHP scripts.
• <html>
<body>
</body>
</html>
s
• Before you can access and work with data in a database, you must
create a connection to the database.
• In PHP, this is done with the mysql_connect() function.
• Syntax
mysql_connect(servername,username,password);
• Parameter Description
– servername Optional. Specifies the server to connect to. Default value is
"localhost:3306"
– username Optional. Specifies the username to log in with. Default value is the
name of the user that owns the server process
– password Optional. Specifies the password to log in with. Default is ""
• Note: There are more available parameters, but the ones listed above
are the most important.
EXAMPLE
• In the following example we store the connection in a
variable ($con) for later use in the script.
• The "die" part will be executed if the connection fails:
<?php
$con = mysql_connect("localhost",“Gemechu",“pass456");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
?>
Closing a Connection
• Now we will create an HTML form that can be used to add new
records to the "Student" table.
<html>
<body>
<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
insert into form
• When a user clicks the submit button in the HTML
form in the example above, the form data is sent to
"insert.php".
• The "insert.php" file connects to a database, and
retrieves the values from the form with the PHP
$_POST variables.
• Then, the mysql_query() function executes the INSERT
INTO statement, and a new record will be added to
the database table.
• Below is the code in the "insert.php" page
EXAMPLE
<?php
$con = mysql_connect("localhost",“Gemechu",“pass456");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$sql="INSERT INTO student(FirstName, LastName, Age)
VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
SQL SELECT Statement
• The SELECT statement is used to select data from a
database.
• The SELECT statement is used to select data from a
database.
• Syntax
SELECT column_name(s) FROM table_name
• To get PHP to execute the statement above we must
use the mysql_query() function.
• This function is used to send a query or command to a
MySQL connection
SQL SELECT Statement
• The following example selects all the data stored in the “Student" table
(The * character selects all of the data in the table):
<?php
$con = mysql_connect("localhost",“Gemechu",“pass456");
if (!$con)S
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Student");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}
mysql_close($con);
?>
SQL SELECT Statement
• The example above stores the data returned by the mysql_query()
function in the $result variable.
• Next, we use the mysql_fetch_array() function to return the first row from
the recordset as an array.
• Each subsequent call to mysql_fetch_array() returns the next row in the
recordset.
• The while loop loops through all the records in the recordset.
• To print the value of each row, we use the PHP $row variable
($row['FirstName'] and $row['LastName']).
• The output of the code above will be:
Abebe Yonas
Belachewu Kedir
Hana Temesgen
The ORDER BY Keyword