Main PDF 7 - PHP Application with MySQL
Main PDF 7 - PHP Application with MySQL
Emerging Technologies
MODULE 7
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[“MAX(tuition)”]
echo $fetch[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)”]
echo $fetch[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:
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);