Php/Mysql Tutorial: Introduction To Database
Php/Mysql Tutorial: Introduction To Database
Introduction to Database
Goal of this tutorial
<?php
…
?>
Variables in PHP
?>
Echo
The PHP command ‘echo’ is used to
output the parameters passed to it
The typical usage for this is to send data to the
client’s web-browser
Syntax
void echo (string arg1 [, string argn...])
In practice, arguments are not passed in
parentheses since echo is a language
construct rather than an actual function
Echo example
<?php
$foo = 25; // Numerical variable
$bar = "Hello"; // String variable
Notice how echo ‘5x5=$foo’ outputs $foo rather than replacing it with 25
Strings in single quotes (‘ ‘) are not interpreted or evaluated by PHP
<br> for new line
Arithmetic Operations
<?php
$a=15;
$b=30;
$total=$a+$b;
Print $total;
Print "<p><h1>$total</h1>";
// total is 45
?>
$a - $b // subtraction
$a * $b // multiplication
$a / $b // division
$a += 5 // $a = $a+5 Also works for *= and /=
Concatenation
<?php
$string1= "Hello";
$string2="PHP";
$string3=$string1 . " " . $string2;
Print $string3;
?>
Hello PHP
# Shell-style comments
/* C-style comments
These can span multiple lines */
Escaping the Character
“Computer Science”
PHP Control Structures
Control Structures: Are the structures within a language that
allow us to control the flow of execution through a program or
script.
Grouped into conditional (branching) structures (e.g. if/else) and
repetition structures (e.g. while loops).
Example if/else if/else statement:
if ($foo == 0) {
echo ‘The variable foo is equal to 0’;
}
else if (($foo > 0) && ($foo <= 5)) {
echo ‘The variable foo is between 1 and 5’;
}
else {
echo ‘The variable foo is equal to ‘.$foo;
}
If ... Else...
<?php
if($user=="John")
If (condition) {
Print "Hello John.";
{ }
else
Statements; {
Print "You are not John.
} <br>";
}
Else ?>
{
Statement;
}
While Loops <?php
$count=0;
While($count<3)
{
While (condition) Print "hello PHP. ";
$count += 1;
{ // $count = $count + 1;
// or
Statements; // $count++;
}
}
print $count;
?>
$datedisplay=date(“l, F m, Y”);
Wednesday, April 1, 2009 Print $datedisplay;
# If the date is April 1st, 2009
# Wednesday, April 1, 2009
Month, Day & Date Format Symbols
M Jan
F January
m 01
n 1
Day of Month d 01
Day of Month J 1
Day of Week l Monday
Day of Week D Mon
Functions
Functions MUST be defined before then can be
called
Function headers are of the format
function functionName($arg_1, $arg_2, …, $arg_n)
Note that no return type is specified
Unlike variables, function names are not case
sensitive (foo(…) == Foo(…) == FoO(…))
Function example
<?php
// This is a function
function foo($arg_1, $arg_2)
{
$arg_2 = $arg_1 * $arg_2;
return $arg_2;
}
$result_1 = foo (12, 3); // call the function and store the result
echo $result_1; // Outputs 36
echo ("<BR>");
echo foo (12, 3); // call the function & Outputs 36
?>
Class & Function example
<?php
class Calculate {
private $total=0;
public function getSum (int $a, int $b) {
$this->total = $a+$b;
return $this->total;
}
}
$app = new Calculate;
print ($app->getSum (93,8)); //Output 101
?>
PHP - Forms
•Access to the HTTP POST and GET data is simple in PHP
•The global variables $_POST[] and $_GET[] contain the
request data
<?php
if ($_POST["submit"])
echo "<h2>You clicked Submit!</h2>";
else if ($_POST["cancel"])
echo "<h2>You clicked Cancel!</h2>";
?>
<form action="form.php" method="post">
<input type="submit" name="submit" value="Submit">
<input type="submit" name="cancel" value="Cancel">
</form>
http://www.cs.kent.edu/~nruan/form.php
WHY PHP – Sessions ?
Whenever you want to create a website that allows you to store and display
information about a user, determine which user groups a person belongs to,
utilize permissions on your website or you just want to do something cool on
your site, PHP's Sessions are vital to each of these features.
Cookies are about 30% unreliable right now and it's getting worse every day.
More and more web browsers are starting to come with security and privacy
settings and people browsing the net these days are starting to frown upon
Cookies because they store information on their local computer that they do
not want stored there.
PHP has a great set of functions that can achieve the same results of Cookies
and more without storing information on the user's computer. PHP Sessions
store the information on the web server in a location that you chose in
special files. These files are connected to the user's web browser via the
server and a special ID called a "Session ID". This is nearly 99% flawless in
operation and it is virtually invisible to the user.
PHP - Sessions
•Sessions store their identifier in a cookie in the client’s browser
•Every page that uses session data must be proceeded by the
session_start() function
•Session variables are then set and retrieved by accessing the global
$_SESSION[]
•Save it as session.php
<?php
session_start();
if (!$_SESSION["count"])
$_SESSION["count"] = 0;
if ($_GET["count"] == "yes")
$_SESSION["count"] = $_SESSION["count"] + 1;
echo "<h1>".$_SESSION["count"]."</h1>";
?>
<a href="session.php?count=yes">Click here to count</a>
http://www.cs.kent.edu/~nruan/session.php
Avoid Error PHP - Sessions
PHP Example: <?php
echo "Look at this nasty error below:<br />";
session_start();
?>
Error!
http://www.cs.kent.edu/~nruan/session_destroy.php
PHP Overview
Easy learning
Syntax Perl- and C-like syntax. Relatively
easy to learn.
Large function library
Embedded directly into HTML
Interpreted, no need to compile
Open Source server-side scripting language
designed specifically for the web.
PHP Overview (cont.)
Conceived in 1994, now used on +10 million
web sites.
Outputs not only HTML but can output XML,
images (JPG & PNG), PDF files and even Flash
movies all generated on the fly. Can write these
files to the file system.
Supports a wide-range of databases (20+ODBC).
PHP also has support for talking to other
services using protocols such as LDAP, IMAP,
SNMP, NNTP, POP3, HTTP.
First PHP script
Save as sample.php:
<!– sample.php -->
<html><body>
<strong>Hello World!</strong><br />
<?php
echo "<h2>Hello, World</h2>"; ?>
<?php
$myvar = "Hello World";
echo $myvar;
?>
</body></html>
https://www.tutorialspoint.com/php_webview_online.php
How to Create Table in MySQL database on PHPmyadmin
using PHP and WAMP server ?
WampServer
Practical Time
Functions Covered
(PHP 4, PHP 5)
mysql_connect() include()
mysql_query() mysql_num_rows()
mysql_fetch_array() mysql_close()
Functions Covered
(PHP 7.0)
mysqli_connect() require_once
mysqli_query() mysqli_num_rows()
mysqli_fetch_array() mysqli_close()
Example – Acquire data from mysql
and display in a table format
db_config.php [Define username and password, server address and database name]
Exchanging Data
When exchanging data between a browser and a server, the data
can only be text.
JSON is text, and we can convert any JavaScript object into JSON,
and send JSON to the server.
We can also convert any JSON received from the server into
JavaScript objects.
This way we can work with the data as JavaScript objects, with no
complicated parsing and translations.
JSON Format
<?php
$response["product"] = array();
$product = array("pid"=>"11", "name"=>"brown rice", "price"=>"10.50");
array_push($response["product"], $product);
$response["success"] = 1;
echo json_encode($response);
?>
{"product":[{"pid":"11","name":"brown
rice","price":"10.50"},{"pid":"22","name":"Thai
rice","price":"11.80"}],"success":1}