php getting data from a mysql database. replacing xml as data source with mysql previously we...

22
PHP getting data from a MySQL database

Upload: cecil-stafford

Post on 17-Jan-2016

227 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 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

PHP getting data from a MySQL database

Page 2: 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

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.

Page 3: 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

Back to the sign-up-for-training form

Page 4: 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

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

Page 5: 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

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.

Page 6: 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

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.

}

Page 7: 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

Code to choose a database

$db = mysql_select_db($database, $dbc);

if($db==false)

{

die("problem with database.");

}

Page 8: 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

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.

Page 9: 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

Displaying the results in the drop-down list

while($row = mysql_fetch_array($result, MYSQL_NUM))

{

print "<option value =\"$row[0]\">$row[1]: $row[3] $row[4]</option>";

}

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.

Page 10: 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

Result in browser

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

Page 11: 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

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 "<option value =\"$row[0]\">$row[1]: $row[3] $row[4]</option>";}

}

Page 12: 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

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)

Page 13: 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

Result in browser

Page 14: 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

Return to phpmyadmin, click on your database and the Query

Page 15: 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

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

Page 16: 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

Choose SubjectTitle from SubjectMatter instead of SubjectID from Session

Also choose to add additional fields to the query.

Page 17: 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

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.

Page 18: 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

Update the query, then Submit the query

Page 19: 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

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

Page 20: 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

The phpmyadmin generated PHP code for the query

Copy the code over to your PHP script.

Page 21: 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

Join query code pasted into PHP script

Page 22: 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

Page with Subject titles instead of Subject IDs