0% found this document useful (0 votes)
93 views16 pages

PHP NOTE PART-3 (Working With Databases-1)

This document provides an introduction to using MySQL databases with PHP. It discusses how to connect to a MySQL server from PHP, create databases and tables, insert and select data. Key MySQL functions for PHP are listed that allow interacting with MySQL, such as mysqli_connect() to open a connection, and mysqli_query() to send queries.

Uploaded by

kiru haran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views16 pages

PHP NOTE PART-3 (Working With Databases-1)

This document provides an introduction to using MySQL databases with PHP. It discusses how to connect to a MySQL server from PHP, create databases and tables, insert and select data. Key MySQL functions for PHP are listed that allow interacting with MySQL, such as mysqli_connect() to open a connection, and mysqli_query() to send queries.

Uploaded by

kiru haran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Noking ktth Datahases:

antaoductfbn do HYsqL

Many 4 the appcatfons that aweb devdope

uants to se ca b made easnby the. uba e

Standandljed database tD s t p Ongarize and acess

unyoimatfon
MYSQL an epen souiCe Stuchired
&uey Language (sq-) database ha ast, varlabe
se and sultable to appiRcotton e

any stze ,

Show Databases ahd, CTeate databas Commands:

Fst heed Create t e neu database

check he Cueert databases to make Sne a

database ethat hame doesn' alreedy eaist

and vetfy the


hem Create he neu ne '

eristence o the nec dotabas2,

MY36L> SHoWN DATABR SES

Databae

myso
test

2 1ows set (o-08 sec)


These
These databases alseady eAsts MysQL - mysal
s a database that MysqL uses fan itenal
adminfatsatton, and test ib a datababe pa test

poseb.
xDatabase tablas ana stoTad inside databas6,

Cveate database . ou
So the fist step so a

mtght usa this database dto Contain yoamaston


about vadlous its and vegetables, fan eample,

and so can Create a databasa t named, say4

Ptoduce. You Can create the pnoduce databasa


with the CREATE DATABASF aRmmandd the

MYsQL monitan

Mysal > CREATE DATABASE poduces


uery ok, 1taö affected (0.ol sec)

F SHON DA TABRSES

Database
maysa
test

Pacdute

3 rows h t CO.oR sec)


mysa> USE potoduuce

Database ehanged

*Ae thee any tmbles n ts hew databas2?


You Can use the SHON TABLES
Command check
that Out

mysg> SHoN TABLES


Epty set (O.ol sec)

ThEs nesponse - " Empdty s e t - tells o u that his

database doesn't Contaln any ables yale

*SaL Com mands and Sub commands, ane

Case nsensttive.

The name the database ana Case

6ersitfve.

A A database a containen Ho aelated

tables
o s o w S eaeh
a colectfon
*A able
ecoad each a e c o r d
holding data fo ne
NCws
alled falds.
Contairing foamatfon
CREATING a New Table:

Each tables uithin the database mus

be dened and Created.Thks done uuih the

CREATE TRBLE Command.

*Mysal needs o houw what end ok data


Can be stoned h telodls

Mysal CREATE TABLE fuult hame VARCHAR(20),

hLumben VARCHAR(20)

a1ows affected o. 12 sec)


u e ek

Msal > SHow TABLES3

Tables-h- pacduce

1ow h set (o.00 sec)

mysal DE3CRI BE fuát

Freld Type key Deyaut Extra

Tname yachan(10) YES NoLL


AJWLL
numbenIvanchan(2o)ES

9 ows En set lo,ol sec)


Puting Pata aito the New Database :

Mpa INSERT TNTO VALUE ( apples', 'o30'),

Dovangs', 2329'), C bananas', R2


Ok QuO, 3 nOUs afpected (0.00 see)

To viet the table data uwe Use the

fallowing Command,

msal> SELECT FROM fault

name nuumbe

aPples o90

0anges 3329

bananas 8582

3 910ws in set (o.00 sec),


hNonkêng uth Database

PHP access to a databas2 ten done


two tml doument one Collect a us

equest, fos a database ace ss and he

|host the PHP Code


Ptocess the equest and

genenate tha Netun htme dcument


The 21equest coleat s a
stmple
htm document

Connecttng o mYsL and selectfng a Database -

The PHP fanctfo mysL Connet a scitpt

a mys4L sven.

function takes Three Panameres


This
all uhteh ae aptfonalL.

8 ntac

C"localhost ", Ub2n name , 'pasnrd")


MupaR Connect

localhost The host that nneng mysqL the

- detautt s local host.

mYsQL
ben name the Usenname

Passunnd The pabsu0nd ba the database.

xhe Database n PHP:


ACessing
PHP consfst
* The MYSQL &uppot

can cal
functon ou
a umben o
they ae stedl
and
Sntenac uith MYSQ

belouo table.
nthee
FUNCTIONS MEANINGS

musqalMected.- nows Get the umben ow


aHected b the paevtous
MYS4L openation.

muyeal- change -usen ehange the Jogged- in usen

msal-ellent encodêng Retun the hame the


CLuolent chanacten set.

elose a MYS4L connection


usa_elose

nluysal-Connect open a Conhectfon to a

MYSQL senven.

Msal_ceate-db Create a My6L database

m o - d a t a . saek seek data n the database.

mysal db_ name Get the name Othe2

database

mukal-db-quy sond a MysqL quwy.

mysl.donop db Dnop aMYsQL database.

Pnysal- ewmOn Petun Ahe text o the

eonan hmessage faom the

Petous MYsL apenatton

Fetch a neult OW as an asso. a a


mysafetch_awat both..
anwmete anay On
mu-qeteh, -assoc Fetch a nesut i0u as ann assotattve
ovay
mu-fateh- now Get a neeut oou an enumenated
anay
mya-fied en Ratuin the length o a frar tlald.

muysal-tald name GGet the hame a t h e gBvan teld.

n a aesult

mysal- eld- seek seek ta a gtren Reld oe4set.

table the
mua-teld- table 4et the name o the

% n.
e n eld

mya-ield_typa Get the dype a the ven e d

a aesult,

Gat HYs9L seN no.


mu-g0b_servenine

alout the most necert


msino Get inoamafion
guey.
musa-Itet_dbs Lst databases avallable n a wsa

Seye.
mupo- Ist-ffelds |kst MvsQL table ffelds

a MYSQL detabas«
Muysal s t -tables tst the tables

telds n nesut.|
mya-num-ftelds Get the numben o

the humben a ows in nesult


ua-nuwm- iows Get

Mupo-peennett Open a pesîstent Conecton to a

MYSQL savG.

send a MYsL 9uey


MAys-aesult Get zesult data.

MLysaleelect.db 3eleck a mysaL database

ys-tablenamc qet the table hame a fteld.

PHP 3UpPORTED DATABAsES

Adabas Ingses Onace


dease TntenBase OvRtmos
Empress Piont Bae
Postgresq
File Pro myeL 8altd

Hypesuwave N-SQL Sybase


Direct

IBm De2 MYSL Yelous

Inoni opBC Untx dbm

Example for connecttng to the Database seveN:

42 php
Connectfon mysqlconnet ("Mocalhost voot, ***

dfe couldn't Connect


do seven ) ;

97
o the Database:
Connectfng

ane ected to MYSqL the next


Okaywe
step s to Select the database you uwant to use.

yntan:

mysselectdb (databate_name E, 1?nk identtptnI)

Example
Php
*m** *
GConnectioh msalL Connect (C "localhost", "ioot ",
Oa dee Ccouldh't Conneck *o seven )33

Sdb - msal- select_db Poduce " $connectfon)

adfe"Couldn'E select the datatose);

411
Readtngthe Table and Displaytng
and dlsplay the
do o1ead
The gcal
can send xhat
shat
named ut. You
database table
the allowtng functfon
uncton,
ustng
SNea an sQL ueny

Syntan

uuny ( u L E ,
Ttnk - identf4ten )
mys
Example paogram

<hemdz
head 4Rtte7 Dbplayng tables with MveaL 21tHez
lhead
Abody
hi> Dtsplayfng tables with MYsQL4th1z

phP
connetfen iysql-connect C"'localhost", "root ", ***")
co dfe C"cannot Connect to the senyn )3

$dbmys9k,selett-db CPioduce" $connecon)


n dfeC"Cannat elect h e database ") 3
"

Sa SELECT*FROM fuult

$esult
mysal-quexy ($quvy) on dle ('quy falled:.|
meso evon)5
echo table bondan = 'i 'z "3
echo

echo th7 Name <1th7 {thz Numbn <lth7


echo ltn"
wele (4sow = mupal_feteh_ aoay (4Hesuut))

ech tn">
echo td>", 41ow [ name'], 'ztd7<tdY , trouo[ numbei
cltcd"
ech len7"3

echo ttane7"3

msaelose C¢connectton )
lbody <lhtml7
Output
dRsplay tables. php
D%playing dables ulth MY44L
Name Numbe
apples
0rans 332
bananas 8582

UPdatina Databases:

*Let.we update the tabledata. in php.


*o update bananas lcoO,
Now uwe ase 9oing
Example :

42Php
Beonnedtfon =Msal.connectC"loealhost ",noot", "*** **
ea dae C"annot comect do the seave")s
db mysal- select-db ("paodugtce)connectfon)
on dte C"Cannot select he, database")
NHERE
$u upDATE fult SET numben
==lovoo
hame bananas
aa dte ( 'Quyfailed:
$aesult =
mysal_quuy C$quuy)
mysgl-ek10 C)3

quy SELET FROM fuult 3

8nesult mysal. quy ($quy) on dfe C"quey fafle: "


mysol. voN COD3
echo <tahe torden '>"2

echo "ta> "s

echo th Name th >th7 Numben4 1th7 3


echo ts17"
hfle CAow mysql- {eteh-amay (4aesult ) )

echo t7
echo td>", znouo['name'J, Zltd7ktd>, $row['numbia'],
ltd'

echo ttnz's

echo table
mysot elose ($tonnectfon)
7

Output

vpdatetable iphp
Name Numben

apples.

Caanges 3329
looo
bananas
Tnsentng ito tahes new data:

Let ub insert new data n t the dtoble

PHP. Ne are qotng do hsert manges =2000

Example
4Php
onectfon = mysal-Connect ( "Jocalhest" noot", ****")
on die C "Couldnt Conneck, o the seven");s
db mysgk- select-db "Paduce "; ¢eonnecfon)
0 dfe "Gouldn't selec the database")3

VALUES
4iyz INSERT INTO fult name, numben)
"

'mangos ', 3000>


neult= mysakuy ( 4quny)
OA d i e ( " q u e y falled " m u s a l -e v a n ¬ ) )s

SELECT fsuuit "3


4u FROM

nesult -
mysal-9uny C$uuny)
0n dte C"Quy falled mysal-evoa419
echo table bonden''z'3

echo "t7"
Numben <l&h7
echo Ztn7 Name hh7 <th7

Whtle ($ow =mysal-Jetch-avay ($nesult))


echo<tn7"
<Itd7<td7, rowt 'nunber'J,
h o td7 "» $nou [ 'hame'J,

echottx7"
3
echo Ztable7
mMusalelose ( $connectfen)
97
utput:

@inswtdatafntotable-ph
Name Nurbenr
apples L020

0nangts 3329 EAC i,


bananas

Mangos

Delefng Reconds

the econd, rom the tabe


Let delete
n PHP We aregaing to delete apPles » l020

Example
4Php
"*****
fconnectfon = mjsconneet ("ocalhest", 2opt",
Cn dle (Camot onnect to the&nYELSE ") 3
Seven
Jdb mgal.select-dbProduce, $eonnectfon
091 die'annot slect he database ") 3

1uny DELETE FROM fruit NHERE name =apples


esutmysalLquy($uny) cndtel quwy failkd: "
y- SELECT* FrOM ult"

esult mysglquwy (4quy) eon


die Cquy fatld: ".
mysal-evnoxi):
echo table banden '7'5

echo tn7 "


Numben <lth7
echo "th7 Name 4lth7 <th7

echo <1ta7 "3

ukle (3now = mysol- fetch-aonay (4aesult))

echo AA7"
'kHd>{td7, $row['nuumben'J,
echo td>, rowl 'name'],

echo ta7's

echo Ktable";
mysok close (3tonnectfon)s

7
Output:

@deletedatafromtable. php
Name Numbea

Onangs 3329

bananas lobb

3ooo
Mangos

You might also like