Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Main PDF 7 - PHP Application with MySQL

This document covers PHP application development with MySQL, focusing on group functions like SUM, MAX, and MIN, as well as file upload handling using the $_FILES array. It explains how to retrieve data from MySQL, validate files, and perform file operations such as reading, writing, and appending. Additionally, it provides examples of using various MySQL and file handling functions in PHP.

Uploaded by

Charles Uy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Main PDF 7 - PHP Application with MySQL

This document covers PHP application development with MySQL, focusing on group functions like SUM, MAX, and MIN, as well as file upload handling using the $_FILES array. It explains how to retrieve data from MySQL, validate files, and perform file operations such as reading, writing, and appending. Additionally, it provides examples of using various MySQL and file handling functions in PHP.

Uploaded by

Charles Uy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Applications Development and

Emerging Technologies
MODULE 7

PHP Application with MySQL


Using Group Functions
• Example: SUM
$query = mysql_query(“SELECT SUM(tuition) FROM tblstudent”);
$fetch = mysql_fetch_array($query)

echo $fetch[“SUM(tuition)”]
Using Group Functions
$query = mysql_query(“SELECT SUM(tuition) FROM tblstudent”);
$fetch = mysql_fetch_array($query)

echo $fetch[0]
Using Group Functions
$query = mysql_query(“SELECT SUM(tuition) FROM tblstudent”);
$fetch = mysql_result($query,0);

echo $fetch;
mysql_result()
• mysql_result() retrieves the contents of one cell from a
MySQL result set
• Syntax:
mysql_result($result_query, row, field)
$result_query - The result resource that is being
evaluated. This result comes from a call to mysql_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.
Example: mysql_result()
$query = mysql_query(“SELECT name FROM tblstudent”);
$fetch = mysql_result($query, 2) //gets the 3rd row of name

echo $fetch; // prints the 3rd row of name

$query = mysql_query(“SELECT CONCAT(name,’ ‘,gender) as


Output FROM tblstudent WHERE id=‘200812345’”);
$fetch = mysql_result($query, 0, “Output”)

echo $fetch; // prints the name & gender of the query


Using Group Functions
• Example: MAX
$query = mysql_query(“SELECT MAX(tuition) FROM tblstudent”);
$fetch = mysql_fetch_array($query)

echo $fetch[“MAX(tuition)”]

$query = mysql_query(“SELECT MAX(tuition) FROM tblstudent”);


$fetch = mysql_fetch_array($query)

echo $fetch[0]

$query = mysql_query(“SELECT MAX(tuition) FROM tblstudent”);


$fetch = mysql_result($query,0);

echo $fetch;
Using Group Functions
• Example: MIN
$query = mysql_query(“SELECT MIN(tuition) FROM tblstudent”);
$fetch = mysql_fetch_array($query)

echo $fetch[“MIN(tuition)”]

$query = mysql_query(“SELECT MIN(tuition) FROM tblstudent”);


$fetch = mysql_fetch_array($query)

echo $fetch[0]

$query = mysql_query(“SELECT MIN(tuition) FROM tblstudent”);


$fetch = mysql_result($query,0);

echo $fetch;
mysql_affected_rows()
• mysql_affected_rows() get the number of affected rows
by the last SELECT, INSERT, UPDATE, REPLACE or DELETE query.
• Syntax: mysql_affected_rows()
• Example:

$query = mysql_query(“SELECT id FROM tblstudent WHERE id LIKE


‘%2009%’”);

echo mysql_affected_rows(); // will return the number of rows


matched from the query
mysql_num_rows()
• mysql_num_rows() retrieves the number of rows from a
result set. This command is only valid for statements like SELECT
or SHOW that return an actual result set.
• Syntax: mysql_num_rows()
• Example:

$query = mysql_query(“SELECT id FROM tblstudent WHERE id LIKE


‘%2009%’”);

echo mysql_num_rows(); // will return the number of rows


matched from the query
More MySQL Functions

Visit
➢http://www.php.net/manual/en/ref.mysql.php
➢http://w3schools.com/php/php_ref_mysql.asp
Working with File Uploads
$_FILES
• Using the global PHP $_FILES array you can upload files from a
client computer to the remote server.
• Syntax: $_FILES[“file”][“index”]
file – the name of the field in the form
index – specifies the parameter to be processed by $_FILES
Index Description
name The name of the uploaded file
type The type of the uploaded file
size The size of the 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
Saving File Permanently
• To save permanently the uploaded file, use the
move_uploaded_file() function. This function returns
TRUE on success, or FALSE on failure.
• Syntax: move_upload_file(file, new location);
• Example:
$destination = ‘uploadedFile’; // folder in your htdocs
$tmp_name = $_FILES[“fileUp”][“tmp_name”];
$filename = $_FILES[“fileUp”][“name”];

move_uploaded_file($tmp_name, “$destination/$filename”);
Example
Example
Validating Files
Function Description Example
file_exists() Checking for file file_exists(“test.txt”)
existence
is_file() Checking if it is a file is_file(“test.txt”)
is_dir() Checking if it is a is_dir(“/tmp”)
directory
is_readable() is_readable(“test.txt”)
is_writable() Checking the file status is_writable(“test.txt”)
is_executable() is_executable(“test.txt”)
filesize() Determining file size filesize(“test.txt”)
Example
<?php
$file = "test.txt";
outputFileTestInfo($file);

function outputFileTestInfo($f) {
if (!file_exists($f)) {
echo "<p>$f does not exist</p>";
return;
}
echo "<p>$f is ".(is_file($f)?"":"not ")."a file</p>";
echo "<p>$f is ".(is_dir($f)?"":"not ")."a directory</p>";
echo "<p>$f is ".(is_readable($f)?"":"not ")."readable</p>";
echo "<p>$f is ".(is_writable($f)?"":"not ")."writable</p>";
echo "<p>$f is ".(is_executable($f)?"":"not ")."executable</p>";
echo "<p>$f is ".(filesize($f))." bytes</p>";
}
?>
touch() - attempts to create an empty file. If the file
already exists, its contents won't be disturbed, but
the modification date will be updated to reflect the
time at which the function executed.
Example: touch(“myfile.txt”);
unlink() – a function used to remove an existing
file.
Example: unlink(“myfile.txt”);
fopen() – is used to open a file for reading, writing or
appending content
Syntax: fopen(“file”, “mode”);
Example:
• fopen(“test.txt”, “r”) – for reading
• fopen(“test.txt”, “w”) – for writing
• fopen(“test.txt”, “a”) – for appending
fclose() – to close a file
Reading Lines from a File
• fgets() – to read a line from a file
• feof() – to tell the last line of a file
$filename = "test.txt";
$fp = fopen($filename, "r") or die("Couldn't open $filename");
while (!feof($fp)) {
$line = fgets($fp, 1024);
echo "$line<br>";
}
Reading Lines from a File
• 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
• The fwrite() function accepts a file resource and a
string, and then writes the string to the file
• The 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>";
$fp = fopen($filename, "a") or die("Couldn't open $filename");
fputs($fp, "And another thing\n");
fclose($fp);

You might also like