Website Development & Management Getting Started with Databases Instructor: John Seydel, Ph.D. CIT Fall
Student Objectives Upon completion of this class meeting, you should be able to: Interact directly with MySQL to Start the database server Manage users Create databases Work with database tables Create Insert rows Display information Summarize basic database concepts Write basic PHP scripts that connect to MySQL databases and display database info
Data Management Fundamentals Review of the logical organization of data Field: characteristic of the entity involved (column) Record: collection of fields for a specific entity (row) File: collection of records for a given entity type (table) Database: collection of related files Typical data management operations Sort Query Update Purpose: raw data information (reports) Typically done via some interface Direct interaction with DBMS Applications program calls to DBMS Desktop applications Web applications (via PHP)
Review: The Installation Process for MySQL Generally follows procedure given in Meloni textbook Download mysql win.zip from course Handouts page (or from MySQL.com) into Downloads directory Uncompress into a default temporary directory and then open that directory Double-click on SETUP.EXE and accept all defaults as the installation wizard runs Test the installation Run c:\mysql\bin\winmysqladmin.exe Provide a username and password you’ll remember (generally, these won’t be used again, ever) Note the stoplight now on taskbar at bottom right MySQL’s database server now starts upon bootup Now, some followup...
Now: Completing the MySQL Setup Note the following clarifications, as well as corrections to errors in the textbook MySQL executables are located in c:\mysql\bin (not c:\mysqlbin) Initial startup for the MySQL database server Use the WinMySQLAdmin interface (not simply c:\mysql\bin) Starts the database server for the first time Creates a userID and password (typically not used again) From then on MySQL will start automatically at boot time You must create a database user (in DOS window) before working with any databases (see Chapter 11, pp ) mysql –u root -p USE mysql; GRANT ALL ON *.* TO IDENTIFIED BY “psw”; To ensure things are working, create a database CREATE DATABASE testDB;(do not need to run mysqladmin) When done with MySQL QUIT;
How MySQL Stores the Data Each table (i.e., relation) involves three files: TableName.frm TableName.MYD (where the actual data are) TableName.MYI The files for all the tables belonging to a given database are stored in a directory with the same name as the database All database directories are stored in the data directory within c:\mysql Note: upon completion of the MySQL installation process, there are two databases mysql Stores system info (users, permissions, etc.) Do not mess with this test (this is initially empty, and anyone can access it without login)
Working with MySQL Get started mysql -u jojobeans –p USE testDB; SHOW TABLES; Using tables CREATE TABLE tblTest (testID int, testNote text); EXPLAIN tblTest; SELECT * FROM tblTest; INSERT INTO tblTest VALUES (123,’First entry’); INSERT INTO tblTest VALUES (456,’Second entry’); SELECT * FROM tblTest; QUIT;
Using PHP to Interface with MySQL Making sure MySQL works with PHP In php.ini: uncomment extension=php_mysql.dll Copy c:\php\ext\php_mysql.dll into c:\windows New Always starts with the creation of a connection Requires opening a database Generally involves generating a recordset (aka, result set) Processing records typically involves some sort of looping construct One repetition per record Must be controlled by some sort of counter: foreach(), while(), and for() Makes use of several important functions...
PHP Functions: Processing Basic Queries mysql_connect() Arguments (string): server, userID, password Returns a string (connection information) mysql_select_db() Arguments (string): database to be used, connection Returns a boolean value mysql_query() Arguments (string): SQL query, connection Returns a recordset (an array of data) mysql_num_rows() Argument (array): recordset Returns an integer (number of records) mysql_fetch_array() Arguments (string): recordset Returns a row from the recordset
Demonstration: Listing FKAuto’s Inventory The database: fkauto One table at present: UsedCars Write a script to access the data and create a web page displaying a bullet list with make, model, year, mileage, and price for each carscript
What We’ve Seen New PHP functions mysql_connect() mysql_select_db() mysql_query() mysql_num_rows() mysql_fetch_array() A little SQL Used directly with MySQL Incorporated into PHP script The for() construct How to use PHP to connect to MySQL, open a database, create a recordset, and process that recordset How/where MySQL files are stored
The FKAuto Database Files For use on your own computer: Download these Available in the Documents directory (not public_html) of the cit3353 account on SuSE1 Recall the password? All three files for the UsedCars database are in a directory named fkauto For use on SuSE1: don’t worry about where they are, as they are available to your scripts However, the “owner” is user cit3353 Thus use the appropriate values for user and password