Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHP getting data from a MySQL database. Replacing XML as data source with MySQL Previously we obtained the data about the training session from an XML.

Similar presentations


Presentation on theme: "PHP getting data from a MySQL database. Replacing XML as data source with MySQL Previously we obtained the data about the training session from an XML."— Presentation transcript:

1 PHP getting data from a MySQL database

2 Replacing XML as data source with MySQL Previously we obtained the data about the training session from an XML file. Now we would like to get the information out of the MySQL database. PHP must connect to the database, perform a SQL select statement of the appropriate table or tables and then parse the query result and display the information.

3 Back to the sign-up-for-training form

4 Code to load database information into select element (drop-down list)

5 Information needed to connect to database $host = "localhost"; //if PHP server and mySQL server same $user="blum2"; $password = “tb4db"; $database = "blum2"; Even though the client will never see this code, it is standard procedure to place username and password data eventually gets placed in another file.

6 Code to connect to MySQL $dbc = mysql_connect($host,$user,$password); if($dbc==false) { die("Problem connecting to MySQL."); //The die() function prints a message //and exits the current script. }

7 Code to choose a database $db = mysql_select_db($database, $dbc); if($db==false) { die("problem with database."); }

8 Code to ask for data from Session table $sql = "SELECT SessionID, SubjectID, LocationID, Date, Time FROM Session"; //$sql = "SELECT * FROM Session"; $result = mysql_query($sql,$dbc); The $sql variable above will hold a string corresponding to a SQL query requesting data from the Session table. The last statement performs the SQL query and places the results in a variable result.

9 Displaying the results in the drop-down list while($row = mysql_fetch_array($result, MYSQL_NUM)) { print " $row[1]: $row[3] $row[4] "; } This code loops through the records resulting from the query. For each record, row becomes an array of the fields of that record. The order of fields in row matches the order in the SQL statement.

10 Result in browser We need to re-insert our code that only displayed present and future training sessions. It would be preferable if the

11 Code to display only future training while($row = mysql_fetch_array($result, MYSQL_NUM)) { list($year, $month, $day) = split('[/.-]', $row[3]); //note info from database arranges it year, month, day //month already a number $trainingTime = mktime(0,0,0,$month, $day+1, $year); $now = time(); if($trainingTime >= $now) { print " $row[1]: $row[3] $row[4] "; }

12 The date format has changed – $year comes first and $month is a number list($year, $month, $day) = split('[/.-]', $row[3]); //note info from database arranges it year, month, day //month already a number //same as before $trainingTime = mktime(0,0,0,$month, $day+1, $year); $now = time(); if($trainingTime >= $now)

13 Result in browser

14 Return to phpmyadmin, click on your database and the Query

15 Choose the tables to be used in the query, then start choosing the fields

16 Choose SubjectTitle from SubjectMatter instead of SubjectID from Session Also choose to add additional fields to the query.

17 Make a “natural join” between the Session and SubjectMatter tables Note that we have chosen to display all of the fields except the last, and on the last field SubjectMatter.SubjectID we impose the constraint that its value equal that from Session.SubjectID -- that is we want the records from the two tables to have the proper relationship. The character ` seen above is above the Tab on the upper left of the keyboard.

18 Update the query, then Submit the query

19 Results of Query Be careful to use the phpmyadmin’s navigation, such as Edit, and not the browser’s back button if you need to fix anything. If happy click Create PHP Code Query results

20 The phpmyadmin generated PHP code for the query Copy the code over to your PHP script.

21 Join query code pasted into PHP script

22 Page with Subject titles instead of Subject IDs


Download ppt "PHP getting data from a MySQL database. Replacing XML as data source with MySQL Previously we obtained the data about the training session from an XML."

Similar presentations


Ads by Google