0% found this document useful (0 votes)
61 views

PHP Info

The document provides an introduction to connecting and interacting with a MySQL database using PHP. It discusses connecting to the database, creating tables, executing queries using SQL statements, and other basic MySQL operations. Key points covered include using mysql_connect() to connect, mysql_select() to select a database, and mysql_query() to execute queries and return result sets from the database.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

PHP Info

The document provides an introduction to connecting and interacting with a MySQL database using PHP. It discusses connecting to the database, creating tables, executing queries using SQL statements, and other basic MySQL operations. Key points covered include using mysql_connect() to connect, mysql_select() to select a database, and mysql_query() to execute queries and return result sets from the database.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

ei e 

   (Connect)

o
o

  (Create)

   (Insert)

 k (Select)

Where Clause

o
o

ORDER BY Keyword
  (Update)

  (Delete)

ei e
i e  u 
 
| PHP MySQL I nt r oduct i on
  :
   

e  ei   !   ie u   #      ,


    , 
   i$    
% PHP $    $ MySQL Database '
o  e) uk
 la  award winning CMS(content
management System) ei combination $ %       MySQL e  ! !  -     

   i
      e . o e
-  o    a  - %   
MySQL % c e

  s    $  o1  1ei 
   
 k % 1 #      

e

 % c row e column e d  4   56 #$ #  e  
- 7 %  #   e
u   e
 m   m k 
 % e

 #    
"Employees", "Products", "Customers" e "Orders".
DATABASE TABLES
e

 a  
  1 4 e p $

  e
7 6   #   "Customers" "Orders"
i$ 
   #$ % row(records) #   e e

 e u % 1 %     "Persons"
LastName
Hansen
Svendson
Pettersen
u  
  3) record
and City).

FirstName
Ola
Tove
Kari

Address
Timoteivn 10
Borgvn 23
Storgt 20

 (p $ $ k $ e
) e 4)  

City
Sandnes
Sandnes
Stavanger

 (LastName, FirstName, Address,

QUERI ES
e
Query % c e
p=  e
a  > (Question or Request)
MySQL  1    
 e
query       7 6    #$ $e '   e
' '  4 1
   e e
 1 1 %
1.SELECT LastName FROM Persons

ei  1 ) Persons table #  LastName column )  k      ' '  4 1  


LastName
Hansen
Svendson
Pettersen


ie  u .
/ ( MySQL Connect Tut or i al i n Bangl a)
  :
 1  

MySQL Database #  
  % 

 a-$i

  MySQL Database e #   (connect) % 

% ei  ei  - ei!    %1


 7
1.mysql_connect(servername,username,password);

Parameter

7

servername

e
 7 -   ? IP address  ? mu
 host name   MySQL server
'
o1$ 
 (>  mu
 )       i  ei MySQL server
(web server software)  #   %   servername %  localhost)

username

e  'l !$  % c ? iu     ei !7  p   

password

e  'l !$  % c

o  $  
    e  i - tB7 c   C1    ei   e
' -  call  %1
 e
e
  return(output) 
d Mysql_connect' -
o  -
s  %o1   e
 m  return(output)      
 -

h()  o  e  MySQL e #  k %o1   ) 
01.<?php
02.$con = @mysql_connect("localhost","root","");
03.if (!$con)
04.{
05.echo ('Could not connect: ' . mysql_error());
06.exit();

07.}
08.echo "Connected";
09.// some code
10.?>

**ei  ) .php extension  1  !   localhost e htdocs e   e    


e  e
 tB7  % c Mysql_connect ' -
e
  (

 connection
identifier )return(output)  
$con variable e store  #   e   e
ei script e $ % 
%1  % MySQL e
  '
o1$   i   1 e %     !7 ) 
o1 7   i     a#
iu     o1 7    Mysql_connect ' -
 connection identifier p    e

false return  
e   u   3 )    $ %   % 1  e  % c1.@ h:
h Mysql_connect ' -
  k   $ #7 %1  %  e
a  >$  p-7  e    ei
h
(error suppression operator) 1 i  %  ' -)    c $ #7 % o      $ #7 %   

 
      
2.!(negation operator):ei h

        a#7     1  if($con) e a#7 

 - s  %1  e B 7 ! h) 1  e a#7 %   - s   %1


3.exit ei ' -
    ei J n   1 ? 1 1i   e
 %1   $ %    '  ei
J n       - %      -  i     %  ei  L  J 1  ! , i  ?

8 98 n ;
:
;
:
 - a
)  n % 1    ei script -M %1e$ ei ' -) $ N %1
view source
print?
01.<?php
02.$con = mysql_connect("localhost","root","");
03.if (!$con)
04.{
05.die('Could not connect: ' . mysql_error());
06.}
07.
08.// some code

09.
10.mysql_close($con);
11.?>

PHP = 


 >7;? ;
( Cr eat e)
  :
 1  

e   - s  % 1      % ? 


    1   >  i e

       
      Participants e e
   k   % ,-> e
' -   i  k % 1  
1.mysql_select(Participants,$con);

k$   $con variable ) !  n connection identifier ) (connection identifier e m 7 
)u
1    % 1 )  e 
 1i  ' -
   c     -
$ %    %ei
$  

a- ,  o %1, k t ' -
7 - M   -
  % 1 
 identifier %  > 
SQL QUERY PHP e;
AB 
C
8

PHP  1 MySQL e Query  Q   e


 n % c mysql_query ei ' -)
Mysql_query(query,connection_id)

 >7;? ;
PHP =
MySQL e  n s
n  1 
    %1    e o ? s
n  1   %1 -> e
 k -  % 
 !  PHP e  >$  e
 Q  %1  u %  !  my_db   e

    %1  1 %
01.<?php
02.$con = mysql_connect("localhost","rej","abc123");
03.if (!$con)
04.{
05.die('Could not connect: ' . mysql_error());
06.}
07.
08.if (mysql_query("CREATE DATABASE my_db",$con))
09.{
10.echo "Database created";
11.}
12.else
13.{
14.echo "Error creating database: " . mysql_error();
15.}
16.
17.mysql_close($con);
18.?>

** http://localhost/phpmyadmin/ ei  -   1 Privileges
$  k   
    e  o1 7 7 

        rej  1    password abc123


PHP = e 
TABLE >7;? ;

>  

   
i e

     %      )   '
01.<?php
02.$con = mysql_connect("localhost","rej","abc123");
03.if (!$con)
04.{
05.die('Could not connect: ' . mysql_error());
06.}
07.
08.// Create database
09.if (mysql_query("CREATE DATABASE my_db",$con))
10.{
11.echo "Database created";
12.}
13.else
14.{
15.echo "Error creating database: " . mysql_error();
16.}
17.
18.// Create table
19.mysql_select_db("my_db", $con);
20.$sql = "CREATE TABLE Persons
21.(
22.FirstName varchar(15),
23.LastName varchar(15),
24.Age int
25.)";
26.
27.// Execute query
28.mysql_query($sql,$con);
29.
30.mysql_close($con);
31.?>

e

 a-$i  k   %  mysql_select_db() ei  n)  1
 e

  'l
i varchar   a-$i ' l X7$ 7 6 (:varchar(15))    % 
p) 
  e
p i   #  
p i   > 

i m % 1 #  e   AUTO_INCREMENT ' )    '  p   

 7     # #i 1     i Yd    
p i       null  %1  i not null  ) ) ' l   % 

ei i8
D u
;
 ( PHP MySQL I nser t Tut or i al i n Bangl a)
  :
   

INSERT INTO statement d     7 


      ei  ei s
n eku
  $
mysql_query ' -) $ N %
01.<?php
02.$con = mysql_connect("localhost","rej","abc123");
03.if (!$con)
04.{
05.die('Could not connect: ' . mysql_error());
06.}
07.
08.mysql_select_db("my_db", $con);
09.
10.mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
11.VALUES ('Peter', 'Griffin', '35')");
12.
13.mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
14.VALUES ('Glenn', 'Quagmire', '33')");
15.
16.mysql_close($con);
17.?>

F D G 
8 
 HI
8
(

8
( DATA I NSERT FROM FORM
FORM)
e e
ei)ee '7     #  
  "persons" table e     7    
01.<html>
02.<body>
03.
04.<form action="insert.php" method="post">
05.Firstname: <input type="text" name="firstname" />
06.Lastname: <input type="text" name="lastname" />
07.Age: <input type="text" name="age" />
08.<input type="submit" />
09.</form>
10.
11.</body>
12.</html>

 iu  

 k    
insert.php     ei insert.php 
  #      
e PHP e $_POST !  e  >$  
    '7 # 
insert.php page
e % 
01.<?php
02.$con = mysql_connect("localhost","rej","abc123");
03.if (!$con)
04.{
05.die('Could not connect: ' . mysql_error());
06.}

07.
08.mysql_select_db("my_db", $con);
09.
10.$sql="INSERT INTO Persons (FirstName, LastName, Age)
11.VALUES
12.('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
13.
14.if (!mysql_query($sql,$con))
15.{
16.die('Error: ' . mysql_error());
17.}
18.echo "1 record added";
19.
20.mysql_close($con)
21.?>


  k ( PHP MySQL Sel ect Tut or i al i n Bangl a)
  :
 1  

SELECT Statement ) $ %    
 #  
 k   1    u %) persons table e s

 k  
01.<?php
02.$con = mysql_connect("localhost","rej","abc123");
03.if (!$con)
04.{
05.die('Could not connect: ' . mysql_error());
06.}
07.
08.mysql_select_db("my_db", $con);
09.
10.$result = mysql_query("SELECT * FROM Persons");
11.
12.while($row = mysql_fetch_array($result))

13.{
14.echo $row['FirstName'] . " " . $row['LastName'];
15.echo "<br />";
16.}
17.
18.mysql_close($con);
19.?>

u  u %  mysql_query() function  
'N  Q   $result variable k % 1 # 
OuI

Peter
Glenn Quagmire
OuI HTML TABLE e =P
8

   ) u  i ei 
 k   n u


   
01.<?php
02.$con = mysql_connect("localhost","rej","abc123");
03.if (!$con)
04.{
05.die('Could not connect: ' . mysql_error());
06.}
07.
08.mysql_select_db("my_db", $con);
09.
10.$result = mysql_query("SELECT * FROM Persons");
11.
12.echo <b>"</b><table border='1'><b>
13.<</b>tr>
14.<th>Firstname</th>

Griffin

15.<th>Lastname</th>
16.</tr>";
17.
18.while($row = mysql_fetch_array($result))
19.{
20.echo "<tr>";
21.echo "<td>" . $row['FirstName'] . "</td>";
22.echo "<td>" . $row['LastName'] . "</td>";
23.echo "</tr>";
24.}
25.echo "</table>";
26.
27.mysql_close($con);
28.?>

OuI
Firstname Lastname
Glenn

Quagmire

Peter

Griffin

PHP MySQL Where Clause = 


=P

  :
 1  


  a  J %1  %  Where Clause  1 
 #      
   
  u %) persons table e    (rows)  k    i "FirstName='Peter' 
e  a-$i mysql_query() function ) $ %    %   execute %o1  $  e) MySQL e  
 1   n  Q 
01.<?php

02.$con = mysql_connect("localhost","rej","abc123");
03.if (!$con)
04.{
05.die('Could not connect: ' . mysql_error());
06.}
07.
08.mysql_select_db("my_db", $con);
09.
10.$result = mysql_query("SELECT * FROM Persons
11.WHERE FirstName='Peter'");
12.
13.while($row = mysql_fetch_array($result))
14.{
15.echo $row['FirstName'] . " " . $row['LastName'];
16.echo "<br />";
17.}
18.?>

u


Peter Griffin

ORDER BY  o
D u
;
 ( PHP My SQL Or der BY Keywor d Tut or i al
i n Bangl a)
  :
 1  

ORDER BY Keyword  1 
 %1 i  'l e

 ascending order e 1
  u %   ) persons table e  
 1 a 1 
01.<?php
02.$con = mysql_connect("localhost","rej","abc123");
03.if (!$con)
04.{
05.die('Could not connect: ' . mysql_error());
06.}
07.
08.mysql_select_db("my_db", $con);
09.
10.$result = mysql_query("SELECT * FROM Persons ORDER BY age");
11.
12.while($row = mysql_fetch_array($result))
13.{
14.echo $row['FirstName'];
15.echo " " . $row['LastName'];
16.echo " " . $row['Age'];
17.echo "<br />";

18.}
19.
20.mysql_close($con);
21.?>

u


Glenn
Peter Griffin 35

Quagmire

33


O u
;
(
u
;
( PHP MySQL Dat a Updat e Tut or i al i n Bangl a)
  :
   

Update Statement )   
  
   
  u %  persons table e 
 
(  ) %  
01.<?php
02.$con = mysql_connect("localhost","rej","abc123");
03.if (!$con)
04.{
05.die('Could not connect: ' . mysql_error());
06.}
07.
08.mysql_select_db("my_db", $con);
09.
10.mysql_query("UPDATE Persons SET Age = '36'
11.WHERE FirstName = 'Peter' AND LastName = 'Griffin'");
12.
13.mysql_close($con);
14.?>

 )u
   person   
)   %      e
FirstName
Peter
Glenn
u     
     %  e

LastName
Griffin
Quagmire

Age
35
33

FirstName
Peter
Glenn

LastName
Griffin
Quagmire

Age
36
33

 u   where clause $ %   %   e    7 6 e



  


%   i 
   -  #   % 


%  e
    
i


 
W
( Dat abase Del et e Tut or i al i n Bangl a)
  :
 o  

DELETE Statement   
  
 #  
  '  
  u %  persons table e 
   (delete) %  
01.<?php
02.$con = mysql_connect("localhost","rej","abc123");
03.if (!$con)
04.{
05.die('Could not connect: ' . mysql_error());
06.}
07.
08.mysql_select_db("my_db", $con);
09.
10.mysql_query("DELETE FROM Persons WHERE LastName='Griffin'");
11.
12.mysql_close($con);
13.?>

p#  persons table ) e 


FirstName

LastName

Age

Peter

Griffin

35

Glenn

Quagmire

33

FirstName

LastName

Age

Glenn

Quagmire

33

 
   e % 1 

 u   where clause $ %   %   e    7 6 e



  
 
%  e
    
i
 
%   i  
   -  #   % 

You might also like