0% found this document useful (0 votes)
38 views

Introduction To Output Formatting

This document discusses output formatting in PHP and using tables to display data retrieved from a MySQL database. It covers: 1. Using print and table tags (<table>, <tr>, <td>) to output HTML and display data in a table. 2. Modifying a PHP script to connect to a database, select specific columns from a table using SQL, assign the results to variables, and print the variables in table cells. 3. Using a while loop and mysql_fetch_array() to retrieve and display multiple rows of data from a database query result.

Uploaded by

Raymond Ramirez
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Introduction To Output Formatting

This document discusses output formatting in PHP and using tables to display data retrieved from a MySQL database. It covers: 1. Using print and table tags (<table>, <tr>, <td>) to output HTML and display data in a table. 2. Modifying a PHP script to connect to a database, select specific columns from a table using SQL, assign the results to variables, and print the variables in table cells. 3. Using a while loop and mysql_fetch_array() to retrieve and display multiple rows of data from a database query result.

Uploaded by

Raymond Ramirez
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Introduction to output

formatting
Web Systems and Technologies
basicSelection.php
• This script did not format the data very well
• We should use tables to display data
• Table tags
• <table> </table>
• <tr> </tr> a row
• <td> </td> a data cell
• We can also use print to output HTML
print
• print("<table width='500' border='1'>");

• This command will create a table with a width of 500 pixels and a
border
• Make this the first command in the modified basicSelection.php
• (after the if ($connectionSuccess == 1) { )
Be more selective
Use SQL to retrieve the title and author

$result = mysql_query("SELECT title, author


FROM books");
while ($row = mysql_fetch_array($result)) {
Assign variables
Assign the results of the query to variables

$Title = $row['title'];
$Author = $row['author'];
Print the results in table cells
print("<tr>");
print("<td>$Title </td><td> $Author</td>");
print("</tr>");
Complete code
if ($connectionSuccess == 1) {
print("<table width='500' border='1'>");
$result = mysql_query("SELECT title, author FROM books");
while ($row = mysql_fetch_array($result)) {
$Title = $row['title'];
$Author = $row['author'];
print("<tr>");
print("<td>$Title </td><td> $Author</td>");
print("</tr>");
}
mysql_free_results($result); //free up server memory
}
Appearance
PHP: Selecting Pieces
of Data
Web Systems and Technologies
PHP includes
• To avoid repeating code we can use an
include to add an existing script to a new
script
• Take the connection.php script and cut the
$hostname, $username, $password and
$databaseName and paste into a new file
called basicSelection.php
basicSelection file
• <?php
• /*
• File: basicSelection.php
• By: Bob
• Date: 2012-03-04

• This script illustrates the SQL SELECT command


• */
• $hostname = “localhost";
• $username = “MISNumber";
• $password = "*******";
• $databaseName = “MISNumber_library";
• include("connection.php"); //adds the code from the connection.php script
• ?>
Remove the success messages
• We only need to know if the connection has failed
• We do not want to execute any further code it has failed
• We do this by setting a Boolean variable to indicate success in the
connection.php script and test it in the basicSelection script
Modified connection script
<?php
error_reporting(0);
$connectionSuccess = 0; // Initialise the Boolean variable
$dbConnected = mysql_connect($hostname, $username, $password);
$dbSelected = mysql_select_db ($databaseName,$dbConnected);
if ($dbConnected) {
if ($dbSelected) {
$connectionSuccess = 1; //Set to 1 if connections OK
} else {
echo "DB Connected FAILED<br /><br />";
}
} else {
echo "MySQL connection FAILED<br /><br />";
}
?>
basicSelection.php
<?php
$hostname = “localhost";
$username = “MISNumber";
$password = "YourPassword";
$databaseName = “MISNumber_library";
include("connection.php");
echo $connectionSuccess;
?>
We now have a success
variable
When the script is viewed we should just see a 1 or an
error message and a 0

We can now test the variable and proceed if it is 1 or exit if


it is 0
Testing the variable
<?php
$hostname = “localhost";
$username = “MISNumber";
$password = "YourPassword";
$databaseName = “MISNumber_library";
include("connection.php");
if ($connectionSuccess == 1) {
echo "Your code here";
}
?>
mysql_query
The mysql_query command is used to run an SQL query
The syntax is:
$result = mysql_query("SQL statement");
Eg:

$result = mysql_query("SELECT * FROM books");


$result contains a MySQL resource ID
What use is that?
To make use of it we need another function
mysql_fetch_array
The mysql_fetch_array command is used to recover a row
from the resource ID, which is the result of the query
The syntax is:
$row = mysql_fetch_array($result);

To see what we have retrieved we can use a print_r($row)


which will display the row contents on the screen
Displaying a row
<?php
// put your comment block and connection code here
if ($connectionSuccess == 1) {
$result = mysql_query("SELECT * FROM books");
$row = mysql_fetch_array($result);
print_r($row);
}
?>
The result
Array ( [0] => 1 [bookID] => 1 [1] => ISBN [isbn]
=> ISBN [2] => Title [title] => Title [3] =>
Author [author] => Author [4] => Publisher
[publisher] => Publisher [5] => Publisher's
Web Site [publisherswebsite] => Publisher's
Web Site [6] => Genre [genre] => Genre )

We can access the individual entries either by


index number [1] or column name
Displaying selected columns
if ($connectionSuccess == 1) {
$result = mysql_query("SELECT * FROM
books");
$row = mysql_fetch_array($result);
echo $row[2];
echo "<br/>";
echo $row["author"];
}
The result
Title
Author

But this only shows one row


Use of a while loop
if ($connectionSuccess == 1) {
$result = mysql_query("SELECT * FROM books");
while ($row = mysql_fetch_array($result)) {
echo $row[2];
echo "<br/>";
echo $row["author"];
echo "<br/><br/>";
}
mysql_free_results($result); //free up server memory
}
The result
Title
Author

"The Definitive Guide to MySQL 5, Third Edition (Definitive Guide)“


Michael Kofler

PHP Cookbook (Cookbooks (O'Reilly))


David Sklar

Harry Potter and the Philosopher's Stone (Book 1)


J.K. Rowling

And so it goes
Escaping PHP for HTML
if ($connectionSuccess == 1) {
$result = mysql_query("SELECT * FROM books");
while ($row = mysql_fetch_array($result)) {
echo $row[2];
echo "<br/>";
echo $row["author"];
?>
<br/><br/>
<?php //This restores PHP after escaping it for the HTML
}
mysql_free_results($result); //free up server memory
}
Exercises
• Try:
• retrieving different columns
• formatting the results using HTML and CSS

You might also like