Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class 3 MySQL Robert Mudge Reference:

Similar presentations


Presentation on theme: "Class 3 MySQL Robert Mudge Reference:"— Presentation transcript:

1 Class 3 MySQL Robert Mudge Reference: http://www.w3schools.com/php/

2 MySQL Configuration/Installation  Installation from scratch  my.ini file  Passwords (old and new)  Command line operation

3 MySQL Tools  Review of Tools MySQL Administrator MySQL Query Browser MySQL PhpMyAdmin dbDesigner4

4 MySQL Create database  Best way – MySQL Administrator Create user Assign permission  Control Panel – Creation Example Create user Create database Assign user to database Watch username prefix

5 Create table of users  Create a table using phpMyAdmin for users (suggest lower case for variables and UPPER case for SQL) username password realname date  Add users to table using phpMyAdmin

6 Query – Select Statement  Reference – W3Schools http://www.w3schools.com  Using MySQL Query browser SELECT * FROM users; SELECT * FROM users WHERE username=‘smith’;

7 PHP MySQL Access - connect // ---------------------------------------------------------------------------- function connect_to_database() { // $host = get_server(); $host = 'localhost'; $dbusername = 'bragn_admin'; $dbpassword = 'bragn_admin'; $database = 'bragnbracelets'; // setup the connection to the database @ $db = mysql_connect($host, $dbusername, $dbpassword); if (!$db) { echo ' Error : Could not connect to database '; echo "Host : $host "; echo ' '; echo ' Please try again or contact the system administrator '; echo ' '; logger(mysql_error()); exit; } @ $conn = mysql_select_db($database); if (!$conn) { logger(mysql_error()); exit; } return $db; }

8 PHP MySQL Access - connect function get_table_row($table, $key, $constraint) { $row = 0; // connect to db $db = connect_to_database(); $query = "select * from $table where $key = '$constraint'"; $result = mysql_query($query, $db); if ($result) { if (mysql_num_rows($result) > 1) { echo ' Warning : Query returns a non-unique result in enrollment. '; echo ' '; echo "Table : $table "; echo "Key : $key "; echo "Constraint: $constraint "; echo ' '; echo 'Showing the first result found '; echo 'Please examine the table data '; } $row = mysql_fetch_array($result); } else { echo " Error: No row found in $table for $key = $constraint "; } return $row; }

9 Homework 3  Create a role based database design. Create the database, populate with the given data and write PHP to access the database and perform the following queries.  Show all user names with the role of administrator.  Show what roles Susan Johnson has.  Show all the users, all their data  Show all the roles in ascending order of their level.  Turn in printed PHP source code and screen captures from your running program.

10 Users and Roles User Table - userid int(10) pkey - username char(20) - password char(20) Role Table - userid int(10) fkey - roleid int(10) fkey RoleTypes Table - roleid int(10) pkey - name char(20) - level int(10)

11 User Data  User Table Record 1  userid = ‘1000’  username = ‘jsmith’  password = ‘jsmith’  realname = ‘John Smith’ Record 2  userid = ‘1001’  username = ‘tjones’  password = ‘tjones’  realname = ‘Tom Jones’ Record 3  userid = ‘1002’  username = ‘sjohnson’  password = ‘sjohnsom’  realname = ‘Susan Johnson’

12 Role Types Data  RoleTypes Table Record 1  roleid = ‘100’  name = ‘guest’  level = 1 Record 2  roleid = ‘200’  name = ‘user’  level = 10 Record 3  roleid = ‘300’  name = ‘manager’  level = 100 Record 4  roleid = ‘400’  name = ‘administrator’  level = 1000

13 Role Data  RoleTypes Table Record 1  Userid = ‘1002’  roleid = ‘300’ Record 2  Userid = ‘1002’  roleid = ‘400’ Record 3  Userid = ‘1000’  roleid = ‘100’ Record 4  Userid = ‘1001’  roleid = ‘300’ Record 5  Userid = ‘1001’  roleid = ‘200’ Record 6  Userid = ‘1002’  roleid = ‘200’


Download ppt "Class 3 MySQL Robert Mudge Reference:"

Similar presentations


Ads by Google