Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP: Database Basic Selection FdSc Module 109

Similar presentations


Presentation on theme: "PHP: Database Basic Selection FdSc Module 109"— Presentation transcript:

1 PHP: Database Basic Selection FdSc Module 109
Server side scripting and Database design 2011

2 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

3 basicSelection file <?php /* File: basicSelection.php By: Bob
Date: 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 ?>

4 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

5 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 />"; } echo "MySQL connection FAILED<br /><br />"; ?>

6 basicSelection.php <?php $hostname = “localhost"; $username = “MISNumber"; $password = "YourPassword"; $databaseName = “MISNumber_library"; include("connection.php"); echo $connectionSuccess; ?>

7 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

8 Testing the variable <?php $hostname = “localhost"; $username = “MISNumber"; $password = "YourPassword"; $databaseName = “MISNumber_library"; include("connection.php"); if ($connectionSuccess == 1) { echo "Your code here"; } ?>

9 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

10 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

11 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); } ?>

12 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

13 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"]; }

14 The result Title Author But this only shows one row

15 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

16 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

17 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

18 Exercises Try: retrieving different columns
formatting the results using HTML and CSS


Download ppt "PHP: Database Basic Selection FdSc Module 109"

Similar presentations


Ads by Google