Вы находитесь на странице: 1из 31

PHP MySQL

(kachanov@ogs.gomel.by)


(vvtk@stealthcomp.com)
(http://web.stealthcomp.com)


(mine@convex.ru)
(http://exper.ural.ru)

1.1 ( 16.05.2000)

PHP MySQL


........................................................................................................................................ 2
............................................................................................................................. 2
PHP? ............................................................................................................................................ 3
PHP ................................................................................................................................................... 4
PHP .............................................................................................................................................. 4
PHP?............................................................................................................................... 5
PHP....................................................................................................................................................... 5
PHP?......................................................................................................................................................... 6
PHP? .............................................................................................................................................. 6

MySQL? ...................................................................................................................................... 6
MySQL ............................................................................................................................................. 6

PHP..................................................................................................................................... 7
PHP- .................................................................................................................................... 7
PHP.............................................................................................................................................. 9
HTML-............................................................................................................................ 9
.................................................................................................................................................. 11
MySQL ( ) ............................................................................................. 13
MySQL ( ). II .............................................................................. 15
MySQL ( ). III............................................................................. 17
........................................................................................................................................ 18
MySQL ( ) ....................................................................................... 20
................................................................................................................................................. 26
................................................................................................................................... 26
..................................................................................................... 27


,
, -,
Web- ,
PHP MySQL.
PHP MySQL. ,
,
. .
PHP
MySQL. , .
.


PHP (www.php.net) MySQL (www.mysql.com).
:

PHP3 Manual

PHP MySQL

http://www.php.net/manual/ - HTML- -

http://www.php.net/distributions/manual.zip -

http://www.php.net/distributions/manual_a-l.pdf , http://www.php.net/distributions/manual_m-x.pdf PDF- ( 2 )

http://www.php.net/docs.php3 -

http://www.webclub.ru/materials/php/ - PHP2 (,
)

MySQL manual
o

http://www.mysql.com/doc.html - MySQL,
. .

(- ) http://www.webreview.com
http://www.devshed.com.

PHP?
PHP (server scripting language), HTML,
.
PHP HTML. .. :

. 1
, "" , -. ,
, , ,
( .phtml .php3, ) (
) . PHP-
HTML-,
, . , .html/.htm Web-
- .
CGI-, , Perl C , CGI HTML-, , PHP HTML-, ( <?php ?>).
PHP JavaScript/Jscript/VBScript,
. , PHP- ,
, JavaScript-
.
MS Internet Information Server , PHP Active Server Pages (ASP),
Java , PHP Java Server Pages (JSP). PHP
ColdFusion (www.allaire.com) embPerl. , Web, HTML-. ,
, Web- , .
PHP . web- php:
PHP MySQL

<html>
<head>
<title></title>
</head>
<body>
<?php echo ", PHP-!"; ?>
</body>
</html>

. 2
,

, PHP-!
. 3
.
<html>
<head>
<title>Example</title>
</head>
<body>
, PHP-!
</body>
</html>

. 4
, PHP-. , ,
PHP , .

PHP
PHP , CGI. :
, , (cookies).
PHP (databases), Web-
.
:
Adabas D

InterBase

Solid

dBase

mSQL

Sybase

Empress

MySQL

Velocis

FilePro

Oracle

Unix dbm

Informix

PostgreSQL

ODBC

PHP IMAP, SNMP, NNTP, POP3 HTTP,


(sockets) .

PHP
PHP 1994 , Rasmus Lerdorf
.
PHP MySQL

1995 Personal Home Page Tools ( PHP).


.
1995 , PHP/FI Version 2. FI
Rasmusa, (Form Interpritator). PHP/FI
Apache API Apache. PHP
CGI , . PHP
Perl, CGI-.
(, MySQL Oracle). GD ,
gif- . PHP/FI.
1997 Zeev Suraski Andi Gutmans ,
. , 6 1998
, PHP 3.
1999 PHP 3 . NetCraft 1999
PHP 1 . .
( 2000) PHP 4,
( Zend - www.zend.com). ,
, .
RC1 RC2 . , , .

PHP?
Web- , web- -
.
: , , ..
CGI-, Perl. , CGI-
. CGI-, ,
. PHP
Web-, ASP Microsoft ColdFusion Allaire.
PHP C Perl. , ,
PHP.
/ .
, PHP, . Perl- PHP-
.
( MySQL
) PHP-MySQL -. , ,
Windows, , Unix. , PHP
CGI-, , , web Apache IIS.
, ,
, - .
, ,
, .

PHP
1.

PHP 3, , PHP
. , , PHP 3

( PHP 3, ,
Zeev Suraski).
PHP 4, , , .

2.

PHP , , ,
. , , -,
PHP, ,
CGI-, .

PHP MySQL

3.

, , , PAN Perl.
. PHP 4 , CPAN, ,
.

4.

(session), , , ASP. PHP 4 .

PHP?
, PHP, www.php3.net. PHP
UNIX- Win32.
4- PHP Zend www.zend.com.

PHP?

MySQL?
MySQL , . MySQL
, .
MySQL TcX ,
. , MySQL 1996 40 ,
10,000 , 500 7 .
MySQL .
. Unix-,
, .
Windows, MySQL Windows NT Windows 95/98.
MySQL , 3.22 .
MySQL- .
, 190 EUR.

MySQL
MySQL SQL ANSI 92,
, .
MySQL.
1.

, .

2.

50 .

3.

. MySQL .

4.

MySQL ,
. MySQL :
1.

, SELECT * FROM table1 WHERE id IN (SELECT id FROM table2).


, 3.23.

2.

. LOCK/UNLOCK TABLE.

3.

(foreign) .

4.

5.

(VIEW). 3.23 .

PHP MySQL

2-4 .
. Web,
.

PHP
PHP-
, . ,
, ,
. ,
PHP- , Web-, ,
, .php3.
PHP-.
test.php3 :
<html>
<body>
<?php
$myvar = "Hello, World";
echo $myvar;
?>
</body>
</html>

. 5
URL , : http://stage/test.php3.
:

Hello, World
. 6
, , ,
, , , .
:
<html>
<body>
Hello, World
</body>
</html>

. 7
, PHP- , PHP-,
, Web- .
, PHP-! HTML-
. ,
.
, ,
PHP MySQL

. - ,
, , .

. , PHP-.
, , . ,
<?php. PHP- ,
. ?>. <?php ?>
. , , PHP- Web ,
, "". PHP , PHP-
HTML-.
, . <?php ?>
<? ?>.
, ASP, <%
%>. PHP , .
:
<SCRIPT LANGUAGE="PHP">

<SCRIPT>

. 8
, , - .
"", . -
PHP- , .
, ,
, . ,
.
, , myvar $ (). PHP,
. ( "=") "Hello, World" $myvar.
.
$.
PHP . , ,
- . PHP ,
700 . ,
.
. echo,
, , , , , .
PHP-. ,
HTML-. .html ( .htm) .php3.
Web- ,
PHP-. <html><body> . </body></html> PHP-.
, <?php ?>. ,
. 2. , HTML- PHP-,
:
<?php
echo "<html>";
echo "<body>";
$myvar = "Hello World";
echo $myvar;
echo "</body>";
echo "</html>";

PHP MySQL

?>

. 9
. , - . PHP . PHP-
<html>, <body>, </body>, </html>, ?
. , PHP .
.

PHP
PHP phpinfo().
PHP-, , .
:
<html>
<body>
<?php
phpinfo();
?>
</body>
</html>

. 10
: " ,
". ,
PHP-, Web-, .
, MySQL. MySQL, .
, - PHP MySQL.
, , , , ,
.

HTML-
Web-, . : print echo:
<?php
print "Hello, world.";
?>

<?php
echo "Hello, world.";
?>

. 11
Print , . "print" ";"
, . , , .
, "Hello, World" . print.php3
:
PHP MySQL

<html>
<body>
<?php
print " print.";
print "<p>";
echo " echo.",
" ",
"P.S. .",
" ",
" .";
print "<p>";
printf (" printf.");
print "<p>";
printf (" printf .");
print "<p>";
printf (" , printf.");
?>
</html>
</body>

. 12
:

print.
echo. P.S. .
.
printf.
printf .
, printf.
. 13
print .
echo , print, ,
, .
printf , ,
, printf .
:

echo

printf

PHP MySQL

10

print


, PHP , HTML.
HTML HTML-
(GET POST).
. ,
, . , , .
, :
<FORM NAME="_"
ACTION="__"
METHOD="__">
...
</FORM>

, . ,
"". , .
NAME, .
: GET POST. ,
GET URL, ACTION.
:
http://site.domain/action.php3?=&...=
"=" , NAME.
POST .
, -, . ,
"" .
PHP ,
.
, :
<FORM ACTION="mult.php3" METHOD="GET">
<INPUT TYPE="text" NAME="first" SIZE="4" MAXLENGTH="4">
<INPUT TYPE="text" NAME="second" SIZE="4" MAXLENGTH="4">
<INPUT TYPE="Submit" VALUE="">
</FORM>

, mult.php3 :
<?php
Header("Content-type: text/html");
echo "$first $second ", $first*$second;
?>

, .
, HIDDEN. ,
, , NAME, . ,
, , .
, .
PHP MySQL

11

, .

TEXT

SELECT

RADIO

-. .

CHECKBOX

-. .

SUBMIT

, .

IMAGE

. SUBMIT

<TEXTAREA> .
, .
TEXT - . : = (
, GET).
$.
SELECT - VALUE <OPTION>. <SELECT>
:
<SELECT NAME="mySelect">
<OPTION VALUE="test1">test1</OPTION>
<OPTION VALUE="test2">test2</OPTION>
<OPTION VALUE="test3">test3</OPTION>
</SELECT>

C mySelect=test1, .
: $mySelect.
<SELECT> MULTIPLE, .
<SELECT> : [].
: []=&[]=...,
, $.
, VALUE, , <OPTION>
</OPTION>.
RADIO - VALUE, TEXT.
, . , on
CHECKBOX - , on, ,
. , ,
$ "on". TEXT.
SUBMIT - SUBMIT, , .
, . VALUE.
TEXT.
IMAGE - . : .x .y,
.
: .x=&.y=.
$_x $_y.
<TEXTAREA> - TEXT.
. ,
- "_" "%"
PHP MySQL

12

. "+".
.
PHP .
. :
<FORM NAME="testForm" ACTION="test.php3">
name: <INPUT TYPE="text" NAME="personal[name]"><BR>
e-mail: <INPUT TYPE="text" NAME="personal[email]"><BR>
<INPUT TYPE="SUBMIT">
</FORM>

, :
$personal["name"] $personal["email"].
, PHP <?php_track_vars?>, , ,
$HTTP_GET_VARS $HTTP_POST_VARS,

MySQL ( )
SQL .
. MySQL, :
mysql > CREATE DATABASE mydb;
mysql> CREATE TABLE employees
( id tinyint(4) DEFAULT '0' NOT NULL AUTO_INCREMENT,
first varchar(20), last varchar(20), address varchar(255),
position varchar(50), PRIMARY KEY (id), UNIQUE id (id));
INSERT INTO employees VALUES
(1,'Bob','Smith','128 Here St, Cityname','Marketing Manager');
INSERT INTO employees VALUES
(2,'John','Roberts','45 There St ,Townville','Telephonist');
INSERT INTO employees VALUES
(3,'Brad','Johnson','1/34 Nowhere Blvd, Snowston','Doorman');

. 14
mydb. employees ().
. PHP MySQL
.
HTML-. MySQL PHP
.
int mysql_connect(string hostname, string username, string password);
MySQL.
:
Hostname , .
PHP MySQL

13

Username .
Password .
int, 0, , 0
.
int mysql_select_db(string database_name, int link_identifier);
.
:
Database_name .
link_identifier ID , mysql_connect. ( ,
, ID mysql_connect)
true false
int mysql_query(string query, int link_identifier);
.
:
query ,
link_identifier . .
ID 0, .
int mysql_result(int result, int i, column);
column i.
int mysql_close(int link_identifier);
MySQL.
:
link_identifier . .
true false
.php3 :
<html>
<body>
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
$result = mysql_query("SELECT * FROM employees",$db);
printf("First Name: %s<br>\n", mysql_result($result,0,"first"));
printf("Last Name: %s<br>\n", mysql_result($result,0,"last"));
printf("Address: %s<br>\n", mysql_result($result,0,"address"));
printf("Position: %s<br>\n", mysql_result($result,0,"position"));
mysql_close($db);
?>
</body>

PHP MySQL

14

</html>

. 15
, . mysql_connect()
MySQL. (localhost),
, (root), , (
).
localhost , MySQL , Web- PHP. ( ) MySQL,
.
.
.
, $db.
(. ).
, (
"" ). mysql_select_db().
,
.
mysql_select_db() true false.
true, false. , -
,
false, . , ,
.
, SQL.
mysql_query(). , ""
, mysql_connect().
mysql_query() , -
$result.
, mysql_result() (..
$result), - ( 0),
( ). 0 2, ,
.

printf(), .
, - Perl C, printf .
, "%s" ,
, printf.
" ".
printf() .
PHP.
, mysql_result(),
mysql_fetch_row() mysql_fetch_array(),
, , mysql_result().

MySQL ( ). II
, .
:
<html>
<body>
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);

PHP MySQL

15

$result = mysql_query("SELECT * FROM employees",$db);


echo "<table border=1>\n";
echo "<tr><td>Name</td><td>Position</tr>\n";
while ($myrow = mysql_fetch_row($result))
{
printf("<tr><td>%s %s</td><td>%s</td></tr>\n", $myrow[1], $myrow[2], $myrow[3]);
}
echo "</table>\n";
?>
</body>
</html>

. 16
, .
while(). , , $result
, mysql_fetch_row
$myrow. , "{}".
.
"". mysql_query
, $result. ,
:

$result =

id

first

last

address

position

Bob

Smith

128 Here St, Cityname

Marketing Manager

John

Roberts

45 There St ,Townville

Telephonist

Brad

Johnson

1/34 Nowhere Blvd, Snowston

Doorman

$result . , .
0 2. 5 0 4. ,
, 0- 2-.
mysql_fetch_row ( " ").
mysql_fetch_row $result. ,
$myrow .
mysql_fetch_row, , ,
. mysql_fetch_row false, ,
.
- .
$myrow. , $result, , .
:

$myrow =

id

first

last

address

position

Bob

Smith

128 Here St, Cityname



$results

Marketing Manager

$myrow :
0

id

first

last

address

position

PHP MySQL



16

$results
$myrow =

John

Roberts

45 There St ,Townville

Telephonist

, :

$myrow =

id

first

last

address

position

Brad

Johnson

1/34 Nowhere Blvd, Snowston



$results

Doorman

$myrow ,
. , , $myrow[1] "Bob", $myrow[4] "
Telephonist".
, .
, 0, 1,
, .
, HTML printf() ,
.
: ,
, . , . .

MySQL ( ). III
:
<html>
<body>
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
$result = mysql_query("SELECT * FROM employees",$db);
if ($myrow = mysql_fetch_array($result))
{
echo "<table border=1>\n";
echo "<tr><td>Name</td><td>Position</td></tr>\n";
do
{
printf("<tr><td>%s %s</td><td>%s</tr>\n", $myrow["first"], $myrow["last"],
$myrow["address"]);
}
while ($myrow = mysql_fetch_array($result));
echo "</table>\n";
}
else
{
echo "Sorry, no records were found!";
}
?>
</body>

PHP MySQL

17

</html>

. 17
, . -,
mysql_fetch_row() mysql_fetch_array(). ,
mysql_fetch_row() :
, . ,
$myrow[1] (1 ), $myrow["first"] ("first"
). .
, do/while if-else.
if-else , $myrow, ,
, , else
. , , SQL- "SELECT * FROM
employees WHERE id=6" - , .
do/while while(), .
do/while . if
$myrow.
(.. while ($myrow = mysql_fetch_row($result)), ,
. do/while ,
. , .
if-else . ,
.


. ,
. , GET ( "
"). . ,
.
<a href="http://my_machine/mypage.php3?id=1">

. 18
.
, . .
.
<html>
<body>
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
$result = mysql_query("SELECT * FROM employees",$db);
if ($myrow = mysql_fetch_array($result))
{
do
{
printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF,
$myrow["last"]);

$myrow["id"],$myrow["first"],

}
while ($myrow = mysql_fetch_array($result));
}

PHP MySQL

18

else
{
echo "Sorry, no records were found!";
}
?>
</body>
</html>

. 19
, printf(). , . -,
. PHP-, ,
, , .
, , PHP
.
, $PHP_SELF. URL
. ,
. , ,
, . if-else
, .
, , , .
$PHP_SELF , ,
.
, , ,
: . ,
-, PHP-, ,
.
name=value . ,
$id, . :
<html>
<body>
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
// display individual record
if ($id)
{
$result = mysql_query("SELECT * FROM employees WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
printf("First name: %s\n<br>", $myrow["first"]);
printf("Last name: %s\n<br>", $myrow["last"]);
printf("Address: %s\n<br>", $myrow["address"]);
printf("Position: %s\n<br>", $myrow["position"]);
}
else
{
// show employee list
$result = mysql_query("SELECT * FROM employees",$db);
if ($myrow = mysql_fetch_array($result))
{

PHP MySQL

19

// display list if there are records to display


do
{
printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF, $myrow["id"],
$myrow["first"], $myrow["last"]);
}
while ($myrow = mysql_fetch_array($result));
}
else
{
// no records to display
echo "Sorry, no records were found!";
}
}
?>
</body>
</html>

. 20
, , .
//. ,
/* */.
, , PHP-, MySQL.
.

MySQL ( )
.
. PHP . :
<html>
<body>
<form method="post" action="<?php echo $PHP_SELF?>">
First name:<input type="Text" name="first"><br>
Last name:<input type="Text" name="last"><br>
Address:<input type="Text" name="address"><br>
Position:<input type="Text" name="position"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
</body>
</html>

. 21
, $PHP_SELF. , PHP-
HTML. ,
. -, , ,
, .
Submit. , ,
$submit. , , ,
.
PHP MySQL

20

, ,
. , , . ,
, .
, , , .
, , ,
$HTTP_POST_VARS. .
, , $GLOBALS.
<html>
<body>
<?php
if ($submit) {
// process form
while (list($name, $value) = each($HTTP_POST_VARS)) {
echo "$name = $value<br>\n";
}
} else{
// display form
?>
<form method="post" action="<?php echo $PHP_SELF?>">
First name:<input type="Text" name="first"><br>
Last name:<input type="Text" name="last"><br>
Address:<input type="Text" name="address"><br>
Position:<input type="Text" name="position"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php
} // end if
?>
</body>
</html>

. 22
, .
.
<html>
<body>
<?php
if ($submit) {
// process form
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
$sql = "INSERT INTO employees (first,last,address,position) VALUES
('$first','$last','$address','$position')";
$result = mysql_query($sql);
echo "Thank you! Information entered.\n";
} else{
// display form

PHP MySQL

21

?>
<form method="post" action="<?php echo $PHP_SELF?>">
First name:<input type="Text" name="first"><br>
Last name:<input type="Text" name="last"><br>
Address:<input type="Text" name="address"><br>
Position:<input type="Text" name="position"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php
}
// end if
?>
</body>
</html>

. 23
. . ,
- , ? ,
? . .
SQL- ($sql),
mysql_query(). . -
, SQL- , .
, . ,
. . :
, .
, .
, .
. :
<html>
<body>
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
if ($id) {
// query the DB
$sql = "SELECT * FROM employees WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>
<form method="post" action="<?php echo $PHP_SELF?>">
<input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
First name:<input type="Text" name="first" value="<?php echo $myrow["first"] ?>"><br>
Last name:<input type="Text" name="last" value="<?php echo $myrow["last"] ?>"><br>
Address:<input type="Text" name="address" value="<?php echo $myrow["address"] ?>"><br>
Position:<input type="Text" name="position" value="<?php echo $myrow["position"] ?>"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php

PHP MySQL

22

} else {
// display list of employees
$result = mysql_query("SELECT * FROM employees",$db);
while ($myrow = mysql_fetch_array($result)) {
printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF, $myrow["id"], $myrow["first"],
$myrow["last"]);
}
}
?>
</body>
</html>

. 24
,
. .
. Submit,
, , .
SQL-.
<html>
<body>
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
if ($id) {
if ($submit) {
$sql = "UPDATE employees SET first='$first',last='$last',address='$address',position='$position'
WHERE id=$id";
$result = mysql_query($sql);
echo "Thank you! Information updated.\n";
} else {
// query the DB
$sql = "SELECT * FROM employees WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>
<form method="post" action="<?php echo $PHP_SELF?>">
<input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
First name:<input type="Text" name="first" value="<?php echo $myrow["first"] ?>"><br>
Last name:<input type="Text" name="last" value="<?php echo $myrow["last"] ?>"><br>
Address:<input type="Text" name="address" value="<?php echo $myrow["address"] ?>"><br>
Position:<input type="Text" name="position" value="<?php echo $myrow["position"] ?>"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php
}
} else {
// display list of employees
$result = mysql_query("SELECT * FROM employees",$db);

PHP MySQL

23

while ($myrow = mysql_fetch_array($result)) {


printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF, $myrow["id"], $myrow["first"],
$myrow["last"]);
}
}
?>
</body>
</html>

. 25
. , . ,
if() if() .
- PHP-.
<html>
<body>
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
if ($submit) {
// here if no ID then adding else we're editing
if ($id) {
$sql = "UPDATE employees SET first='$first',last='$last',address='$address',position='$position'
WHERE id=$id";
} else {
$sql = "INSERT INTO employees (first,last,address,position) VALUES
('$first','$last','$address','$position')";
}
// run SQL against the DB
$result = mysql_query($sql);
echo "Record updated/edited!<p>";
} elseif ($delete) {
// delete a record
$sql = "DELETE FROM employees WHERE id=$id";
$result = mysql_query($sql);
echo "$sql Record deleted!<p>";
} else {
// this part happens if we don't press submit
if (!$id) {
// print the list if there is not editing
$result = mysql_query("SELECT * FROM employees",$db);
while ($myrow = mysql_fetch_array($result)) {
printf("<a href=\"%s?id=%s\">%s %s</a> \n", $PHP_SELF, $myrow["id"], $myrow["first"],
$myrow["last"]);
printf("<a href=\"%s?id=%s&delete=yes\">(DELETE)</a><br>", $PHP_SELF, $myrow["id"]);
}
}
?>
<P>

PHP MySQL

24

<a href="<?php echo $PHP_SELF?>">ADD A RECORD</a>


<P>
<form method="post" action="<?php echo $PHP_SELF?>">
<?php
if ($id)
{
// editing so select a record
$sql = "SELECT * FROM employees WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$id = $myrow["id"];
$first = $myrow["first"];
$last = $myrow["last"];
$address = $myrow["address"];
$position = $myrow["position"];
// print the id for editing
?>
<input type=hidden name="id" value="<?php echo $id ?>">
<?php
}
?>
First name:<input type="Text" name="first" value="<?php echo $first ?>"><br>
Last name:<input type="Text" name="last" value="<?php echo $last ?>"><br>
Address:<input type="Text" name="address" value="<?php echo $address ?>"><br>
Position:<input type="Text" name="position" value="<?php echo $position ?>"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
<?php
}
?>
</body>
</html>

. 26
, . . if()
, Submit, , ,
$id. , .
.
, $delete. , . ,
if() , POST,
if() ,
GET.
, , :
. $id. ,
. .
, , . while() if(),
SQL - SELECT, INSERT, UPDATE, DELETE
, , URL
.
PHP MySQL

25


H:

echo "$var";

p:

echo $var;

print("Test");

p:

print "Test";

H:

echo"<a href=\"http://www.php.net\">PHP</a>";

p:

?><a href="http://www.php.net">PHP</a><?

H:

$a[0]=1; $a[1]=2; $a[2]=3;

p:

$a = array(1,2,3);

H:

if($a>1) { $b=2; } else { $b=3; }

p:

$b = ($a>1) ? 2:3;

H:

$result=mysql_query(...);

p:

$result=mysql_query(...) or die (mysql_error());


, PHP MySQL, .
, ,
. -, ,
, .
, . - Web-,
Web- . ,
, .
,
PHP-, .

PHP MySQL

26

. 27

. 28

. 27. . Web PHP, MySQL. msql_connect()


localhost, IP- .
:
IP- 226.117.45.2
IP- 226.117.45.7
PHP-,
msql_connect("226.117.45.7",$db);

, IP-.
PHP-,
.
, . 28, . ,
MySQL. msql_connect()
- IP- .
:
IP- : 226.117.45.2 ,
192.168.0.5
IP- 192.168.0.104 - ,
PHP-, :
msql_connect("192.168.0.104",$db);


HTML -.

PHP MySQL

27

<HTML>
<HEAD>
<TITLE> </TITLE>
<BODY>
<CENTER>
?
<P>
<TABLE WIDTH = 400><TR><TD align = right>
<FORM ACTION="email.php3" METHOD="POST">
:<BR>
<INPUT TYPE="text" NAME="name" SIZE="20" MAXLENGTH="30">
<P>
email:<BR>
<INPUT TYPE="text" NAME="email" SIZE="20" MAXLENGTH="30">
<P>
:
<SELECT NAME="preference">
<OPTION value = "">
<OPTION value = "">
</SELECT>
<P>
<INPUT TYPE="submit" VALUE=" !">
</FORM>
</TD></TR></TABLE></CENTER>
</BODY>
</HTML>

. 29
request.html. , email.php3.
:
<?
/* request.html */
PRINT "<CENTER>";
PRINT ", $name.";
PRINT "<BR><BR>";
PRINT " .<BR><BR>";
PRINT " $preference. email: $email.";
PRINT "</CENTER>";
?>

. 30
, request.html , email: vasya@pupkin.com
, , " !",
:

,
.
. email: vasya@pupkin.com
. 31
. email.
PHP MAIL.
void mail(string to, string subject, string message, string add_headers);
to email .
subject .
message .
add_headers ( ).
PHP MySQL

28

email.php3 :
<?
mail($email, " ", "$name\n
!\n
$preference\n
.
.\n
");
mail("administration@me.com",
" .",
"$name $preference\n
email-: $email. \n");
?>

. 32
.
.
, -
.
email.php3 :
<?
/* request.html */
/* */
$hostname
$username
$password
$dbName =

= "localhost";
= "myusername";
= "mypassword";
"products";

/* MySQL, */
$userstable = "clients";
/* email */
$adminaddress = "administration@me.com";
/* */
$db = mysql_connect($hostname,$username,$password) or die(" ");
mysql_select_db("$dbName",$db) or die(" ");
print
print
print
print
print
print

"<CENTER>";
", $name.";
"<BR><BR>";
" .<BR><BR>";
" $preference. email: $email.";
"</CENTER>";

/* email */
mail($email, " ", "$namen\n
!\n
$preference\n
.
.\n
");
mail("administration@me.com",
" .",
"$name $preference\n
email-: $email. \n");
/* */
$query = "INSERT INTO $userstable VALUES('$name','$email', '$preference')";
$result = mysql_query($query);
print " .";
/* */

PHP MySQL

29

mysql_close();
?>

. 33
,
MySQL.
,
( Apple Macintosh, Apple Macintosh . www.stealthcomp.com).
apple.php3
<?/* , , */
$hostname = "localhost";
$username = "myusername";
$password = "mypassword";
$dbName = "products";
/* MySQL, */
$userstable = "clients";
/* */
mysql_connect($hostname,$username,$password) or die(" ");
@mysql_select_db("$dbName") or die(" ");
/* - */
$query = "SELECT * FROM $userstable WHERE choice = ''";
$result = mysql_query($query);
/* */
$number = mysql_numrows($result);
/* */
$i = 0;
if ($number == 0)
{
print "<CENTER><P> </CENTER>";
}
elseif ($number > 0)
{
print "<CENTER><P> : $number<BR><BR>";
while ($i < $number)
{
$name = mysql_result($result,$i,"name");
$email = mysql_result($result,$i,"email");
print " $name .<BR>";
print " Email: $email.";
print "<BR><BR>";
$i++;
}
print "</CENTER>";
}
?>

. 34
:
int mysql_num_rows(int result);
:
result ID .
.

PHP MySQL

30

, .

E-mail: kachanov@ogs.gomel.by.

PHP MySQL

31

Вам также может понравиться