Website Development Working with MySQL
What you will achieve today! Connecting to mySql Creating tables in mySql Saving data on a server using mySql Getting data from the server using mySql
: Customer browser request service access page interpret set data present html return html get data databasescripting language web server Reminder of the general process today’s emphasis
Essential PHP functions
mysql_connect ([string hostname, string username [, string password]]]) Returns a positive MySQL link identifier on success, or an error message on failure. mysql_connect() establishes a connection to a MySQL server.
mysql_query (string query [, int link_identifier]) mysql_query() sends a query to the currently active database on the server that's associated with the specified link identifier. If link_identifier isn't specified, the last opened link is assumed. mysql_query() returns TRUE (non-zero) or FALSE to indicate whether or not the query succeeded. A return value of TRUE means that the query was legal and could be executed by the server. It does not indicate anything about the number of rows affected or returned. It is perfectly possible for a query to succeed but affect no rows or return no rows.
MyDBFunctions.php
array mysql_fetch_row (int result) Returns: An array that corresponds to the fetched row, or false if there are no more rows. mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0. Subsequent call to mysql_fetch_row() would return the next row in the result set, or false if there are no more rows.
mysql_num_rows (int result) mysql_num_rows() returns the number of rows in a result set. This command is only valid for SELECT statements. mysql_num_fields (int result) mysql_num_fields() returns the number of fields in a result set.
Now some SQL
CREATE TABLE ( col_name col_type,... )
CREATE TABLE DATEPERSON ( NAME TEXT, AGE INT, HOBBIES TEXT, TEXT, LIKES TEXT, HATES TEXT, POSTCODE TEXT, LOWERAGE INT, UPPERAGE INT )
test
DROP TABLE DATEPERSON DROP TABLE
test
INSERT INTO SET Col_name = value,...
INSERT INTO DATEPERSON SET NAME = “ROMEO", AGE = 14, HOBBIES = "maudling", = LIKES = "Mercutio", HATES = "Tibbalt", POSTCODE = "VE21 2BB", LOWERAGE = 14, UPPERAGE = 16
test
SELECT FROM WHERE,,...
SELECT * FROM DATEPERSON
test
Have a look at this Sample Database Application
What else? Forms for input – JavaScript validation? Forms for editing existing data Better error trapping An attractive interface – style sheets etc.
So what have we got now? Full database access Internet presentation In principle, most business applications can be served in this way