Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

APPLICATIONS DEVELOPMENT AND EMERGING

TECHNOLOGIES REVIEWER
MODULE 7 OUTPUT:
PHP Application with MySQL

Using Group Functions


• Example: SUM

$query = mysql_query(“SELECT
SUM(tuition) FROM tblstudent”); mysql_result()
$fetch = mysql_fetch_array($query) • mysql_result() retrieves the contents of
one cell from a MySQL result set
echo $fetch[“SUM(tuition)”]
• Syntax:
OUTPUT:
mysql_result(result_query, row, field)
SUM(tuition)
$result_query

- The result resource that is being


evaluated. This result comes from a call to
mysql_query().

$query = mysql_query(“SELECT row


SUM(tuition) FROM tblstudent”);
$fetch = mysql_fetch_array($query) - The row number from the result that's
being retrieved. Row numbers start at 0.
echo $fetch[0]
field
OUTPUT:
- The name or offset of the field being
SUM(tuition) retrieved.

Example

$query = mysql_query(“SELECT name


FROM tblstudent”);
$fetch = mysql_result($query, 2) //gets
$query = mysql_query(“SELECT the 3rd row of name
SUM(tuition) FROM tblstudent”);
$fetch = mysql_result($query,0); echo $fetch; // prints the 3rd row of
name
echo $fetch;
$query = mysql_query(“SELECT
CONCAT(name,’ ‘,gender) as Output
FROM tblstudent WHERE
id=‘200812345’”);
$fetch = mysql_result($query, 0, INSERT, UPDATE, REPLACE or
“Output”) DELETE query.

echo $fetch; // prints the name & • Syntax: mysql_affected_rows()


gender of the query.
• Example:
• Example: MAX
$query = mysql_query(“SELECT id
$query = mysql_query(“SELECT FROM tblstudent WHERE id LIKE
MAX(tuition) FROM tblstudent”); ‘%2009%’”);
$fetch = mysql_fetch_array($query)
echo mysql_affected_rows(); // will
echo $fetch[“MAX(tuition)”] return the number of rows matched
from the query
$query = mysql_query(“SELECT
MAX(tuition) FROM tblstudent”); mysql_num_rows()
$fetch = mysql_fetch_array($query) • mysql_num_rows() retrieves the
number of rows from a result set. This
echo $fetch[0] command is only valid for statements like
SELECT or SHOW that return an actual
$query = mysql_query(“SELECT result set.
MAX(tuition) FROM tblstudent”);
$fetch = mysql_result($query,0); • Syntax: mysql_num_rows()

echo $fetch; • Example:

• Example: MIN $query = mysql_query(“SELECT id


FROM tblstudent WHERE id LIKE
$query = mysql_query(“SELECT ‘%2009%’”);
MIN(tuition) FROM tblstudent”);
$fetch = mysql_fetch_array($query) echo mysql_num_rows(); // will return
the number of rows matched from the
echo $fetch[“MIN(tuition)”] query

$query = mysql_query(“SELECT
More MySQL Functions
MIN(tuition) FROM tblstudent”);
Visit
$fetch = mysql_fetch_array($query)
➢http://www.php.net/manual/en/ref.mysql.
echo $fetch[0] php
➢http://w3schools.com/php/php_ref_mysq
$query = mysql_query(“SELECT l.asp
MIN(tuition) FROM tblstudent”);
$fetch = mysql_result($query,0); WORKING WITH FILE UPLOADS

echo $fetch;
$_FILES
mysql_affected_rows()
• Using the global PHP $_FILES array you
• mysql_affected_rows() get the number
can upload files from a client computer to
of affected rows by the last SELECT,
the remote server.
• Syntax: $_FILES[“file”][“index”] This function returns TRUE on success, or
FALSE on failure.
file
• Syntax: move_upload_file(file, new
– the name of the field in the form location);

• Example:
index
$destination = ‘uploadedFile’; // folder
– specifies the parameter to be processed in your htdocs
by $_FILES $tmp_name =
$_FILES[“fileUp”][“tmp_name”];
Index Description $filename = $_FILES[“fileUp”][“name”];
name The name of the
uploaded file move_uploaded_file($tmp_name,
type The type of the “$destination/$filename”);
uploaded file
size The size of the Example
uploaded file
tmp_name The name of the
temporary copy of
the file stored on
the server
error the error code
resulting from the
file upload

Create the upload form.

Sample Output Validating Files

Function Descrip Example


tion
file_exist() Checkin file_exists(“test.t
g for file xt”)
existenc
e
is_file() Checkin is_file(“test.txt”)
Saving File Permanently g if it is
• To save permanently the uploaded file, a file
use the move_uploaded_file() function.
is_dir() Checkin is_dir(“/tmp”)
g if it is Example: touch(“myfile.txt”);
a
directory
is_readabl is_readable(“tes
e() t.txt”) unlink()
Checkin
is_writable is_writable(“test.
g the file
() txt”)
status – a function used to remove an existing
is_execut is_executable(“t
file.
able() est.txt”)
filesize() Determi filesize(“test.txt”
ning file ) Example: unlink(“myfile.txt”);
size
OPENING A FILE FOR WRITING,
Example READING, OR APPENDING

<?php fopen()
$file = "test.txt";
outputFileTestInfo($file); – is used to open a file for reading, writing
or appending content
function outputFileTestInfo($f) {
if (!file_exists($f)) { Syntax: fopen(“file”, “mode”);
echo "<p>$f does not exist</p>";
return; Example:
}
echo "<p>$f is ".(is_file($f)?"":"not fopen(“test.txt”, “r”)
")."a file</p>";
echo "<p>$f is ".(is_dir($f)?"":"not ")."a – for reading
directory</p>";
echo "<p>$f is fopen(“test.txt”, “w”)
".(is_readable($f)?"":"not
")."readable</p>"; – for writing
echo "<p>$f is ".(is_writable($f)?"":"not
")."writable</p>"; fopen(“test.txt”, “a”)
echo "<p>$f is
".(is_executable($f)?"":"not – for appending
")."executable</p>";
echo "<p>$f is ".(filesize($f))." fclose()
bytes</p>";
} – to close a file
?>
READING LINES FROM A FILE
CREATING AND DELETING FILES

fgets()
touch()
– to read a line from a file
- attempts to create an empty file. If the file
already exists, its contents won't be feof()
disturbed, but the modification date will be
updated to reflect the time at which the – to tell the last line of a file
function executed.
$filename = "test.txt"; $fp = fopen($filename, "a") or
$fp = fopen($filename, "r") or die("Couldn't open $filename");
die("Couldn't open $filename"); fputs($fp, "And another thing\n");
while (!feof($fp)) { fclose($fp);
$line = fgets($fp, 1024);
echo "$line<br>";
}

fread()

– to read a specified set of character in a


file

fgetc()

– to read a file per character

$filename = "test.txt";
$fp = fopen($filename, "r") or
die("Couldn't open $filename");
while (!feof($fp)) {
$chunk = fread($fp, 8);
echo "$chunk<br>"; }

$filename = "test.txt";
$fp = fopen($filename, "r") or
die("Couldn't open $filename");
while (!feof($fp)) {
$char = fgetc($fp);
echo "$char<br>"; }

WRITING AND APPENDING TO A FILE

fwrite()

- function accepts a file resource and a


string, and then writes the string to the file

fputs()

- function works in exactly the same way.

$filename = "test.txt";
echo "<p>Writing to $filename ... </p>";
$fp = fopen($filename, "w") or
die("Couldn't open $filename");
fwrite($fp, "Hello world\n");
fclose($fp);
echo "<p>Appending to $filename
...</p>";

You might also like