SlideShare a Scribd company logo
PHP AND MYSQL WEB DEVELOPMENT
• WHEN YOU INSTALL PHP, YOU CAN SELECT FROM A
NUMBER OF EXTENSIONS.
• THE MYSQL SUPPORT IN PHP CONSISTS OF A NUMBER OF
FUNCTIONS YOU CAN CALL TO INTERACT WITH MYSQL,
AND HERE ARE SOME OF THEM:
PHP and MySQL Web Development
tMyn 1
PHP and MySQL Web Development
tMyn 2
The mysql_connect() function opens a non-persistent MySQL connection.
This function returns the connection on success, or FALSE and
an error on failure.
Syntax
mysql_connect(server,user,pwd,newlink,clientflag)
Parameter Description
server Optional. Specifies the server to connect to (can also
include a port number, e.g. "hostname:port" or a path to a
local socket for the localhost).
Default value is "localhost:3306"
user Optional. Specifies the username to log in with. Default
value is the name of the user that owns the server process
pwd Optional. Specifies the password to log in with. Default is ""
PHP and MySQL Web Development
tMyn 3
The mysql_select_db() function sets the active MySQL database.
This function returns TRUE on success, or FALSE on failure.
Syntax
mysql_select_db(database,connection)
Parameter Description
database Required. Specifies the database to select.
connection Optional. Specifies the MySQL connection. If not specified,
the last connection opened by mysql_connect() or
mysql_pconnect() is used.
PHP and MySQL Web Development
tMyn 4
The mysql_query() function executes a query on a MySQL database.
This function returns the query handle for SELECT queries,
TRUE/FALSE for other queries, or FALSE on failure.
Syntax
mysql_query(query,connection)
Parameter Description
query Required. Specifies the SQL query to send (should not
end with a semicolon).
connection Optional. Specifies the MySQL connection. If not specified,
the last connection opened by mysql_connect() or
mysql_pconnect() is used.
PHP and MySQL Web Development
tMyn 5
The mysql_fetch_array() function returns a row from a recordset as an
associative array and/or a numeric array. This function gets a row from
the mysql_query() function and returns an array on success, or FALSE
on failure or when there are no more rows.
Syntax
mysql_fetch_array(data,array_type)
Parameter Description
data Required. Specifies which data pointer to use. The data
pointer is the result from the mysql_query() function
array_type Optional. Specifies what kind of array to return.
Possible values:
MYSQL_ASSOC - Associative array
MYSQL_NUM - Numeric array
MYSQL_BOTH - Default. Both associative and numeric array
PHP and MySQL Web Development
tMyn 6
The mysql_fetch_object() function returns a row from a recordset as an object.
This function gets a row from the mysql_query() function and returns an object
on success, or FALSE on failure or when there are no more rows.
Syntax
mysql_fetch_object(data)
Parameter Description
data Required. Specifies which data pointer to use. The data
pointer is the result from the mysql_query() function
Tips and Notes
Note: Each subsequent call to mysql_fetch_object() returns the
next row in the recordset.
PHP and MySQL Web Development
tMyn 7
The mysql_affected_rows() function returns the number of affected rows
in the previous MySQL operation. This function returns the number of
affected rows on success, or -1 if the last operation failed.
Syntax
mysql_affected_rows(connection)
Parameter Description
connection Optional. Specifies the MySQL connection. If not specified,
the last connection opened by mysql_connect() or
mysql_pconnect() is used.
PHP and MySQL Web Development
tMyn 8
The mysql_num_rows() function returns the number of rows in a recordset.
This function returns FALSE on failure.
Syntax
mysql_num_rows(data)
Parameter Description
data Required. Specifies which data pointer to use.
The data pointer is the result from the mysql_query() function
PHP and MySQL Web Development
tMyn 9
The mysql_result() function returns the value of a field in a recordset.
This function returns the field value on success, or FALSE on failure.
Syntax
mysql_result(data,row,field)
Parameter Description
data Required. Specifies which result handle to use. The data
pointer is the return from the mysql_query() function
row Required. Specifies which row number to get.
Row numbers start at 0
PHP and MySQL Web Development
tMyn 10
field Optional. Specifies which field to get. Can be field offset,
field name or table.fieldname. If this parameter is not defined
mysql_result() gets the first field from the specified row.
Tips and Notes
This function is slower than mysql_fetch_row(),
mysql_fetch_array(), mysql_fetch_assoc() and
mysql_fetch_object().
PHP and MySQL Web Development
tMyn 11
The mysql_error() function returns the error description of the last
MySQL operation. This function returns an empty string ("") if no error occurs.
Syntax
mysql_error(connection)
Parameter Description
connection Optional. Specifies the MySQL connection. If not specified,
the last connection opened by mysql_connect() or
mysql_pconnect() is used.
PHP and MySQL Web Development
tMyn 12
The mysql_close() function closes a non-persistent MySQL connection.
This function returns TRUE on success, or FALSE on failure.
Syntax
mysql_close(connection)
Parameter Description
connection Optional. Specifies the MySQL connection to close.
If not specified, the last connection opened by
mysql_connect() is used.
PHP and MySQL Web Development
tMyn 13
die — Equivalent to exit()
Description
This language construct is equivalent to exit().
PHP and MySQL Web Development
tMyn 14
exit — Output a message and terminate the current script
Description
void exit ([ string $status ] )
void exit ( int $status )
Terminates execution of the script.
Parameters
status
If status is a string, this function prints the status just before exiting.
If status is an integer, that value will also be used as the exit status.
Exit statuses should be in the range 0 to 254, the exit status 255 is
reserved by PHP and shall not be used. The status 0 is used to
terminate the program successfully.
• A TYPICAL WEB DATABASE TRANSACTION CONSISTS OF THE
FOLLOWING STAGES, WHICH ARE NUMBERED IN THE FIGURE 1:
1. A USER’S WEB BROWSER ISSUES AN HTTP REQUEST FOR A
PARTICULAR WEB PAGE. FOR EXAMPLE, USING AN HTML FORM, SHE
MIGHT HAVE REQUESTED A SEARCH FOR ALL BOOKS AT
MIKKELIONLINEPROFESSIONALBOOKS.COM WRITTEN BY LEILA
KARJALAINEN. THE SEARCH RESULTS PAGE IS CALLED RESULTS.PHP.
2. THE WEB SERVER RECEIVES THE REQUEST FOR RESULTS.PHP,
RETRIEVES THE FILE, AND PASSES IT TO THE PHP ENGINE FOR
PROCESSING.
PHP and MySQL Web Development
tMyn 15
PHP and MySQL Web Development
tMyn 16
1
6
2
5
3
4
MySQL Server
Browser Web Server
PHP Engine
3. THE PHP ENGINE BEGINS PARSING THE SCRIPT. INSIDE THE SCRIPT
IS A COMMAND TO CONNECT TO THE DATABASE AND EXECUTE A
QUERY (PERFORM THE SEARCH FOR BOOKS). PHP OPENS A
CONNECTION TO THE MYSQL SERVER AND SENDS ON THE
APPROPRIATE QUERY.
4. THE MYSQL SERVER RECEIVES THE DATABASE QUERY, PROCESSES
IT, AND SENDS THE RESULTS - A LIST OF BOOKS - BACK TO THE PHP
ENGINE.
5. THE PHP ENGINE FINISHES RUNNING THE SCRIPT, WHICH
USUALLY INVOLVES FORMATTING THE QUERY RESULTS NICELY IN
HTML. IT THEN RETURNS THE RESULTING HTML TO THE WEB SERVER.
PHP and MySQL Web Development
tMyn 17
6. THE WEB SERVER PASSES THE HTML BACK TO THE BROWSER,
WHERE THE USER CAN SEE THE LIST OF BOOKS SHE REQUESTED.
• THE ABOVE DESCRIBED PROCESS IS BASICALLY THE SAME
REGARDLESS OF WHICH SCRIPTING ENGINE OR DATABASE SERVER
YOU USE.
• SOMETIMES THE WEB SERVER, PHP ENGINE, AND DATABASE SERVER
ALL RUN ON THE SAME MACHINE.
• HOWEVER, IT IS QUITE COMMON FOR THE DATABASE SERVER TO
RUN ON A DIFFERENT MACHINE. YOU MIGHT DO THIS FOR
REASONS OF SECURITY, INCREASED CAPACITY, OR LOAD
SPREADING. FROM A DEVELOPMENT PERSPECTIVE, THIS APPROACH
IS MUCH THE SAME TO WORK WITH.
PHP and MySQL Web Development
tMyn 18
• FIRST EXAMPLE READS IN AND DISPLAYS THE CONTENTS OF THE
FRIEND TABLE FROM THE DATABASE FUTURE.
• OUR SCRIPT WILL DO THE FOLLOWING JOBS:
• SET UP A CONNECTION TO THE APPROPRIATE DATABASE
• QUERY THE DATABASE TABLE
• RETRIEVE THE RESULTS
• PRESENT THE RESULTS BACK TO THE USER
• FIRST WE NEED TO CREATE THE NEEDED DATABASE AND DATABASE
TABLE – THIS TIME WE WILL DO IT DIRECTLY USING MYSQL QUERY
BROWSER:
PHP and MySQL Web Development
tMyn 19
TUTORIAL NO.4
PHP and MySQL Web Development
tMyn 20
PHP and MySQL Web Development
tMyn 21
PHP and MySQL Web Development
tMyn 22
• NEXT THE PHP SCRIPT:
PHP and MySQL Web Development
tMyn 23
PHP and MySQL Web Development
tMyn 24
PHP and MySQL Web Development
tMyn 25
PHP and MySQL Web Development
tMyn 26
…and what you can see from the browser:
‘$SQLRESULT = MYSQL_QUERY...’
• WHEN YOU SELECT ITEMS FROM A DATABASE USING
MYSQL_QUERY(), THE DATA IS RETURNED AS A MYSQL
RESULT. SINCE WE WANT TO USE THIS DATA IN OUR
PROGRAM WE NEED TO STORE IT IN A VARIABLE.
$SQLRESULT NOW HOLDS THE RESULT FROM OUR
MYSQL_QUERY().
PHP and MySQL Web Development
tMyn 27
‘WHILE($SQLROW =
MYSQL_FETCH_ARRAY( $SQLRESULT…)’
• THE MYSQL_FETCH_ARRAY FUNCTION GETS THE NEXT-IN-LINE
ASSOCIATIVE ARRAY FROM A MYSQL RESULT. BY PUTTING IT IN A
WHILE LOOP IT WILL CONTINUE TO FETCH THE NEXT ARRAY UNTIL
THERE IS NO NEXT ARRAY TO FETCH. THIS FUNCTION CAN BE
CALLED AS MANY TIMES AS YOU WANT, BUT IT WILL RETURN
FALSE WHEN THE LAST ASSOCIATIVE ARRAY HAS ALREADY BEEN
RETURNED.
• BY PLACING THIS FUNCTION WITHIN THE CONDITIONAL
STATEMENT OF THE WHILE LOOP, WE CAN “KILL” TWO BIRDS WITH
ONE STONE:
PHP and MySQL Web Development
tMyn 28
1. WE CAN RETRIEVE THE NEXT ASSOCIATIVE ARRAY FROM
OUR MYSQL RESOURCE, $SQLRESULT, SO THAT WE CAN
PRINT OUT THE RETRIEVED INFORMATION.
2. WE CAN TELL THE WHILE LOOP TO STOP PRINTING OUT
INFORMATION WHEN THE MYSQL RESOURCE HAS RETURNED
THE LAST ARRAY, AS FALSE IS RETURNED WHEN IT REACHES
THE END AND THIS WILL CAUSE THE WHILE LOOP TO HALT.
• A RESOURCE IS A SPECIAL VARIABLE, HOLDING A REFERENCE
TO AN EXTERNAL RESOURCE.
PHP and MySQL Web Development
tMyn 29
• IN THE ABOVE SCRIPT, WE HAVE ACCESSED THE FIRSTNAME
COLUMN LIKE THIS: $SQLROW[‘FIRSTNAME’]. THAT CAN
ALSO BE DONE BY USING INTEGER INDEXING:
PHP and MySQL Web Development
tMyn 30
PHP and MySQL Web Development
tMyn 31
• OR FINDING OUT THE NUMBER OF ROWS IN A RECORDSET:
PHP and MySQL Web Development
tMyn 32
PHP and MySQL Web Development
tMyn 33
PHP and MySQL Web Development
tMyn 34
• OR RETURNING A ROW FROM A RECORDSET AS AN OBJECT
PHP and MySQL Web Development
tMyn 35
PHP and MySQL Web Development
tMyn 36
PHP and MySQL Web Development
tMyn 37
• A MINOR MODIFICATION TO THE ORIGINAL EXAMPLE: LET’S
MAKE IT DISPLAY A MESSAGE IF THERE IS AN ERROR WHEN
CONNECTING TO THE DATABASE SERVER:
PHP and MySQL Web Development
tMyn 38
PHP and MySQL Web Development
tMyn 39
PHP and MySQL Web Development
tMyn 40
• SO IT SEEMS THAT DIE() NEEDS NO ARGUMENTS BECAUSE
MYSQL_CONNECT() IS ABLE TO GIVE THE SAME
INFORMATION:
PHP and MySQL Web Development
tMyn 41
PHP and MySQL Web Development
tMyn 42
PHP and MySQL Web Development
tMyn 43
• A MINOR MODIFICATION TO THE ORIGINAL EXAMPLE: LET’S
MAKE IT DISPLAY A MESSAGE IF THERE IS AN ERROR WHEN
SELECTING THE DATABASE WE WANT TO USE:
PHP and MySQL Web Development
tMyn 44
PHP and MySQL Web Development
tMyn 45
PHP and MySQL Web Development
tMyn 46
• IN THE NEXT EXAMPLE WE WILL INSERT ONE ROW TO THE
FRIEND TABLE. FIRST DIRECTLY FROM WEB SERVER TO THE
DATABASE SERVER WITHOUT ANY USER INTERFACE.
PHP and MySQL Web Development
tMyn 47
PHP and MySQL Web Development
tMyn 48
PHP and MySQL Web Development
tMyn 49
PHP and MySQL Web Development
tMyn 50
ABIT MORE COMPLEX TASK: INSERT DATA FROM A FORM INTO
A DATABASE:
• NOW WE WILL CREATE AN HTML FORM THAT CAN BE USED
TO ADD NEW RECORDS TO THE FRIEND TABLE, FILE
DATABASE3.HTML:
PHP and MySQL Web Development
tMyn 51
PHP and MySQL Web Development
tMyn 52
• WHEN A USER CLICKS THE SUBMIT BUTTON IN THE HTML
FORM IN THE EXAMPLE ABOVE, THE FORM DATA IS SENT TO
DATABASE3.PHP.
• THE DATABASE3.PHP FILE CONNECTS TO A DATABASE, AND
RETRIEVES THE VALUES FROM THE FORM WITH THE PHP
$_POST VARIABLES.
• THEN, THE MYSQL_QUERY() FUNCTION EXECUTES THE
INSERT INTO STATEMENT, AND A NEW RECORD WILL BE
ADDED TO THE FRIEND TABLE.
• HERE IS THE DATABASE3.PHP PAGE:
PHP and MySQL Web Development
tMyn 53
PHP and MySQL Web Development
tMyn 54
PHP and MySQL Web Development
tMyn 55
PHP and MySQL Web Development
tMyn 56
PHP and MySQL Web Development
tMyn 57
• FROM THE PREVIOUS SLIDE IT CAN BE SEEN THAT THE PRIMARY
KEY JUMPS FROM 4 TO 9 - THAT IS BECAUSE THERE WERE
SOME ERRORS WHEN TESTING THE EXAMPLE…
• THE PRIMARY KEY CAN BE MODIFIED DIRECTLY USING MYSQL
QUERY BROWSER (NATURALLY UPDATES CAN BE DONE USING
OUR HTML USER INTERFACE) IF THE CURRENT SITUATION
ANNOYS SOMEONE:
PHP and MySQL Web Development
tMyn 58
PHP and MySQL Web Development
tMyn 59
• A MINOR MODIFICATION TO THE DATABASE3.PHP EXAMPLE:
LET’S TEST THAT ALL THE HTML FIELDS HAVE AT LEAST
SOMETHING INPUTTED:
PHP and MySQL Web Development
tMyn 60
PHP and MySQL Web Development
tMyn 61
PHP and MySQL Web Development
tMyn 62
PHP and MySQL Web Development
tMyn 63
PHP and MySQL Web Development
tMyn 64
PHP and MySQL Web Development
tMyn 65
PHP and MySQL Web Development
tMyn 66
• A MINOR MODIFICATION TO THE DATABASE3.PHP EXAMPLE:
PUTTING IT ALL IN ONE PAGE.
• THE FIRST TEST: HAS THE USER SUBMITTED THE FORM?
• SECOND TEST: IS THERE SOMETHING IN EVERY FIELD?
PHP and MySQL Web Development
tMyn 67
PHP and MySQL Web Development
tMyn 68
PHP and MySQL Web Development
tMyn 69
PHP and MySQL Web Development
tMyn 70
PHP and MySQL Web Development
tMyn 71
PHP and MySQL Web Development
tMyn 72
PHP and MySQL Web Development
tMyn 73
• IF EVERY FIELD DOES HAVE SOMETHING:
PHP and MySQL Web Development
tMyn 74
PHP and MySQL Web Development
tMyn 75
PHP and MySQL Web Development
tMyn 76
• IF THERE IS ONE OR MORE EMPTY FIELDS:
PHP and MySQL Web Development
tMyn 77
PHP and MySQL Web Development
tMyn 78
PHP and MySQL Web Development
tMyn 79
PHP and MySQL Web Development
tMyn 80
PHP and MySQL Web Development
tMyn 81
UPDATE DATA FROM A FORM INTO A DATABASE:
• NOW WE WILL CREATE AN HTML FORM THAT CAN BE USED
TO UPDATE ONE COLUMN IN THE FRIEND TABLE, FILE
UPDATE1.PHP. WE HAVE ARBITRARILY CHOSEN TO UPDATE THE
FIRST NAME OF THE PERSON.
• THE FIRST TEST: HAS THE USER SUBMITTED THE FORM?
• SECOND TEST: IS THERE SOMETHING IN EVERY FIELD?
• THIRD TEST: IS THE NEW FIRST NAME LONGER THAN THE FIELD
DOMAIN PERMITS?
PHP and MySQL Web Development
tMyn 82
PHP and MySQL Web Development
tMyn 83
PHP and MySQL Web Development
tMyn 84
PHP and MySQL Web Development
tMyn 85
PHP and MySQL Web Development
tMyn 86
PHP and MySQL Web Development
tMyn 87
PHP and MySQL Web Development
tMyn 88
• LET’S FIND OUT WHAT THERE IS IN THE TABLE FRIEND:
PHP and MySQL Web Development
tMyn 89
PHP and MySQL Web Development
tMyn 90
• THE TASK IS TO UPDATE THE FIRST NAME ROSALIND TO
ROSALIND ELSIE:
PHP and MySQL Web Development
tMyn 91
PHP and MySQL Web Development
tMyn 92
PHP and MySQL Web Development
tMyn 93
• THE THIRD TEST: HAS THE USER INPUTTED TOO LONG A
NAME?:
PHP and MySQL Web Development
tMyn 94
PHP and MySQL Web Development
tMyn 95
New First Name: Longer than 45 character constants
PHP and MySQL Web Development
tMyn 96
• ONE MORE DETAIL FROM THE PREVIOUS EXAMPLE: THE FIRST
PARAMETER OF THE MYSQL_QUERY() IS UPDATE STATEMENT.
• UPDATE AND DELETE STATEMENTS BEHAVE IN THE SAME WAY
IN THOSE KINDS OF SITUATIONS: IF THERE ARE NO ROWS TO
BE UPDATED OR DELETED, THEN THERE WOULD NOT COME
ANY WARNINGS OR ERRORS BACK:
PHP and MySQL Web Development
tMyn 97
PHP and MySQL Web Development
tMyn 98
PHP and MySQL Web Development
tMyn 99
• DEFINITELY WE NEED TO IMPROVE THE SOURCE CODE:
PHP and MySQL Web Development
tMyn 100
PHP and MySQL Web Development
tMyn 101
PHP and MySQL Web Development
tMyn 102
• LET US TEST THE MODIFICATION. THE SECOND NAME AND THE
ADDRESS ARE VALID VALUES:
PHP and MySQL Web Development
tMyn 103
PHP and MySQL Web Development
tMyn 104
PHP and MySQL Web Development
tMyn 105
• THE FOLLOWING EXAMPLE SELECTS THE SAME DATA AS THE
EXAMPLE ABOVE, BUT WILL DISPLAY THE DATA IN AN HTML
TABLE:
PHP and MySQL Web Development
tMyn 106
PHP and MySQL Web Development
tMyn 107
PHP and MySQL Web Development
tMyn 108
PHP and MySQL Web Development
tMyn 109

More Related Content

PPT
Php and MySQL Web Development
PDF
PHP with MySQL
PPT
Php with MYSQL Database
PPT
PHP - Getting good with MySQL part II
ODP
ODP
PDF
9 Python programming notes for ktu physics and computer application semester 4
PDF
Using php with my sql
Php and MySQL Web Development
PHP with MySQL
Php with MYSQL Database
PHP - Getting good with MySQL part II
9 Python programming notes for ktu physics and computer application semester 4
Using php with my sql

Similar to Lect_04b_PhpMysqlKEY PERFORMANCE INDICATOR FOR ICT-UNIT (new).ppt (20)

PPT
Php classes in mumbai
PPT
9780538745840 ppt ch08
PPTX
Interface Python with MySQL connectivity.pptx
PPT
Php MySql For Beginners
PPTX
Interface Python with MySQLwedgvwewefwefwe.pptx
PPT
Synapse india reviews on php and sql
PPT
PHP - Introduction to PHP Date and Time Functions
PPT
MySQLi - An Improved Extension of MySQL
PPTX
Database Connectivity using Python and MySQL
PPTX
PythonDatabaseAPI -Presentation for Database
PDF
Fnt Software Solutions Pvt Ltd Placement Papers - PHP Technology
PPT
Sql php-vibrant course-mumbai(1)
PPTX
Learn PHP Lacture2
PDF
RMySQL Tutorial For Beginners
DOCX
VPN Access Runbook
PPTX
PHP FUNCTIONS
PPTX
SQL-Connectivity python for beginners easy explanation with concepts and outp...
PPTX
3-Chapter-Edit.pptx debre tabour university
PDF
Introduction to JDBC and database access in web applications
Php classes in mumbai
9780538745840 ppt ch08
Interface Python with MySQL connectivity.pptx
Php MySql For Beginners
Interface Python with MySQLwedgvwewefwefwe.pptx
Synapse india reviews on php and sql
PHP - Introduction to PHP Date and Time Functions
MySQLi - An Improved Extension of MySQL
Database Connectivity using Python and MySQL
PythonDatabaseAPI -Presentation for Database
Fnt Software Solutions Pvt Ltd Placement Papers - PHP Technology
Sql php-vibrant course-mumbai(1)
Learn PHP Lacture2
RMySQL Tutorial For Beginners
VPN Access Runbook
PHP FUNCTIONS
SQL-Connectivity python for beginners easy explanation with concepts and outp...
3-Chapter-Edit.pptx debre tabour university
Introduction to JDBC and database access in web applications
Ad

More from SenzotaSemakuwa (10)

PPT
Widespread Adoption and IntegrationCACC_05.ppt
PPT
Advancements in Models CA Advancements in ModelsCC_04.ppt
PPT
KEY PERFORMANCE INDICATOR FOR ICT-UNIT (new)Lect_04c_Detailed_Self_Reading.ppt
PPT
Lecture_Notes_AutomatContext Free Languages.ppt
PPT
For mapping a category whose defining superclass Additional02.ppt
PPT
Cross-reference or relationship relation optionAdditional01.ppt
PPT
Mapping of Multivalued attributesER-to-Relational Mapping Algorithm.ppt
PPT
Mapping EER Model Constructs to Relations ADB_05_BRvs.ppt
PPTX
Ethical_Issues_in_Ecommerce_Presentation.pptx
PPT
168581476-Critical-Characteristics-of-Information-In-Information-Security.ppt
Widespread Adoption and IntegrationCACC_05.ppt
Advancements in Models CA Advancements in ModelsCC_04.ppt
KEY PERFORMANCE INDICATOR FOR ICT-UNIT (new)Lect_04c_Detailed_Self_Reading.ppt
Lecture_Notes_AutomatContext Free Languages.ppt
For mapping a category whose defining superclass Additional02.ppt
Cross-reference or relationship relation optionAdditional01.ppt
Mapping of Multivalued attributesER-to-Relational Mapping Algorithm.ppt
Mapping EER Model Constructs to Relations ADB_05_BRvs.ppt
Ethical_Issues_in_Ecommerce_Presentation.pptx
168581476-Critical-Characteristics-of-Information-In-Information-Security.ppt
Ad

Recently uploaded (20)

PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
Classroom Observation Tools for Teachers
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Cell Types and Its function , kingdom of life
PDF
Trump Administration's workforce development strategy
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
01-Introduction-to-Information-Management.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Lesson notes of climatology university.
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Microbial disease of the cardiovascular and lymphatic systems
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
RMMM.pdf make it easy to upload and study
Classroom Observation Tools for Teachers
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
human mycosis Human fungal infections are called human mycosis..pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
O7-L3 Supply Chain Operations - ICLT Program
Cell Types and Its function , kingdom of life
Trump Administration's workforce development strategy
Supply Chain Operations Speaking Notes -ICLT Program
01-Introduction-to-Information-Management.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
GDM (1) (1).pptx small presentation for students
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Lesson notes of climatology university.

Lect_04b_PhpMysqlKEY PERFORMANCE INDICATOR FOR ICT-UNIT (new).ppt

  • 1. PHP AND MYSQL WEB DEVELOPMENT • WHEN YOU INSTALL PHP, YOU CAN SELECT FROM A NUMBER OF EXTENSIONS. • THE MYSQL SUPPORT IN PHP CONSISTS OF A NUMBER OF FUNCTIONS YOU CAN CALL TO INTERACT WITH MYSQL, AND HERE ARE SOME OF THEM: PHP and MySQL Web Development tMyn 1
  • 2. PHP and MySQL Web Development tMyn 2 The mysql_connect() function opens a non-persistent MySQL connection. This function returns the connection on success, or FALSE and an error on failure. Syntax mysql_connect(server,user,pwd,newlink,clientflag) Parameter Description server Optional. Specifies the server to connect to (can also include a port number, e.g. "hostname:port" or a path to a local socket for the localhost). Default value is "localhost:3306" user Optional. Specifies the username to log in with. Default value is the name of the user that owns the server process pwd Optional. Specifies the password to log in with. Default is ""
  • 3. PHP and MySQL Web Development tMyn 3 The mysql_select_db() function sets the active MySQL database. This function returns TRUE on success, or FALSE on failure. Syntax mysql_select_db(database,connection) Parameter Description database Required. Specifies the database to select. connection Optional. Specifies the MySQL connection. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used.
  • 4. PHP and MySQL Web Development tMyn 4 The mysql_query() function executes a query on a MySQL database. This function returns the query handle for SELECT queries, TRUE/FALSE for other queries, or FALSE on failure. Syntax mysql_query(query,connection) Parameter Description query Required. Specifies the SQL query to send (should not end with a semicolon). connection Optional. Specifies the MySQL connection. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used.
  • 5. PHP and MySQL Web Development tMyn 5 The mysql_fetch_array() function returns a row from a recordset as an associative array and/or a numeric array. This function gets a row from the mysql_query() function and returns an array on success, or FALSE on failure or when there are no more rows. Syntax mysql_fetch_array(data,array_type) Parameter Description data Required. Specifies which data pointer to use. The data pointer is the result from the mysql_query() function array_type Optional. Specifies what kind of array to return. Possible values: MYSQL_ASSOC - Associative array MYSQL_NUM - Numeric array MYSQL_BOTH - Default. Both associative and numeric array
  • 6. PHP and MySQL Web Development tMyn 6 The mysql_fetch_object() function returns a row from a recordset as an object. This function gets a row from the mysql_query() function and returns an object on success, or FALSE on failure or when there are no more rows. Syntax mysql_fetch_object(data) Parameter Description data Required. Specifies which data pointer to use. The data pointer is the result from the mysql_query() function Tips and Notes Note: Each subsequent call to mysql_fetch_object() returns the next row in the recordset.
  • 7. PHP and MySQL Web Development tMyn 7 The mysql_affected_rows() function returns the number of affected rows in the previous MySQL operation. This function returns the number of affected rows on success, or -1 if the last operation failed. Syntax mysql_affected_rows(connection) Parameter Description connection Optional. Specifies the MySQL connection. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used.
  • 8. PHP and MySQL Web Development tMyn 8 The mysql_num_rows() function returns the number of rows in a recordset. This function returns FALSE on failure. Syntax mysql_num_rows(data) Parameter Description data Required. Specifies which data pointer to use. The data pointer is the result from the mysql_query() function
  • 9. PHP and MySQL Web Development tMyn 9 The mysql_result() function returns the value of a field in a recordset. This function returns the field value on success, or FALSE on failure. Syntax mysql_result(data,row,field) Parameter Description data Required. Specifies which result handle to use. The data pointer is the return from the mysql_query() function row Required. Specifies which row number to get. Row numbers start at 0
  • 10. PHP and MySQL Web Development tMyn 10 field Optional. Specifies which field to get. Can be field offset, field name or table.fieldname. If this parameter is not defined mysql_result() gets the first field from the specified row. Tips and Notes This function is slower than mysql_fetch_row(), mysql_fetch_array(), mysql_fetch_assoc() and mysql_fetch_object().
  • 11. PHP and MySQL Web Development tMyn 11 The mysql_error() function returns the error description of the last MySQL operation. This function returns an empty string ("") if no error occurs. Syntax mysql_error(connection) Parameter Description connection Optional. Specifies the MySQL connection. If not specified, the last connection opened by mysql_connect() or mysql_pconnect() is used.
  • 12. PHP and MySQL Web Development tMyn 12 The mysql_close() function closes a non-persistent MySQL connection. This function returns TRUE on success, or FALSE on failure. Syntax mysql_close(connection) Parameter Description connection Optional. Specifies the MySQL connection to close. If not specified, the last connection opened by mysql_connect() is used.
  • 13. PHP and MySQL Web Development tMyn 13 die — Equivalent to exit() Description This language construct is equivalent to exit().
  • 14. PHP and MySQL Web Development tMyn 14 exit — Output a message and terminate the current script Description void exit ([ string $status ] ) void exit ( int $status ) Terminates execution of the script. Parameters status If status is a string, this function prints the status just before exiting. If status is an integer, that value will also be used as the exit status. Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.
  • 15. • A TYPICAL WEB DATABASE TRANSACTION CONSISTS OF THE FOLLOWING STAGES, WHICH ARE NUMBERED IN THE FIGURE 1: 1. A USER’S WEB BROWSER ISSUES AN HTTP REQUEST FOR A PARTICULAR WEB PAGE. FOR EXAMPLE, USING AN HTML FORM, SHE MIGHT HAVE REQUESTED A SEARCH FOR ALL BOOKS AT MIKKELIONLINEPROFESSIONALBOOKS.COM WRITTEN BY LEILA KARJALAINEN. THE SEARCH RESULTS PAGE IS CALLED RESULTS.PHP. 2. THE WEB SERVER RECEIVES THE REQUEST FOR RESULTS.PHP, RETRIEVES THE FILE, AND PASSES IT TO THE PHP ENGINE FOR PROCESSING. PHP and MySQL Web Development tMyn 15
  • 16. PHP and MySQL Web Development tMyn 16 1 6 2 5 3 4 MySQL Server Browser Web Server PHP Engine
  • 17. 3. THE PHP ENGINE BEGINS PARSING THE SCRIPT. INSIDE THE SCRIPT IS A COMMAND TO CONNECT TO THE DATABASE AND EXECUTE A QUERY (PERFORM THE SEARCH FOR BOOKS). PHP OPENS A CONNECTION TO THE MYSQL SERVER AND SENDS ON THE APPROPRIATE QUERY. 4. THE MYSQL SERVER RECEIVES THE DATABASE QUERY, PROCESSES IT, AND SENDS THE RESULTS - A LIST OF BOOKS - BACK TO THE PHP ENGINE. 5. THE PHP ENGINE FINISHES RUNNING THE SCRIPT, WHICH USUALLY INVOLVES FORMATTING THE QUERY RESULTS NICELY IN HTML. IT THEN RETURNS THE RESULTING HTML TO THE WEB SERVER. PHP and MySQL Web Development tMyn 17
  • 18. 6. THE WEB SERVER PASSES THE HTML BACK TO THE BROWSER, WHERE THE USER CAN SEE THE LIST OF BOOKS SHE REQUESTED. • THE ABOVE DESCRIBED PROCESS IS BASICALLY THE SAME REGARDLESS OF WHICH SCRIPTING ENGINE OR DATABASE SERVER YOU USE. • SOMETIMES THE WEB SERVER, PHP ENGINE, AND DATABASE SERVER ALL RUN ON THE SAME MACHINE. • HOWEVER, IT IS QUITE COMMON FOR THE DATABASE SERVER TO RUN ON A DIFFERENT MACHINE. YOU MIGHT DO THIS FOR REASONS OF SECURITY, INCREASED CAPACITY, OR LOAD SPREADING. FROM A DEVELOPMENT PERSPECTIVE, THIS APPROACH IS MUCH THE SAME TO WORK WITH. PHP and MySQL Web Development tMyn 18
  • 19. • FIRST EXAMPLE READS IN AND DISPLAYS THE CONTENTS OF THE FRIEND TABLE FROM THE DATABASE FUTURE. • OUR SCRIPT WILL DO THE FOLLOWING JOBS: • SET UP A CONNECTION TO THE APPROPRIATE DATABASE • QUERY THE DATABASE TABLE • RETRIEVE THE RESULTS • PRESENT THE RESULTS BACK TO THE USER • FIRST WE NEED TO CREATE THE NEEDED DATABASE AND DATABASE TABLE – THIS TIME WE WILL DO IT DIRECTLY USING MYSQL QUERY BROWSER: PHP and MySQL Web Development tMyn 19 TUTORIAL NO.4
  • 20. PHP and MySQL Web Development tMyn 20
  • 21. PHP and MySQL Web Development tMyn 21
  • 22. PHP and MySQL Web Development tMyn 22
  • 23. • NEXT THE PHP SCRIPT: PHP and MySQL Web Development tMyn 23
  • 24. PHP and MySQL Web Development tMyn 24
  • 25. PHP and MySQL Web Development tMyn 25
  • 26. PHP and MySQL Web Development tMyn 26 …and what you can see from the browser:
  • 27. ‘$SQLRESULT = MYSQL_QUERY...’ • WHEN YOU SELECT ITEMS FROM A DATABASE USING MYSQL_QUERY(), THE DATA IS RETURNED AS A MYSQL RESULT. SINCE WE WANT TO USE THIS DATA IN OUR PROGRAM WE NEED TO STORE IT IN A VARIABLE. $SQLRESULT NOW HOLDS THE RESULT FROM OUR MYSQL_QUERY(). PHP and MySQL Web Development tMyn 27
  • 28. ‘WHILE($SQLROW = MYSQL_FETCH_ARRAY( $SQLRESULT…)’ • THE MYSQL_FETCH_ARRAY FUNCTION GETS THE NEXT-IN-LINE ASSOCIATIVE ARRAY FROM A MYSQL RESULT. BY PUTTING IT IN A WHILE LOOP IT WILL CONTINUE TO FETCH THE NEXT ARRAY UNTIL THERE IS NO NEXT ARRAY TO FETCH. THIS FUNCTION CAN BE CALLED AS MANY TIMES AS YOU WANT, BUT IT WILL RETURN FALSE WHEN THE LAST ASSOCIATIVE ARRAY HAS ALREADY BEEN RETURNED. • BY PLACING THIS FUNCTION WITHIN THE CONDITIONAL STATEMENT OF THE WHILE LOOP, WE CAN “KILL” TWO BIRDS WITH ONE STONE: PHP and MySQL Web Development tMyn 28
  • 29. 1. WE CAN RETRIEVE THE NEXT ASSOCIATIVE ARRAY FROM OUR MYSQL RESOURCE, $SQLRESULT, SO THAT WE CAN PRINT OUT THE RETRIEVED INFORMATION. 2. WE CAN TELL THE WHILE LOOP TO STOP PRINTING OUT INFORMATION WHEN THE MYSQL RESOURCE HAS RETURNED THE LAST ARRAY, AS FALSE IS RETURNED WHEN IT REACHES THE END AND THIS WILL CAUSE THE WHILE LOOP TO HALT. • A RESOURCE IS A SPECIAL VARIABLE, HOLDING A REFERENCE TO AN EXTERNAL RESOURCE. PHP and MySQL Web Development tMyn 29
  • 30. • IN THE ABOVE SCRIPT, WE HAVE ACCESSED THE FIRSTNAME COLUMN LIKE THIS: $SQLROW[‘FIRSTNAME’]. THAT CAN ALSO BE DONE BY USING INTEGER INDEXING: PHP and MySQL Web Development tMyn 30
  • 31. PHP and MySQL Web Development tMyn 31
  • 32. • OR FINDING OUT THE NUMBER OF ROWS IN A RECORDSET: PHP and MySQL Web Development tMyn 32
  • 33. PHP and MySQL Web Development tMyn 33
  • 34. PHP and MySQL Web Development tMyn 34
  • 35. • OR RETURNING A ROW FROM A RECORDSET AS AN OBJECT PHP and MySQL Web Development tMyn 35
  • 36. PHP and MySQL Web Development tMyn 36
  • 37. PHP and MySQL Web Development tMyn 37
  • 38. • A MINOR MODIFICATION TO THE ORIGINAL EXAMPLE: LET’S MAKE IT DISPLAY A MESSAGE IF THERE IS AN ERROR WHEN CONNECTING TO THE DATABASE SERVER: PHP and MySQL Web Development tMyn 38
  • 39. PHP and MySQL Web Development tMyn 39
  • 40. PHP and MySQL Web Development tMyn 40
  • 41. • SO IT SEEMS THAT DIE() NEEDS NO ARGUMENTS BECAUSE MYSQL_CONNECT() IS ABLE TO GIVE THE SAME INFORMATION: PHP and MySQL Web Development tMyn 41
  • 42. PHP and MySQL Web Development tMyn 42
  • 43. PHP and MySQL Web Development tMyn 43
  • 44. • A MINOR MODIFICATION TO THE ORIGINAL EXAMPLE: LET’S MAKE IT DISPLAY A MESSAGE IF THERE IS AN ERROR WHEN SELECTING THE DATABASE WE WANT TO USE: PHP and MySQL Web Development tMyn 44
  • 45. PHP and MySQL Web Development tMyn 45
  • 46. PHP and MySQL Web Development tMyn 46
  • 47. • IN THE NEXT EXAMPLE WE WILL INSERT ONE ROW TO THE FRIEND TABLE. FIRST DIRECTLY FROM WEB SERVER TO THE DATABASE SERVER WITHOUT ANY USER INTERFACE. PHP and MySQL Web Development tMyn 47
  • 48. PHP and MySQL Web Development tMyn 48
  • 49. PHP and MySQL Web Development tMyn 49
  • 50. PHP and MySQL Web Development tMyn 50
  • 51. ABIT MORE COMPLEX TASK: INSERT DATA FROM A FORM INTO A DATABASE: • NOW WE WILL CREATE AN HTML FORM THAT CAN BE USED TO ADD NEW RECORDS TO THE FRIEND TABLE, FILE DATABASE3.HTML: PHP and MySQL Web Development tMyn 51
  • 52. PHP and MySQL Web Development tMyn 52
  • 53. • WHEN A USER CLICKS THE SUBMIT BUTTON IN THE HTML FORM IN THE EXAMPLE ABOVE, THE FORM DATA IS SENT TO DATABASE3.PHP. • THE DATABASE3.PHP FILE CONNECTS TO A DATABASE, AND RETRIEVES THE VALUES FROM THE FORM WITH THE PHP $_POST VARIABLES. • THEN, THE MYSQL_QUERY() FUNCTION EXECUTES THE INSERT INTO STATEMENT, AND A NEW RECORD WILL BE ADDED TO THE FRIEND TABLE. • HERE IS THE DATABASE3.PHP PAGE: PHP and MySQL Web Development tMyn 53
  • 54. PHP and MySQL Web Development tMyn 54
  • 55. PHP and MySQL Web Development tMyn 55
  • 56. PHP and MySQL Web Development tMyn 56
  • 57. PHP and MySQL Web Development tMyn 57
  • 58. • FROM THE PREVIOUS SLIDE IT CAN BE SEEN THAT THE PRIMARY KEY JUMPS FROM 4 TO 9 - THAT IS BECAUSE THERE WERE SOME ERRORS WHEN TESTING THE EXAMPLE… • THE PRIMARY KEY CAN BE MODIFIED DIRECTLY USING MYSQL QUERY BROWSER (NATURALLY UPDATES CAN BE DONE USING OUR HTML USER INTERFACE) IF THE CURRENT SITUATION ANNOYS SOMEONE: PHP and MySQL Web Development tMyn 58
  • 59. PHP and MySQL Web Development tMyn 59
  • 60. • A MINOR MODIFICATION TO THE DATABASE3.PHP EXAMPLE: LET’S TEST THAT ALL THE HTML FIELDS HAVE AT LEAST SOMETHING INPUTTED: PHP and MySQL Web Development tMyn 60
  • 61. PHP and MySQL Web Development tMyn 61
  • 62. PHP and MySQL Web Development tMyn 62
  • 63. PHP and MySQL Web Development tMyn 63
  • 64. PHP and MySQL Web Development tMyn 64
  • 65. PHP and MySQL Web Development tMyn 65
  • 66. PHP and MySQL Web Development tMyn 66
  • 67. • A MINOR MODIFICATION TO THE DATABASE3.PHP EXAMPLE: PUTTING IT ALL IN ONE PAGE. • THE FIRST TEST: HAS THE USER SUBMITTED THE FORM? • SECOND TEST: IS THERE SOMETHING IN EVERY FIELD? PHP and MySQL Web Development tMyn 67
  • 68. PHP and MySQL Web Development tMyn 68
  • 69. PHP and MySQL Web Development tMyn 69
  • 70. PHP and MySQL Web Development tMyn 70
  • 71. PHP and MySQL Web Development tMyn 71
  • 72. PHP and MySQL Web Development tMyn 72
  • 73. PHP and MySQL Web Development tMyn 73
  • 74. • IF EVERY FIELD DOES HAVE SOMETHING: PHP and MySQL Web Development tMyn 74
  • 75. PHP and MySQL Web Development tMyn 75
  • 76. PHP and MySQL Web Development tMyn 76
  • 77. • IF THERE IS ONE OR MORE EMPTY FIELDS: PHP and MySQL Web Development tMyn 77
  • 78. PHP and MySQL Web Development tMyn 78
  • 79. PHP and MySQL Web Development tMyn 79
  • 80. PHP and MySQL Web Development tMyn 80
  • 81. PHP and MySQL Web Development tMyn 81
  • 82. UPDATE DATA FROM A FORM INTO A DATABASE: • NOW WE WILL CREATE AN HTML FORM THAT CAN BE USED TO UPDATE ONE COLUMN IN THE FRIEND TABLE, FILE UPDATE1.PHP. WE HAVE ARBITRARILY CHOSEN TO UPDATE THE FIRST NAME OF THE PERSON. • THE FIRST TEST: HAS THE USER SUBMITTED THE FORM? • SECOND TEST: IS THERE SOMETHING IN EVERY FIELD? • THIRD TEST: IS THE NEW FIRST NAME LONGER THAN THE FIELD DOMAIN PERMITS? PHP and MySQL Web Development tMyn 82
  • 83. PHP and MySQL Web Development tMyn 83
  • 84. PHP and MySQL Web Development tMyn 84
  • 85. PHP and MySQL Web Development tMyn 85
  • 86. PHP and MySQL Web Development tMyn 86
  • 87. PHP and MySQL Web Development tMyn 87
  • 88. PHP and MySQL Web Development tMyn 88
  • 89. • LET’S FIND OUT WHAT THERE IS IN THE TABLE FRIEND: PHP and MySQL Web Development tMyn 89
  • 90. PHP and MySQL Web Development tMyn 90
  • 91. • THE TASK IS TO UPDATE THE FIRST NAME ROSALIND TO ROSALIND ELSIE: PHP and MySQL Web Development tMyn 91
  • 92. PHP and MySQL Web Development tMyn 92
  • 93. PHP and MySQL Web Development tMyn 93
  • 94. • THE THIRD TEST: HAS THE USER INPUTTED TOO LONG A NAME?: PHP and MySQL Web Development tMyn 94
  • 95. PHP and MySQL Web Development tMyn 95 New First Name: Longer than 45 character constants
  • 96. PHP and MySQL Web Development tMyn 96
  • 97. • ONE MORE DETAIL FROM THE PREVIOUS EXAMPLE: THE FIRST PARAMETER OF THE MYSQL_QUERY() IS UPDATE STATEMENT. • UPDATE AND DELETE STATEMENTS BEHAVE IN THE SAME WAY IN THOSE KINDS OF SITUATIONS: IF THERE ARE NO ROWS TO BE UPDATED OR DELETED, THEN THERE WOULD NOT COME ANY WARNINGS OR ERRORS BACK: PHP and MySQL Web Development tMyn 97
  • 98. PHP and MySQL Web Development tMyn 98
  • 99. PHP and MySQL Web Development tMyn 99
  • 100. • DEFINITELY WE NEED TO IMPROVE THE SOURCE CODE: PHP and MySQL Web Development tMyn 100
  • 101. PHP and MySQL Web Development tMyn 101
  • 102. PHP and MySQL Web Development tMyn 102
  • 103. • LET US TEST THE MODIFICATION. THE SECOND NAME AND THE ADDRESS ARE VALID VALUES: PHP and MySQL Web Development tMyn 103
  • 104. PHP and MySQL Web Development tMyn 104
  • 105. PHP and MySQL Web Development tMyn 105
  • 106. • THE FOLLOWING EXAMPLE SELECTS THE SAME DATA AS THE EXAMPLE ABOVE, BUT WILL DISPLAY THE DATA IN AN HTML TABLE: PHP and MySQL Web Development tMyn 106
  • 107. PHP and MySQL Web Development tMyn 107
  • 108. PHP and MySQL Web Development tMyn 108
  • 109. PHP and MySQL Web Development tMyn 109