Upload
tara-marie-robinson-white
View
4
Download
0
Embed Size (px)
Citation preview
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 1/105
Working with Databasesand MySQL
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 2/105
Introduction to Databases
• A database is an ordered collection ofinformation from which a computer programcan quickly access information
• ach row in a database table is called a record
• A record in a database is a single completeset of related information
• ach column in a database table is called a!eld
• Fields are the indi"idual categories ofinformation stored in a record
Name of Book 2
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 3/105
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 4/105
Introduction to Databases#continued$
• A fat-le database storesinformation in a single table
•
A relational database stores information across multiple relatedtables
4
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 5/105
%nderstanding &elationalDatabases
• &elational databases consist of one or morerelated tables
• A primary table is the main table in a
relationship that is referenced by anothertable
• A related table #or 'child table($references a primary table in a relational
database• A primary key is a !eld that contains a
unique identi!er for each record in aprimary table
5
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 6/105
%nderstanding &elational Databases#continued$
• A primary key is a type of inde)*which identi!es records in a databaseto make retrie"als and sorting faster
• A oreign key is a !eld in a relatedtable that refers to the primary key in aprimary table
• +rimary and foreign keys link recordsacross multiple tables in a relationaldatabase
6
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 7/105
,ne-to-,ne &elationships
• A one-to-one relationship e)ists betweentwo tables when a related table containse)actly one record for each record in theprimary table
• .reate one-to-one relationships to breakinformation into multiple* logical sets
• Information in the tables in a one-to-onerelationship can be placed within a single
table• Make the information in one of the tables
con!dential and accessible only by certainindi"iduals
7
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 8/105
,ne-to-,ne &elationships#continued$
8
Figure 8-2 One-to-one relationship
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 9/105
,ne-to-,ne &elationships#continued$
9
Figure 8-2 One-to-one relationship (continued)
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 10/105
,ne-to-Many &elationship
• A one-to-many relationship e)ists in arelational database when one record in aprimary table has many related records in
a related table• /reaking tables into multiple related tables
to reduce redundant and duplicateinformation is called normalization
• +ro"ides a more e0cient and lessredundant method of storing thisinformation in a database
10
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 11/105
,ne-to-Many &elationship#continued$
11
Figure 8-3 Table with redundant inormation
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 12/105
,ne-to-Many &elationship#continued$
12
Figure 8-! One-to-many relationship
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 13/105
,ne-to-Many &elationship#continued$
13
Figure 8-! One-to-many relationship (continued)
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 14/105
Many-to-Many &elationship
• A many-to-many relationship e)istsin a relational database when manyrecords in one table are related to
many records in another table• A junction table creates a one-to-
many relationship for each of the twotables in a
many-to-many relationship• A 1unction table contains foreign keys
from the two tables
14
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 15/105
Working with Database ManagementSystems
• A database management system #or D/MS$is an application or collection of applicationsused to access and manage a database
• A schema is the structure of a databaseincluding its tables* !elds* and relationships
• A fat-le database management systemis a system that stores data in a 2at-!le format
• A relational database management
system #or &D/MS$ is a system that storesdata in a relational format
15
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 16/105
Working with Database ManagementSystems #continued$
16
Figure 8-" #any-to-many relationship
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 17/105
Working with Database ManagementSystems #continued$
17
Figure 8-" #any-to-many relationship (continued)
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 18/105
Working with Database ManagementSystems #continued$
• Important aspects of databasemanagement systems3
– 4he structuring and preser"ation of thedatabase !le
– nsuring that data is stored correctly ina database5s tables* regardless of the
database format – Querying capability
18
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 19/105
Working with Database ManagementSystems #continued$
• A query is a structured set of instructionsand criteria for retrie"ing* adding* modifying*and deleting database information
• Structured query language #or SQL$ is astandard data manipulation language used
among many database managementsystems
• Open database connectivity #or ,D/.$allows ,D/.-compliant applications toaccess any data source for which there is an,D/. dri"er
19
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 20/105
Querying Databases with StructuredQuery Language
20
Table 8-1 $ommon %&' eywords
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 21/105
!S"#
Name of Book 21
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 22/105
$hat is yS"#%
• MySQL is a database engine that usesthe standard SQL interface6 MySQL is"ery popular as a back end for web
applications6• 4he MySQL engine can be accessed
from most ma1or
programming7scripting languagessuch as perl* php* and ruby making iteasy to de"elop applications6
Name of Book 22
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 23/105
$hat is yS"# good or%
• $eb &pplications3 /ecause MySQL canbe easily integrated into scriptinglanguages such as perl and php it is "ery
popular for web applications6• '() &pplications3 4he same scripting
language integration that makes MySQLwork well with web applications also
makes it work well with local 8%I and e"encommand line applications or anythingelse that stores structured data6
Name of Book 23
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 24/105
yS"# *ommands
• +o list , vie , see all the database justtype. show databases;
• +o use the database just type. use <database_name>;
)3 use `sample1`;
24
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 25/105
yS"# *ommands
• +o list , vie , see all the tables in thedatabase just type. show tables; (show the tables inside
the current database) or show tables from <database_name>; (to
see tables from different database(s));
)3 show tables from `sample1`;
25
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 26/105
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 27/105
yS"# *ommands
• /dit , 0ename a database.
MySql remo"e the rename feature so you
need to drop and create a new databaseinstead of 1ust renaming it666
27
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 28/105
yS"# *ommands
• 1elete a database.
Synta)3
!R$% !AA"A#E <database_name>;
)3 !R$% !AA"A#E `sample1` ;
28
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 29/105
yS"# *ommands
• *reate a table. 2ote. when creating a table* !rst you
need to be sure that you are using thecorrect database in which you want tocreate your new table and also create atleast : !eld for the table otherwise* an
error will occur666
29
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 30/105
yS"# *ommands
• *reate a table.Synta)3CREAE A"&E <table_name> (<field_name>
<t'pe>(sie_of_the_field) *&& + $ *&&) -; if you want the !eld to accept <ull"alues or not
)3CREAE A"&E `sample1` (`student_name`
,ARC-AR( 1.. ) *&&);
30
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 31/105
yS"# *ommands
• /dit , 0ename a table.
Synta)3
A&ER A"&E <old_table_name>REA/E <new_table_name>;
)3
A&ER A"&E `sample1` REA/E`sample0` ;
31
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 32/105
yS"# *ommands
• 1elete a table.
Synta)3
!R$% A"&E <table_name>;
)3 !R$% A"&E `sample0`;
32
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 33/105
yS"# *ommands
• +o sho all columns , elds rom atable just type.
Synta)3 #how columns from <table_name>;
)3
#how columns from `sample1`;
33
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 34/105
yS"# *ommands
• )nsert a eld , column to a table.
Synta)3
A&ER A"&E <table_name> A!!<field_name> <t'pe>( <sie> ) *&& + $
*&& ;
)3 A&ER A"&E `sample1` A!! `address`
,ARC-AR( 1.. ) *&& ;
34
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 35/105
yS"# *ommands
• /dit , 0ename a eld , column o atable4
Synta)3 A&ER A"&E <table_name> C-AE
<old_field_name> <new_field_name>
<t'pe>(<sie>) *&& + $ *&&;
)3 A&ER A"&E `sample1` C-AE `name1`
`name` 2archar(1..) *&&;
35
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 36/105
yS"# *ommands
• 1elete a eld , column o a table4
Synta)3
A&ER A"&E <table_name> !R$%<field_name>;
)3
A&ER A"&E `sample1` !R$%`address`;
36
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 37/105
yS"# *ommands
• )nserting ne record. Synta)3 3#ER 3$ <table_name>
(<field1>4<field0>555) ,A&*E#(<2alue1>4<2alue0>555);
)3
3#ER 3$ `sample1` ( ` name` 4`address` ) ,A&*E# (6marco elias palic
2alencia64 6porac4 pampan7a6);
37
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 38/105
yS"# *ommands
• /dit a record. A: Without a condition: Synta)3
*%!AE <table_name> set <field_name1>8 <new_2alue1>4 <field_name0>
8<new_2alue0>555 ;
)3 *%!AE `sample1` #E ` name` 8 6marco
elias palic 2alencia164`address` 8
6porac4 pampan7a16;
38
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 39/105
yS"# *ommands
• /dit a record. B: With a condition: Synta)3 *%!AE <table_name> set <field_name1> 8
<new_2alue1>4 <field_name0> 8 <new_2alue0>555
9here <field_name1> 8 <condition_2alue1> and
<field_name0> 8 <condition_2alue0>555;
)3 *%!AE `sample1` #E ` name` 8 6marco elias
palic 2alencia164`address` 8 6porac4 pampan7a16
where name` 8 6marco elias palic 2alencia6 and
`address` 8 6porac4 pampan7a6;
39
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 40/105
yS"# *ommands
• 1elete a record.
A: Without a condition:
Synta)3 !E&EE from <table_name>;
)3 !E&EE from `sample1`;
40
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 41/105
yS"# *ommands
• 1elete a record.
B: With a condition: Synta)3 !E&EE from <table_name> where
<field_name1> 8 <condition_2alue1> and
<field_name0> 8 <condition_2alue0>555;
)3 !E&EE :R$/ `sample1` 9-ERE `name` 8 6marco
elias palic 2alencia6 A! `address` 8 6porac4
pampan7a6;
41
S"# * d
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 42/105
yS"# *ommands• 5ieing , Searching or record6s7
A: Viewing all records: Synta)3
#E&EC <field1>4 <field0> 555 from <table_name>;
or if you want to select all !elds 1ust type =#asterisk$ #E&EC from <table_name>;
)3
#E&EC `name`4`address` from `sample1`; or #E&EC from `sample1`;
42
S"# * d
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 43/105
yS"# *ommands• 5ieing , Searching or record6s7
B: Searching for a particular record: Synta)3
#E&EC <field1>4 <field0> 555 from <table_name>
where <field1>8<condition_2alue1> and
<field0>8<condition_2alue0>555;
or if you want to select all !elds 1ust type = #asterisk$
#E&EC from <table_name> where
<field1>8<condition_2alue1> and<field0>8<condition_2alue0>555;
43
S"# * d
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 44/105
yS"# *ommands• 5ieing , Searching or record6s7
B: Searching for a particular record: )3
#E&EC `name`4`address` :R$/ `sample1`
9-ERE `name` 8 6marco elias palic 2alencia6
A! `address` 8 6porac4 pampan7a6;
or
#E&EC :R$/ `sample1` 9-ERE `name` 8
6marco elias palic 2alencia6 A! `address` 8
6porac4 pampan7a6;
44
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 45/105
Name of Book 45
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 46/105
Securing the Initial MySQLAccounts
• Deleting the Anonymous %ser Account
m'sl> !E&EE :R$/ m'sl5user 9-ERE *ser 8 66;
m'sl> :&*#- %R3,3&EE#;
• Assigning a +assword to the &oot Accountm'sl> *%!AE m'sl5user #E %assword 8 %A##9$R!(6newpwd 6)
=> 9-ERE *ser 8 6root6;
m'sl> :&*#- %R3,3&EE#;
• 4he password assigned to the rootaccount and other user accounts is casesensiti"e
46
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 47/105
.reating %sers
• A pro8y is someone or something that actsor performs a request for another person
• .reate a separate account for each Web
application that needs to access adatabase
• %se a RA statement to create useraccounts and assign pri"ileges
• 9rivileges are the operations that a usercan perform with a database
47
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 48/105
.reating %sers #continued$
48
Table 8-2 $ommon #y%&' database priileges
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 49/105
RA Statement
• 4he synta) for the RA statement is3RA privilege (column)? 4 privilege (columns)?? 555
$ @table 5 database5B
$ user 3!E3:3E! " 6 password 6?;
• 4he RA statement creates the useraccountif it does not e)ist and assigns the speci!edpri"ileges
• If the user account already e)ists* theRA statement 1ust updates the pri"ileges
49
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 50/105
&e"oking +ri"ileges
• 4he synta) for the RE,$DE statement is3RE,$DE privilege (column)? 4 privilege (columns)?? 555
$ @table 5 database5B
:R$/ user ;
• 4he RE,$DE A&& %R3,3&EE# statementremo"es all pri"ileges from a useraccount for a speci!ed table or database
• >ou must be logged in with the root
account or ha"e su0cient pri"ileges tore"oke pri"ilegesfrom another user account
50
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 51/105
Deleting %sers
• 4o delete a user3 – &e"oke all pri"ileges assigned to the user
accountfor all databases
– %se the RE,$DE A&& %R3,3&EE# statement – ?iew the pri"ileges assigned to a user account
with the #-$9 RA# :$R user statement
• 4o delete an e)isting user* use the !R$% *#ER statement
• %se the !R$% *#ER user statement todelete the account from the user table inthe m'sl database
51
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 52/105
Specifying @ield Data 4ypes
52
Table 8-3 $ommon #y%&' data types
S if i @i ld D t 4
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 53/105
Specifying @ield Data 4ypes#continued$
53
Table 8-3 $ommon #y%&' data types (continued)
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 54/105
Sorting Query &esults
• %se the $R!ER " keyword with the#E&EC statement to perform analphanumeric sort of the results
returned from a querym'sl> #E&EC mae4 model :R$/ in2entor' $R!ER " mae4 model;
• 4o perform a re"erse sort* add the !E#C keyword after the name of the !eld by
whichyou want to perform the sortm'sl> #E&EC mae4 model :R$/ in2entor' $R!ER " mae !E#C4
model;
54
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 55/105
@iltering Query &esults
• 4he criteria portion of the #E&EC statement determines which !elds toretrie"e from a table
• >ou can also specify which records to returnby using the 9-ERE keywordm'sl> #E&EC :R$/ in2entor' 9-ERE mae86/artin6;
• %se the keywords A! and $R to specify
more detailed conditions about the recordsyou want to returnm'sl> #E&EC :R$/ in2entor' 9-ERE mae869ashburn6
=> A! price<F..;
55
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 56/105
.hapter
Manipulating MySQLDatabases with +B+
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 57/105
+B+ ,"er"iew
• +B+ has the ability to access andmanipulate any database that is ,D/.compliant
• +B+ includes functionality that allowsyou to work directly with diCerent typesof databases* without going through,D/.
• +B+ supports SQLite* databaseabstraction layer functions* and +A& D/
57
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 58/105
nabling MySQL Support in +B+
• ,n %<I7Linu) systems3 – .on!gure +B+ to use the m'sli e)tension
by specifying the ==with=m'sli parameterwhen you run the confi7ure commandduring installation
• ,n Windows3 – .opy the !les libm'sl5dll andphp_m'sli5dll to the installation directory
– dit the php5ini con!guration !le andenable the eGtension8php_m'sli5dll directi"e
58
, i d .l i M SQL
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 59/105
,pening and .losing a MySQL.onnection
• ,pen a connection to a MySQL databaseser"er with the m'sli_connect() function
• 4he m'sli_connect() function returns a
positi"e integer if it connects to thedatabase successfully or false if it does not
• Assign the return "alue from them'sli_connect() function to a "ariable
that you can use to access the database inyour script
59
, i d .l i M SQL
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 60/105
,pening and .losing a MySQL.onnection #continued$
• 4he synta) for the m'sli_connect() function is3
Hconnection 8 m'sli_connect(IhostI4 Iuser I4 I password I4 IdatabaseI?)
=use isam5sis5pitt5edu for host and 'our acct username+psword
• 4he host argument speci!es the host namewhere your MySQL database ser"er isinstalled
• 4he user and password arguments specify a
MySQL account name and password• 4he database argument selects a database
with which to work
60
, i d .l i M SQL
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 61/105
,pening and .losing a MySQL.onnection #continued$
+able :-; yS"# server inormation unctions
61
, i d .l i M SQL
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 62/105
,pening and .losing a MySQL.onnection #continued$
Figure :-; yS"#)no<php in a $ebbroser
62
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 63/105
Selecting a Database
• Select a database with the usedatabase statement when you log onto the MySQL Monitor
• 4he synta) for them'sli_select_db() function is3
m'sli_select_db(connection4 database)
• 4he function returns a "alue of true if
it successfully selects a database orfalse if itdoes not
63
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 64/105
Bandling MySQL rrors
• &easons for not connecting to adatabase ser"er include3
– 4he database ser"er is not running
– Insu0cient pri"ileges to access the datasource
– In"alid username and7or password
64
Bandling MySQL rrors
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 65/105
Bandling MySQL rrors#continued$
• Make sure you are using a "alidusername and password
65
Figure *-2 +atabase connection error message
Suppressing rrors with the rror
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 66/105
Suppressing rrors with the rror.ontrol ,perator
• Writing code that anticipates andhandles potential problems is oftencalled bulletproong
• /ulletproo!ng techniques include3 – ?alidating submitted form data
– %sing the error control operator 6=7
to suppress error messages
66
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 67/105
4erminating Script )ecution
• 4he die() and eGit() functionsterminate script e)ecution
• 4he die() "ersion is usually used when
attempting to access a data source• /oth functions accept a single string
argument
• .all the die() and eGit() functions as
separate statements or by appendingeither function to an e)pression with the$r operator
67
4erminating Script )ecution
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 68/105
4erminating Script )ecution#continued$
H!"Connect 8 Jm'sli_connect(IlocalhostI4 IrootI4 IparisI);
if (KH!"Connect)
die("<p>The database server is not available.</p>");
echo I<p>#uccessfull' connected to the database ser2er5<+p>I;
H!"#elect 8 Jm'sli_select_db(H!"Connect4 Ifli7htlo7I);if (KH!"#elect)
die("<p>The database is not available.</p>");
echo I<p>#uccessfull' opened the database5<+p>I;
++ additional statements that access the database
m'sli_close(H!"Connect);
68
4erminating Script )ecution
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 69/105
4erminating Script )ecution#continued$
H!"Connect 8 Jm'sli_connect(IlocalhostI4 Idon7osselinI4
IrosebudI)
Or die("<p>The database server is not available.</p>");
echo I<p>#uccessfull' connected to the database ser2er5<+p>I;
Jm'sli_select_db(H!"Connect4 Ifli7htlo7I)Or die("<p>The database is not available.</p>");
echo I<p>#uccessfull' opened the database5<+p>I;
++ additional statements that access the database server
m'sli_close(H!"Connect);
69
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 70/105
&eporting MySQL rrors
+able :-> yS"# error reporting unctions
70
&eporting MySQL rrors
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 71/105
&eporting MySQL rrors#continued$
H*ser 8 H_E6username6?;
H%assword 8 H_E6password6?;
H!"Connect 8 Jm'sli_connect(IlocalhostI4 H*ser4 H%assword)
Or die("<p>Unable to connect to the database server.</p>"
. "<p>Error code " . mysqliconnecterrno(). "! " . mysqliconnecterror()) . "</p>";
echo I<p>#uccessfull' connected to the database ser2er5<+p>I;
Jm'sli_select_db(H!"Connect4 Ifli7htlo7I)
$r die(I<p>he database is not a2ailable5<+p>I);
echo I<p>#uccessfull' opened the database5<+p>I;
++ additional statements that access the database
m'sli_close(H!"Connect);
71
&eporting MySQL rrors
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 72/105
&eporting MySQL rrors#continued$
72
Figure *-! Error number and message generated by
an inalid username and password
&eporting MySQL rrors
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 73/105
&eporting MySQL rrors#continued$
H*ser 8 H_E6username6?;
H%assword 8 H_E6password6?;
H!"Connect 8 Jm'sli_connect(IlocalhostI4 H*ser4 H%assword)
$r die(I<p>*nable to connect to the database ser2er5<+p>I
5 I<p>Error code I 5 m'sli_connect_errno()
5 IL I 5 m'sli_connect_error()) 5 I<+p>I;
echo I<p>#uccessfull' connected to the database ser2er5<+p>I;
Jm'sli_select_db(H!"Connect4 Ifli7htplanI)
Or die("<p>Unable to select the database.</p>"
. "<p>Error code " . mysqlierrno(#$%onnect)
. "! " . mysqlierror(#$%onnect)) . "</p>";echo I<p>#uccessfull' opened the database5<+p>I;
++ additional statements that access the database
m'sli_close(H!"Connect);
73
&eporting MySQL rrors
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 74/105
&eporting MySQL rrors#continued$
74
Figure *-" Error code and message generated when
attempting to select a database that does not e,ist
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 75/105
)ecuting SQL Statements
• %se the m'sli_uer'() function to sendSQL statements to MySQL
• 4he synta) for the m'sli_uer'() function is3 m'sli_uer'(connection4 query )
• 4he m'sli_uer'() function returnsone of three "alues3 –
@or SQL statements that do not return results#CREAE !AA"A#E and CREAE A"&E statements$ it returns a "alue of true if thestatement e)ecutes successfully
75
)ecuting SQL Statements
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 76/105
)ecuting SQL Statements#continued$
– @or SQL statements that return results##E&EC and #-$9 statements$ them'sli_uer'() function returns a resultpointer that represents the query results
• A result pointer is a special type of "ariablethat refers to the currently selected row in aresultset
– 4he m'sli_uer'() function returns a
"alue of false for any SQL statements thatfail* regardless of whether they returnresults
76
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 77/105
Working with Query &esults
+able :-? *ommon 9@9 unctions or accessingdatabase results
77
&etrie"ing &ecords into an
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 78/105
&etrie"ing &ecords into anInde)ed Array
• 4he m'sli_fetch_row() functionreturns the !elds in the current row of aresultset into an inde)ed array andmo"es the result pointer to the ne)t row
echo I<table width861..MN border8616>I;
echo I<tr><th>/ae<+th><th>/odel<+th>
<th>%rice<+th><th>Ouantit'<+th><+tr>I;
&o' mysqlietchro'(*+ery&es+lt);do @
echo I<tr><td>@&o',-B<+td>I;
echo I<td>@&o',B<+td>I;echo I<td ali7n86ri7ht6>@&o',0B<+td>I;echo I<td ali7n86ri7ht6>@&o',1B<+td><+tr>I;&o' mysqlietchro'(*+ery&es+lt);B while (HRow);
78
&etrie"ing &ecords into an
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 79/105
&etrie"ing &ecords into anInde)ed Array #continued$
79
Figure *- Output o the inentory table in a .eb browser
&etrie"ing &ecords into an
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 80/105
&etrie"ing &ecords into anAssociati"e Array
• 4he m'sli_fetch_assoc() functionreturns the !elds in the current row of aresultset into an associati"e array andmo"es the result pointer to the ne)t row
• 4he diCerence betweenm'sli_fetch_assoc() andm'sli_fetch_row() is that instead ofreturning the !elds into an inde)ed array*
the m'sli_fetch_assoc() functionreturns the !elds into an associate arrayand uses each !eld name as the array key
80
Accessing Query &esult
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 81/105
Accessing Query &esultInformation
• 4he m'sli_num_rows() functionreturns the number of rows in aquery result
• 4he m'sli_num_fields() functionreturns the number of !elds in aquery result
• /oth functions accept a databaseconnection "ariable as an argument
81
Accessing Query &esult Information
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 82/105
Accessing Query &esult Information#continued$
H#O&strin7 8 I#E&EC :R$/ in2entor'I;HOuer'Result 8 Jm'sli_uer'(H!"Connect4 H#O&strin7)
$r die(I<p>*nable to eGecute the uer'5<+p>I
5 I<p>Error code P 5 m'sli_errno(H!"Connect)
5 IL I 5 m'sli_error(H!"Connect)) 5 I<+p>I;
echo I<p>#uccessfull' eGecuted the uer'5<+p>I;
2+m&o's mysqlin+mro's(*+ery&es+lt);2+m3ields mysqlin+mields(*+ery&es+lt);i (2+m&o's 4 - 55 2+m3ields 4 -)
echo "<p>6o+r q+ery ret+rned 7 . mysqlin+mro's(*+ery&es+lt) . 7 ro's and "
. mysqlin+mields(*+ery&es+lt) . 7 ields.</p>";elseecho "<p>6o+r q+ery ret+rned no res+lts.</p>";
m'sli_close(H!"Connect);
82
Accessing Query &esult Information
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 83/105
Accessing Query &esult Information#continued$
83
Figure *-8 Output o the number o rows and ields
returned rom a /uery
.l i Q & lt
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 84/105
.losing Query &esults
• When you are !nished working withquery results retrie"ed with them'sli_uer'() function* use the
m'sli_free_result() function toclose the resultset
• 4o close the resultset* pass to the
m'sli_free_result() function the"ariable containing the result pointerfrom the m'sli_uer'() function
84
. ti d D l ti D t b
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 85/105
.reating and Deleting Databases
• %se the CREAE !AA"A#E statementwith the m'sli_uer'() function tocreate a new database
8*9strin: "%&ETE #T$8E realestate";
HOuer'Result 8 Jm'sli_uer'(H!"Connect4 H#O&strin7)
$r die(I<p>*nable to eGecute the uer'5<+p>I
5 I<p>Error code I 5 m'sli_errno(H!"Connect)
5 IL I 5 m'sli_error(H!"Connect)) 5 I<+p>I;echo I<p>#uccessfull' eGecuted the uer'5<+p>I;
m'sli_close(H!"Connect);
85
.reating and Deleting Databases
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 86/105
.reating and Deleting Databases#continued$
86
Figure *-* Error code and message that prints when you attempt
to create a database that already e,ists
.reating and Deleting Databases
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 87/105
.reating and Deleting Databases#continued$
• %se the m'sli_db_select() function tocheck whether a database e)ists before youcreate or delete it
• 4o use a new database* you must select it bye)ecuting the m'sli_select_db() function
• Deleting a database is almost identical tocreating one* e)cept use the !R$% !AA"A#E
statement instead of the CREAE !AA"A#E statement with the m'sli_uer'() function
87
.reating and Deleting Databases
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 88/105
.reating and Deleting Databases#continued$
H!"ame 8 Ireal_estateI;
555
if (JKm'sli_select_db(H!"Connect4 H!"ame))
echo I<p>he H!"ame database does not eGistK<+p>I;
else @
8*9strin: "#&O #T$8E #$2ame";HOuer'Result 8 Jm'sli_uer'(H!"Connect4 H#O&strin7)
$r die(I<p>*nable to eGecute the uer'5<+p>I
5 I<p>Error code P 5 m'sli_errno(H!"Connect)
5 IL P 5 m'sli_error(H!"Connect)) 5 I<+p>I;
echo I<p>#uccessfull' deleted the database5<+p>I;
B
m'sli_close(H!"Connect);
88
. ti d D l ti 4 bl
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 89/105
.reating and Deleting 4ables
• 4o create a table* use the CREAE A"&E statement with the m'sli_uer'() function
• )ecute the m'sli_select_db() functionbefore e)ecuting the CREAE A"&E statement or the new table might be createdin the wrong database
• 4o pre"ent code from attempting to create atable that already e)ists* use am'sli_uer'() function that attempts toselect records from the table
89
.reating and Deleting 4ables
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 90/105
g g#continued$
H!"ame 8 Ireal_estateI;
555
8*9strin: "%&ETE T$9E commercial (city =&%&(0?)@ state
=&%&(0?)@ saleorlease =&%&(0?)@ typeo+se =&%&(A-)@
rice B2T@ siCe B2T)";
HOuer'Result 8 Jm'sli_uer'(H!"Connect4 H#O&strin7)
$r die(I<p>*nable to eGecute the uer'5<+p>I
5 I<p>Error code I 5 m'sli_errno(H!"Connect)
5 IL I 5 m'sli_error(H!"Connect)) 5 I<+p>I;
echo I<p>#uccessfull' created the table5<+p>I;
m'sli_close(H!"Connect);
90
.reating and Deleting 4ables
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 91/105
g g#continued$
91
Figure *-11 Error code and message that prints when you
attempt to create a table that already e,ists
Adding* Deleting* and %pdating
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 92/105
g* g* p g&ecords
• 4o add records to a table* use the3#ER and ,A&*E# keywords withthe m'sli_uer'() function
• 4he "alues entered in the ,A&*E# listmust be in the same order in whichyou de!ned the table !elds
• >ou must specify *&& in any !eldsfor which you do not ha"e a "alue
92
Adding* Deleting* and %pdating
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 93/105
g* g* p g&ecords #continued$
• 4o add multiple records to a database*use the &$A! !AA statement and them'sli_uer'() function with a local
te)t !le containing the records youwant to add
• 4o update records in a table* use the
*%!AE* #E* and 9-ERE keywordswith the m'sli_uer'() function
93
Adding* Deleting* and %pdating
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 94/105
g* g* p g&ecords #continued$
• 4he *%!AE keyword speci!es the name ofthe table to update
• 4he #E keyword speci!es the "alue to
assign to the !elds in the records thatmatch the condition in the 9-ERE keyword
• 4o delete records in a table* use the!E&EE and 9-ERE keywords with the
m'sli_uer'() function• 4he 9-ERE keyword determines which
records to delete in the table
94
%sing the
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 95/105
gm'sli_affected_rows() @unction
• With queries that return results ##E&EC queries$* use the m'sli_num_rows() function to !nd the number of records
returned from the query• With queries that modify tables but do
not return results #3#ER* *%!AE* and!E&EE queries$* use them'sli_affected_rows() function todetermine the number of aCected rows
95
%sing the m'sli affected rows()
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 96/105
g ' _ _
@unction #continued$
8*9strin: "U#TE inventory 8ET price1D.0-
FE&E maGeH3enderH 2# modelH#IJH";
HOuer'Result 8 Jm'sli_uer'(H!"Connect4 H#O&strin7)
$r die(I<p>*nable to eGecute the uer'5<+p>I5 I<p>Error code I 5 m'sli_errno(H!"Connect)
5 IL I 5 m'sli_error(H!"Connect)) 5 I<+p>I;
echo "<p>8+ccess+lly +pdated "
. mysqliaectedro's(#$%onnect) . " record(s).</p>";
96
%sing the m'sli affected rows()
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 97/105
g ' _ _
@unction #continued$
97
Figure *-1 Output o mysqliaectedro's() unction
or an U#TE /uery
%sing the m'sli info()
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 98/105
g ' _
@unction
• @or queries that add or update records* oraltera table5s structure* use the m'sli_info() function to return information about the query
• 4he m'sli_info() function returns thenumber of operations for "arious types ofactions* depending on the type of query
• 4he m'sli_info() function returnsinformation about the last query that wase)ecuted on the database connection
98
%sing the m'sli info() @unction
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 99/105
g ' _
#continued$
• 4he m'sli_info() function returnsinformation about queries that matchone of the following formats3 – 3#ER 3$555#E&EC555
– 3#ER 3$555,A&*E# (555)4(555)4(555)
– &$A! !AA 3:3&E 555
– A&ER A"&E 555
– *%!AE
• @or any queries that do not match oneof these formats* the m'sli_info() function returns an empty string
99
%sing the m'sli info() @unction
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 100/105
g _
#continued$
8*9strin: "B28E&T B2TO inventory
=9UE8(HOvationH@ HJJJ 9K 9e:endH@ -AL.--@ 0)@
(HOvationH@ HD 8tandard $alladeerH@ DLL.--@ )@
(HOvationH@ HTan:ent 8eries T1?JH@ ?DL.--@ 1)";
HOuer'Result 8 Jm'sli_uer'(H!"Connect4 H#O&strin7)$r die(I<p>*nable to eGecute the uer'5<+p>I
5 I<p>Error code P 5 m'sli_errno(H!"Connect)
5 IL I 5 m'sli_error(H!"Connect)) 5 I<+p>I;
echo I<p>#uccessfull' added the records5<+p>I;
echo "<p>" . mysqliino(#$%onnect) . "</p>";
100
%sing the m'sli info() @unction
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 101/105
_
#continued$
101
Figure *-10 Output o mysqliino() unction or anB28E&T /uery that adds multiple records
%sing the m'sli info() @unction
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 102/105
_
#continued$
• 4he m'sli_info() function alsoreturns information for &$A! !AA queries
8*9strin: "9O# #T 9O%9 B23B9E Hc!/temp/inventory.tMtHB2TO T$9E inventory;";
HOuer'Result 8 Jm'sli_uer'(H!"Connect4 H#O&strin7)
$r die(I<p>*nable to eGecute the uer'5<+p>I
5 I<p>Error code P 5 m'sli_errno(H!"Connect)
5 IL I 5 m'sli_error(H!"Connect)) 5 I<+p>I;
echo I<p>#uccessfull' added the records5<+p>I;
echo "<p>" . mysqliino(#$%onnect) . "</p>";
102
%sing the m'sli_info() @unction
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 103/105
_
#continued$
103
Figure *-18 Output o mysqliino() unction or a
9O# #T /uery
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 104/105
Name of Book 104
EFphp
7/21/2019 Working with Database.ppt
http://slidepdf.com/reader/full/working-with-databaseppt 105/105
Gconnection: Hmysqlconnect#JlocalhostJ*JrootJ*JJ$K
mysqlselectdb#JmydbJ*Gconnection:$K
F;
Ehtml;
Ehead;
E7head;
Ebody;Ecenter;