Server Side Scripting
Server Side Scripting
PHP INTRODUCTION
resource: www.php.net
> PHP is easy to learn and runs efficiently on the
server side
PHP INTRODUCTION
Some info on MySQL which we will cover in the next
workshop...
applications
> MySQL supports standard SQL
> Arithmetic
> Assignment
> Comparison
> Logical
PHP OPERATORS
PHP OPERATORS
PHP OPERATORS
PHP OPERATORS
PHP CONDITIONAL STATEMENTS
to do this.
> In PHP we have the following conditional
statements...
PHP CONDITIONAL STATEMENTS
> if statement - use this statement to execute
some code only if a specified condition is true
> if...else statement - use this statement to
In
the following example we assign the index
manually:
PHP NUMERIC ARRAYS
In the following example you access the variable
values by referring to the array name and index:
> Often when you write code, you want the same
block of code to run over and over again in a row.
Instead of adding several almost equal lines in a
script we can use loops to perform a task like this.
> In PHP, we have the following looping
statements:
PHP LOOPS
number of times
> foreach - loops through a block of code for each
element in an array
PHP LOOPS - WHILE
The while loop executes a block of code while a
condition is true. The example below defines a loop
that starts with
i=1. The loop will
continue to run as
long as i is less
than, or equal to 5.
i will increase by 1
runs:
PHP LOOPS - WHILE
PHP LOOPS – DO ... WHILE
function.
> You may call a function from anywhere within a
page.
PHP FUNCTIONS
Adding parameters...
> To add more functionality to a function, we can
Notice how the URL carries the information after the file name.
PHP FORMS - $_GET FUNCTION
connectivity such as
mysqli_connection()
mysqli_select_db()
mysqli_query()
mysqli_result()
mysqli_close()
PHP MYSQLI_CONNECTION()
FUNCTION:
PHP mysqli_connect() function is used to connect
with MySQL database.
It returns resource if connection is established or
null.
Syntax of mysql_connection
connection mysqli_connect(server, user, password);
server:
Optional - The host name running database server. If
not specified then default value is localhost:3306.
User:
Optional - The username accessing the database. If
not specified then default is the name of the user that
owns the server process.
password :
Optional - The password of the user accessing the
database. If not specified then default is an empty
password.
PHP MYSQLI_SELECT_DB()
FUNCTION:
The mysqli_select_db() function sets the active
MySQL database.
This function returns TRUE on success, or FALSE
on failure.
mysqli_create_db() function attempts to create a
new database on the server associated with the
specified link identifier.
The mysqli_select_db() function is used to change
the default database for the connection.
Syntax of mysqli_select_db() function
mysqli_select_db(connection, name)
connection :Required. Specifies the MySQL
connection to use
name :Required. Specifies the database name
PHP MYSQL_QUERY() FUNCTION:
mysqli_query() Function performs a query against a
database.
This function selects a database and executes a query
on it.
If the optional link identifier isn't specified, the
function will try to find an open link to the MySQL
server and if no such link is found it'll try to create one
as if mysqli_connect() was called with no arguments.
The mysqli_query() sends a query to the currently
active database on the server that's associated with
the specified link identifier.
This function returns a resource identifier or FALSE if
the query was not executed correctly.
Syntax of mysqli_query() function
mysqli_query(database connection, Sql query);
Example:
$conn = mysqli_connect($host, $user, $pass,$dbname);
$sql = "CREATE DATABASE myDb";
mysqli_query($conn, $sql);
PHP MYSQL_RESULT() FUNCTION:
The mysqli_result() returns the contents of one
cell from a MySQL result set.
Syntax of mysqli_result() function
mysqli_result ($result , $row , $field);
result: The result resource that is being
evaluated. This result comes from a call to
mysqli_query().
row :The row number from the result that's
being retrieved. Row numbers start at 0.
field: The name or offset of the field being
retrieved.
MYSQL_CLOSE() Function
syntax
mysqli_close()
This function used to disconnect Mysql
database
EXAMPLE: CONNECT TO A
DBMS AND ACCESS DATABASE
<?php
$dbLocalhost = mysqli_connect("localhost", "root", "")
or die("Could not connect: " . mysqli_error());
mysqli_select_db(“student", $dbLocalhost)
or die("Could not find database: " . mysql_error());
echo "<h1>Connected To Database</h1>";
?>
• die() stops execution of script if the database connection
attempt failed.
• mysqli_error() returns an error message from the previous
MYSQL operation.
READING FROM A
DATABASE
• We can now send an SQL query to
the database to retrieve some data
records.
• FROM the
• stud table
SEPARATING THE DATABASE
CONNECTION
mysql_select_db("student2",
student2 $dbLocalhost)
or die("Could not find database: " . mysql_error());
}
?>
array = mysql_fetch_row(resourceRecords)
require_once("database2.php");
row
SEARCHING FOR MATCHING
RECORDS
SELECT * FROM stud WHERE sex=‘F’
returned.
SEARCHING FOR MATCHING
RECORDS
SELECT * FROM stud WHERE sex=‘F’
OR sex=‘M’
•The WHERE attribute specifies what to search for within the
database records.
• in this example, only records which have a title of ‘F’ or ‘M’
will be returned.
• we can also use AND and OR to formulate more
sophisticated conditions.
•
SEARCHING FOR MATCHING
RECORDS
SELECT * FROM stud WHERE sex=‘F’
AND Surname=‘Chala’ OR sex=‘M’
sophisticated conditions.
•
SORTING RECORDS
The ORDER BY attribute can be used to
sort the order in which records are
obtained.
SELECT * FROM stud ORDER BY Surname DESC
Example15-12.php
ACCESSING MULTIPLE
<?php
TABLES
// File: example15-13.php
require_once("database2.php");
echo "<p>Students:</p>";
while ($arrRecords = mysql_fetch_array($dbRecords)) {
echo "<p>" . $arrRecords["Id"] . " ";
echo $arrRecords[“sex"] . " ";
echo $arrRecords["Surname"] . " ";
echo $arrRecords["Firstname"] . "</p>";
}
//...continued...
Example15-13.php
ACCESSING MULTIPLE
TABLES
//continuation...
echo "<p>Products:</p>";
while ($arrRecords = mysql_fetch_array($dbRecords)) {
echo "<p>" . $arrRecords["Id"] . " ";
echo $arrRecords[“title"] . " ";
echo $arrRecords["Description"] . " ";
echo $arrRecords[“credithr"] . " ";
echo $arrRecords[“prerequest"] . "</p>";
}
?>
Example15-13.php
INSERTING RECORDS
How to create new database records
and insert them into a table?
INSERT INTO table (field1, field2,...) VALUES (‘value1’, ‘value2’,...)
Example15-15.php
INSERTING RECORDS
<?php
// File: example15-15.php
require_once("database2.php");
e.g.
Note: If you have a relational database, you should tidy-up the other tables, based on
their connection with the record you’ve deleted.
DELETING RECORDS
How to delete database records from
tables?
DELETE FROM table
Example15-18.php
UPDATING RECORDS
<?php
require_once("database2.php");
Another Example:
$dbCustRecords = mysql_query("UPDATE products SET Name='Beer
and Lager Glass' WHERE Name='Beer Glass'", $dbLocalhost)
Example15-19.php
COUNTING THE NUMBER OF
RECORDS
How to count the number of records
after running a query?
$dbProdRecords = mysql_query("SELECT * FROM products",
$dbLocalhost)
or die("Problem reading table: " . mysql_error());
$intProductCount = mysql_num_rows($dbProdRecords);
I Thank you