PHP-language, database- programming Teppo Räisänen Oulu University of Applied Sciences School of Business and Information Management
Idea of database programming End users do not use database from management console using SQL-statements Create UI (web-page, windows form, etc.) for end user and permorm database manipulation in application code (e. g. in PHP-script)
Database programming Database-driver Database-programming API (Application programming interface) Application
Basic steps of database programming 1.Open connection to database-server and Select database 2.Manipulate data 3.Close connection
Example: Read data from database
mysql_fetch_row Structure of the table in database Data (records) of the table in database mysql_fetch_row read one record at time and the return value is array containing information of all fields
Example: Insert data into database save.php newMessage.html
Opening connection Connection to database server must be opened before manipulating data Check always for error when opening database connections
Select database to use Servers may have several different databases
Manipulate data Reading information – mysql_query containing select sql-statement – mysql_fetch_row inside while loop Inserting, updating and deleting – mysql_query containing sql-statement that manipulates data
Close connection Connection to database (server) must be always closed after manipulatin data Open connections consumes resources from server