Professional Documents
Culture Documents
Applications Development and Emerging Technologies Reviewer
Applications Development and Emerging Technologies Reviewer
TECHNOLOGIES REVIEWER
MODULE 7 OUTPUT:
PHP Application with MySQL
$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
Example
$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
<?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()
fgetc()
$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>"; }
fwrite()
fputs()
$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>";