Σημειώσεις php/MySQL
Embed Size (px)
Transcript of Σημειώσεις php/MySQL
-
PHP/MySQL
1 26
HTML
HTML PHP (PHP code mode) :
1 2 3
echo (" editors, FrontPage, "); 4
(short) tags. short_tags(), short_open_tag configuration setting config PHP PHP --enable-short-tags option.
tags ASP asp_tags configuration setting. ASP-style tags 3.0.4. C Perl - (;) tag (?>) -, :
PHP C , Shell
PHP Comments
-
PHP/MySQL
2 26
1 : // ( ) 2 : /* */ ( block ) 3 : # comments , /
. (Variables) .
$.
=
letter _(underscore). +,-,&,* : $today=Monday; //(, string) $total=1000; //(number)
case-sensitive .. $total $Total .
: $total=10; $total=100;
, . php . . Variable Example
-
PHP/MySQL
3 26
PHP (runtime) PHP. , (cast) settype() . , . PHP Integer 8 Double 4.254 String Goodmorning Boolean true (true false) gettype() PHP . , gettype() , . gettype() .
-
PHP/MySQL
4 26
PHP settype() . settype(), , , . 3.14 ( double) . , , . settype() (cast) , .
-
PHP/MySQL
5 26
$holder=(string) $undecided; echo gettype($holder); echo " is $holder"; //integer $holder=(integer) $undecided; echo gettype($holder); echo " is $holder"; //double $holder=(double) $undecided; echo gettype($holder); echo " is $holder"; //boolean $holder=(boolean) $undecided; echo gettype($holder); echo " is $holder"; echo "original variable type: "; echo gettype($undecided); ?> =. : $name=George . + 7+3 10 - 7-3 4 * 7*3 21 / 7/3 2.333333333 % 7%3 1 (concatenation) . , . , "hello"." world" "hello world" .
-
PHP/MySQL
6 26
, PHP , . , . : $x=6; $x=$x+3; // x 9 : $x=6; $x+=3; // x 9 += $x+=3 $x=$x+3 -= $x-=3 $x=$x-3 *= $x*=3 $x=$x*3 /= $x/=3 $x=$x/3 %= $x%=3 $x=$x%3 .= $x.=test $x=$x.test 1. ; $this_is_a_value $___test___ $metritis $the counter $my-name $count $2test 2. ; $test_val=6.5466; settype($test_val, integer); echo $test_val; 3. ; echo gettype(8);
-
PHP/MySQL
7 26
scripts . script , . . scripts .
if if , PHP. . if (expr) statement , expr Boolean . expr TRUE, PHP , FALSE - . a b $a $b: . , if . , statement group. , a is bigger than b $a $b, $a $b: If if , .
else
, . ' else. else if if FALSE. , a is bigger than b $a $b, a is NOT bigger than b :
-
PHP/MySQL
8 26
elseif elseif, , if else. ' else, if if FALSE. , else, elseif TRUE. , a is bigger than b, a equal to b or a is smaller than b:
elseif if. elseif ( ) TRUE . PHP, 'else if' ( ) 'elseif' ( ). .
elseif if elseif FALSE, elseif TRUE.
== != -
=== > >= <
-
PHP/MySQL
9 26
|| Or () true or Or () true xor Xor ( true, ) && And () true and And () true ! Not () true 1. . = 2 = 4. . 2. script . . 3. script . 4. script a, b, c. max, . 5. 1. 0.7, 2. 0.5, 3. 0.4, 4. 0.8. . script 3.
6. , :
-
PHP/MySQL
10 26
800 3,5 . :
. . . . "" "" .
. .
. . . 0 2000 "" "".
switch switch . switch (expression) { case value1: statements; break; case value2: statements; break; default: statements; , case. break switch. , case. default, . ! break, case default. , . while while PHP. while () { } while true, . loop, , ( PHP loop ). , while
-
PHP/MySQL
11 26
FALSE , - . while while (): endwhile; dowhile dowhile while . while loops do..while loop ( ). do { while (); 1. script PHP lang fr, es, en, de, gr. (, , , , ). ! 2. while sript 7.
3. :
-
PHP/MySQL
12 26
1. 1 2 3 4 5 6 7 8 9 10 2. 2 4 6 8 10 12 14 16 18 20 3. 3 6 9 12 15 18 21 24 27 30 4. 4 8 12 16 20 24 28 32 36 40 5. 5 10 15 20 25 30 35 40 45 50 6. 6 12 18 24 30 36 42 48 54 60 7. 7 14 21 28 35 42 49 56 63 70 8. 8 16 24 32 40 48 56 64 72 80 9. 9 18 27 36 45 54 63 72 81 90 10. 10 20 30 40 50 60 70 80 90 100 printf %4d ( 4 ) %d.
4. script .
5. . : Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliet Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey Xray Yankee Zulu. 1-9 (*) . .
: :
Hello 2 you!.
:
Hotel Echo Lima Lima Oscar ** Yankee Oscar Uniform!
-
PHP/MySQL
13 26
for
for loops loops PHP. for loop :
for (expr1; expr2; expr3) statement
(expr1) loop.
, expr2 . TRUE, loop . FALSE, loop .
, expr3.
for (expr1; expr2; expr3) statement 1. script 2000 1-10 : 2000 . . 2. for.
-
PHP/MySQL
14 26
scripts - . HTML server. HTML PHP. HTML. Form.php Name: Address: Test.php Self.php
-
PHP/MySQL
15 26
$user=$_POST[USER]; $address=$_POST[ADDRESS]; if ($user){ echo "Welcome $user"; echo "Your address is:$address"; } ?>
-
PHP/MySQL
16 26
(Arrays) . . $name (, , ). . , . , (key) (value). , 0. , , . array(), , [ ].
array() .
[ ] ( ), .
$cars = array("Toyota", "Mercedes", "BMW", "Ferrari", "Renault"); $cars[] = "Toyota"; $cars[] = "Mercedes"; $cars[] = "BMW"; $cars[] = "Ferrari"; $cars[] = "Renault"; $cars[0] = "Toyota"; $cars[1] = "Mercedes"; $cars[2] = "BMW"; $cars[3] = "Ferrari"; $cars[4] = "Renault"; 1. ('Perl', 'PHP', 'C','Java', 'Pascal', 'Cobol', 'Visual Basic') . 2. (8, 12, 21, 3, 15, 6, 7, 32, 9) . 3. 2. 4. 2 . 5. 8 . , .
-
PHP/MySQL
17 26
6. , N! , . 7. php script N! (1*2*3*4*5.*). . 8. ! browser HTML.
-
PHP/MySQL
18 26
date()
string date ( string format [, int timestamp] )
string format string, integer timestamp timestamp.
timestamp Fri, 13 Dec 1901 20:45:54 GMT Tue, 19 Jan 2038 03:14:07 GMT. ( , , integer 32-bit). windows 01-01-1970 19-01-2038.
timestamp string, strtotime(). , timestamps ( UNIX_TIMESTAMP MySQL).
format string:
a - "am" "pm" A - "AM" "PM" B - Swatch Internet time d - , 2 ("01" "31"). D - , 3 . .. "Fri" F - , . .. "January" g - , 12 ("1" "12"). G - , 24 ("0" "23"). h - , 12 ("01" "12"). H - , 24 ("00" "23"). i - ("00" "59"). I ( i) - "1" , "0". j - ("1" "31"). l ( 'L') - , . .. "Friday" L - boolean ("0" "1"). m - ("01" "12"). M - , 3 . .. "Jan" n - ("1" "12"). O - Greenwich time . .. "+0200" r - RFC 822; .g. "Thu, 21 Dec 2000 16:01:07 +0200"
( PHP 4.0.4) s - ("00" "59") S - , 2 ("st", "nd",
"rd" "th"). t - ("28" "31"). T - ; .. "EST" "MDT" U - Unix Epoch (January 1 1970 00:00:00 GMT) w - , ("0" () to "6" ()). W - ISO-8601,
( PHP 4.1.0) Y - , 4 ; .. "1999"
-
PHP/MySQL
19 26
y - , 2 ; .. "99" z - ("0" "365"). Z - ("-43200" "43200").
UTC , UTC .
date()
echo date ("l dS of F Y h:i:s A"); echo "July 1, 2000 is on a " . date ("l", mktime(0,0,0,7,1,2000));
format string backslash . backslash , backslash.
escape date()
echo date("l \\t\h\e jS"); // prints something like 'Saturday the 8th'
date(). backslash , , PHP. escape , \n, .
date() Formatting
/* Today is March 10th, 2001, 5:16:18 pm */ $today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm $today = date("m.d.y"); // 03.10.01 $today = date("j, n, Y"); // 10, 3, 2001 $today = date("Ymd"); // 20010310 $today = date('h-i-s, j-m-y, it is w Day z '); // 05-16-17, 10-03-01, 1631 1618 6 Fripm01 $today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // It is the 10th day. $today = date("D M j G:i:s T Y"); // Sat Mar 10 15:16:08 MST 2001 $today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:17 m is month $today = date("H:i:s"); // 17:16:17
-
PHP/MySQL
20 26
strtotime() strtotime -- UNIX timestamp int strtotime ( string time [, int now] )
string UNIX timestamp now, . , -1 .
strtotime()
-
PHP/MySQL
21 26
PHP upload browser. upload , binary . PHP, upload upload .
upload :
Upload Upload Files File to Upload:
MAX_FILE_SIZE (hidden) input . byte.
MAX_FILE_SIZE browser. . browser ! PHP- , (maximum-size), . MAX_FILE_SIZE .
upload autoglobal $_FILES PHP 4.1.0. $HTTP_POST_FILES array PHP 4.0.0. upload. $_FILES .
$_FILES . userfile, .
$_FILES['userfile']['name']
client.
-
PHP/MySQL
22 26
$_FILES['userfile']['type']
mime type , browser . "image/gif".
$_FILES['userfile']['size']
, byte, upload.
$_FILES['userfile']['tmp_name']
upload server.
$_FILES['userfile']['error']
upload . ['error'] PHP 4.2.0
: PHP 4.1.0 $HTTP_POST_FILES autoglobal $_FILES. PHP 3 $HTTP_POST_FILES.
server, upload_tmp_dir directive php.ini. TMPDIR PHP. putenv() PHP script . upload, .
is_uploaded_file()
. is_uploaded_file() true .
move_uploaded_file()
. . true false .
upload .
-
PHP/MySQL
23 26
upload
PHP script . $_FILES['userfile']['size'] . $_FILES['userfile']['type'] . PHP 4.2.0, $_FILES['userfile']['error'] . , .
request .
-
PHP/MySQL
24 26
PHP MySQL
, MySQL. :
mysql_connect(servername,username,password);
servername , username password . , MySQL administrator MySQL username "username" password "password" PHP MySQL. , PHP mysql_error() .
:
mysql_query("CREATE DATABASE my_db",$con);
:
-
PHP/MySQL
25 26
if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; } else { echo "Error creating database: ".mysql_error(); } mysql_close($con);
?>
mysql_query() php MySQL . SQL query query , .
:
mysql_close(mysql_connect(servername,username,password));
query SQL:
INSERT INTO table_name (column1, column2, ..., columnN) VALUES (value1, value2,... ,valueN )
value1, value2, ... .
, phone_book php :
-
PHP/MySQL
26 26
SQL query:
SELECT (column1, column2, ..., columnN) FROM (table1, table2,... ,tableM) , phone_book PHP:
30 :
php mysql_fetch() input sql . mysql_fetch_array() php .