Website Development Working with MySQL. What you will achieve today! Connecting to mySql Creating...

Post on 22-Dec-2015

215 views 2 download

Transcript of Website Development Working with MySQL. What you will achieve today! Connecting to mySql Creating...

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

interpretset data

present html

return html

get data

get data

databasescripting language

web server

Reminder of the general process

today’semphasis

Essential PHP functions

http://www.php.net/manual/en/ref.mysql.php

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 <tablename> (col_name col_type,col_name col_type,...

)

http://www.mysql.com/doc/C/R/CREATE_TABLE.html

CREATE TABLE DATEPERSON (NAME TEXT,AGE INT,HOBBIES TEXT,EMAIL TEXT,LIKES TEXT,HATES TEXT,POSTCODE TEXT,LOWERAGE INT,UPPERAGE INT

)

DROP TABLE DATEPERSON

DROP TABLE <tablename>

http://www.mysql.com/doc/D/R/DROP_TABLE.html

test

INSERT INTO <tablename> SETCol_name = value,Col_name = value,...

http://www.mysql.com/doc/I/N/INSERT.html

INSERT INTO DATEPERSON SETNAME = “ROMEO",AGE = 14,HOBBIES = "maudling",EMAIL = "romeo@montague.com",LIKES = "Mercutio",HATES = "Tibbalt",POSTCODE = "VE21 2BB",LOWERAGE = 14,UPPERAGE = 16

test

SELECT <column names>FROM <tablename>WHERE <col_name comparison value>,

<col_name comparison value>,...

http://www.mysql.com/doc/S/E/SELECT.html

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 accessInternet presentationIn principle, most business applications can be served in this way