?????? ????????????? ??? ?????????, ??????? ????????? ???? ????????????? PHP ? MySQL. ??? ????????????, ??? ???????? ???????? ???????? ???????? ?????? ? ?????? ?????? ? ???????????????? ? ????? ???????????? ???????? PHP ??? ???????? ??????? ??????? MySQL.
???????? ???????? ?? ??, ??? ? ????? ?????? ??????? ?????? ??? ??????????? ????????? ??????????? ? ??????? ????????.
?????????? ?? ????????? PHP ? MySQL ??????? ?? ????? ?????? ??????;
??????? ? ???????? 90?, ext/mysql ??????? ???????? ?????? ????? PHP ? MySQL. ???? ? ??? ??????? ?????????? ? ???????? ????? ? ??????, ? ?????, ext/mysql ????? ???? ???? ??????? ? ??? ? ???? ? ??????????? ??? ? PHP, ??? ? ? MySQL.
?????? ? ?????????? PHP 5 ? MySQL 4.1 ??? ?????????? — ?????? ?????????????? ????????? ?????????? ???????? ??????.
? ext/mysql ??????? «???????????, ??????????? ????????????»: ? ?????? ??????? ??? mysql_pconnect() [1] , ??????????? ?? ????????? ? ?????????????? ??????????? [2]. ????? ????, ?????????? ??????????????? ????? ????????? ext/mysql ? ????, ??? ?????????????? ?????????? ??????????? MySQL, ?? ??????? ???????? ? ext/mysql, ? ext/mysqli.
? ??????? ????????? ??? ???????????, ????? ?????? ?????? ????????? ?????????? PHP 5, ??????? ???????????? ????? ??????????? MySQL 4.1 . ??? ?????????? ???????? ???????? ext/mysqli, ??? «i» ???????? ???? ?? ????: improved(??????????), interface(?????????), ingenious(???????????????), incompatible(?????????????) or incomplete(????????). [3]
?????????? ?? ???????? ????? ???????? ?????? ?????????? ????:
Ext/mysqli ???????????? ????? ???????????, ??????????? ? ????????? ??????? MySQL, ? ?????????? ????? ???????.
???????? ??????????? ??????????:
????? ????????? ??????? ? ??????????????? ??????????? MySQL 4.1 , ????? ?? ????? ?????????? ?? ????????????? ext/mysqli?
? ?????????? ? ??????????? ???????????, ext/mysqli ????? ????????? ???????????? ???????????:
????????? ??????? ext/mysqli ?????? ?????????? ?? ??????? ??????????. ? ????? ??????????? ???????????? ??????? ? ??????? ? ?????????, ????????? ? ???????, ????????? ??????????? ???? ??????:
??????, ????? ?? ??????, ??? ??????????, ?? ?????? ????????????? ???, ??????? ?????????????, ??? ???????? ? ???????? ????? ??????????. ???? ??????????????? ???, ??????????? ? ???? ??????, ?????????? ???? ?????? «world», ??????? ????????? ???????? ?? ????? http://www.mysql.com/documentation/index.html.
??? ??????? ??????, ??????? ??????????? ? ???????? MySQL, ???????? ?????? ??????? ? ??????? ????? ??????????, ??????? ?????????? ??????? ? ????? ??????????? ?????????????? ????????? ??????? ? ????????? ??????????.
<?php /* ??????????? ? ??????? MySQL */ $link = mysqli_connect( "localhost", /* ????, ? ???????? ?? ???????????? */ "user", /* ??? ???????????? */ "password", /* ???????????? ?????? */ "world"); /* ???? ?????? ??? ???????? ?? ????????? */ if (!$link) { printf("?????????? ???????????? ? ???? ??????. ??? ??????: %s\n", mysqli_connect_error()); exit; } /* ???????? ?????? ??????? */ if ($result = mysqli_query($link, "SELECT Name, Population FROM City ORDER BY Population DESC LIMIT 5")) { print("????? ??????? ??????:\n"); /* ??????? ??????????? ??????? */ while( $row = mysqli_fetch_assoc($result) ){ printf("%s (%s)\n", $row["Name"], $row["Population"]); } /* ??????????? ???????????? ?????? */ mysqli_free_result($result); } /* ????????? ?????????? */ mysqli_close($link); ?>
??????????? ???????? ?????? ??????? ???-?? ?????:
????? ??????? ??????: Mumbai (Bombay) (10500000) Seoul (9981619) Sao Paulo (9968485) Shanghai (9696300) Jakarta (9604900)
??? ????? ?? ????, ext/mysqli ? ext/mysql ????? ???? ????? ??????. ???????????? ???????????? ????????? ???????? ??, ??? ??????????? ????? ext/mysqli ????????? ????? «???????????».
????????, ??? ??? ???????? ?? ?????? ??????????? ?????? ??? ?? ???? ???? ? ????? ????? ? ??????? ???????????? ??????? ????????? ?? ??????.
????????-??????????????? ????????? ????????????? ??????? ????? ?????????? ? ????? ????????????? ? ??????? ????? ????????????? ext/mysqli. ???, ??????????? ????, ?????????? ?? ?? ????????, ??? ? ??????????, ??????, ??????? ????????? ???????? ???????, ?? ??????? ????? ???????? ????????:
<?php /* ??????????? ? ??????? MySQL */ $mysqli = new mysqli("localhost", "user", "password", "world"); if (mysqli_connect_errno()) { printf("??????????? ? ??????? MySQL ??????????. ??? ??????: %s\n", mysqli_connect_error()); exit; } /* ???????? ?????? ??????? */ if ($result = $mysqli->query("SELECT Name, Population FROM City ORDER BY Population DESC LIMIT 5")) { print("????? ??????? ??????:\n"); /* ???????? ?????????? ???????: */ while( $row = $result->fetch_assoc() ){ printf("%s (%s)\n", $row["Name"], $row["Population"]); } /* ??????????? ?????? */ $result->close(); } /* ????????? ?????????? */ $mysqli->close(); ?>
??????, ????? ?? ????????? ??? ????????????? ??????????, ?????????? ????????? ????? ????????????.
?????????????? ????????? ????????????? ????????????? ??????????? ????????? ???????, ??????? ???????? ????? ???????????, ????? ????? ??????? ?????????????????? ? ????? ?????? ? ?????????.
?????????????? ????????? ????? ???????????? ????? ?????????: ? ????????? ??????????? ? ? ????????? ????????????.
?????????????? ????????? ? ????????? ??????????? ????????? ????????? ??????? ???????? ? ??????? ?? ?? ??????? MySQL. ????? ????? ??????? ??????, ??????, ??????????? ??????, ???????????? ??????? MySQL, ??? ????????? ?????????????? ?????? ? ???????????.
???????? ??????? ???????? ? ????????????? ?????????????? ????????? ? ????????? ??????????? ?????.
????????? ?????? ??????? ? ?????????? ??????? MySQL. ?????? ??? ????????, ????????? ??? ????????????, ????? ?????????, ??? ?? ????? ?????, ? ????????? ??? ? ??????????? ??????. ????? ?????? ?????????? ?????????????, ??????? ????? ???? ? ?????????? ??????????? ??? ????????? ? ??????????????? ?????????.
????? ????? ??????? ??????, ??????, ??????????? ??????, ???????????? ??????? MySQL ? ????????? ?????????????? ?????? ???????????.
? ???? ???????? ????????? ????????? ????? ?????? ???????.
???? ??????? ?????????? ??????? MySQL ?????? ???? ???. ??? ?????????? ????????? ?????????? ?????? ??????, ??????????? ??? ?????????? ???????.
??????? ????? ?????? ?? ???????? ? ????????? ??????? ????????????? ?????? ???? ???, ?????? ????, ????? ?????? ??? ?????? ???.
????? ????, ??? ????????, ??????? ???????? ????????? ?????????? ??????, ??????? ?????? ?????????. ????????, ???? ? ??? ???? ?????? ????:
INSERT INTO City (ID, Name) VALUES (NULL, "Calgary");
?? ?????? ??? ??? ?????????? ??????? ????? ???????? ???? ????? 16 ???? ?????? ??????? 60 ??? ????? ????. (??? ???????????? ????? ???????? ??????? ?? ??? ?????? ????? ?????????????? ??????????????? ?????????, ????? ?????? ??????? — ??? ???????????? ???????? ?????? — ? ?.?., ?? ?? ???????? ??????? ?? ?????? ???????.)
?????? ??????? ?? ?????? ????????? ????? ??????? ????? mysql_real_escape_string(), ????? ?????????, ??? ??? ?????? ????? «SQL-???????» [4] ?????? ?????, ?????? ? ?????? MySQL ???????? ???, ????? ?????????, ??? ????????? ?????? ????????? ?????????? ??? ?? ?????????????? ? ?????????????? ??????????.
?????? ??????? ???????? ???-?? ???:
INSERT INTO City (ID, Name) VALUES (?, ?);
???? «?» ????? ???????????? ? ??????????? ????, ??? ???????????? ?????????? ??????, ???????? ?????? ????? ???? ????????? ??
SELECT Name FROM City WHERE Name = "Calgary";
?
SELECT Name FROM City WHERE name = ?;
<?php $mysqli = new mysqli("localhost", "user", "password", "world"); /* ???????? ?????????? online casino */ if (mysqli_connect_errno()) { printf("??????????? ??????????: %s\n", mysqli_connect_error()); exit(); } $stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)"); $stmt->bind_param("sssd", $code, $language, $official, $percent); $code = "DEU"; $language = "Bavarian"; $official = "F"; $percent = 11.2; /* ?????????? ??????????????? ????????? */ $stmt->execute(); printf("%d Row inserted.\n", $stmt->affected_rows); /* ???????? ?????????? ? ?????????*/ $stmt->close(); /* ???????? ??????? CountryLanguage */ $mysqli->query("DELETE FROM CountryLanguage WHERE Language="Bavarian""); printf("%d Row deleted.\n", $mysqli->affected_rows); /* ??????? ??????????? */ $mysqli->close(); ?>
???????? ???????? ?? ??, ??? ?????? ?????????? bind_param() ???????? ???????? ??????. ??? ?????? ???????, ???????????? ??? ??????????? ????, ??? ??????????? ????????? ?????? ???? ????????????????.
? ?????? ???????????????? ???????? «sssd» ????????, ??? ???????? ?????? ???? ?????????? $code, $language ? $official ????? ??????? ??? ??????, ? ????????? ???????? $percent ????? ????????? ???????? ???? double ? ????????? ???????.
??? ?????? ?????????? ?????????? ? bind_param(), ?????? ???? ???? ????? ? ?????? ???????, ??????? ????????, ??? ?????????? ????? ??????????. ????????
$stmt->bind_param("s", $foo); $stmt->bind_param("si", $foo, $bar); $stmt->bind_param("sid", $foo, $bar, $baz);
?????????? ????? ???????????? ??, ??? ?????????? mysqli ?????, ??? ??????????? ?????? ??? ??????? ?????????????.
??????????? ????? ????? ??????: ?????? ? ???????? ?????????? ????? ?????????????? ??? ?????????????, ???????????? ????? (double) ??? ??? ??????.
????? ??????? ??????????? ???, ??????????? ?????????? ????? (??????? ???????? ???????) ????????.
????????? ??????? ???????????? ???? ? ??????????? ?????????????:
????????????? ???? | ??? ??????? |
i | ??? INT ???? |
d | DOUBLE ? FLOAT |
b | BLOB»? |
s | ????????? ???? |
?????????????? ????????? ? ???????????? ???????????? ????????? ??????????? ?????????? PHP-??????? ? ????????? ????? ?????? ? ?????????????? ????????? ???????.
??????? ?????????? ?????:
??? ??????? ???????? ????, ?????????????? ???????:
<?php $mysqli = new mysqli("localhost", "user", "password", "world"); if (mysqli_connect_errno()) { printf("??????????? ??????????: %s\n", mysqli_connect_error()); exit(); } /* ?????????????? ????????? */ if ($stmt = $mysqli->prepare("SELECT Code, Name FROM Country ORDER BY Name LIMIT 5")) { $stmt->execute(); /* ???????????? ?????????? ? ????????? */ $stmt->bind_result($col1, $col2); /* ??????? ???????? */ while ($stmt->fetch()) { printf("%s %s\n", $col1, $col2); } /* ???????? ????????? $stmt->close(); } /* ???????? ?????????? */ $mysqli->close(); ?>
??? ????? ?????? ??????, ??????????????? ????????????? ? ???????? ??????????, ? ???????? ??????????? ????????????:
<?php $mysqli = new mysqli("localhost", "user", "password", "world"); if (mysqli_connect_errno()) { printf("??????????? ??????????: %s\n", mysqli_connect_error()); exit(); } /* ?????????????? ????????? */ if ($stmt = $mysqli->prepare("SELECT Code, Name FROM Country WHERE Code LIKE ? LIMIT 5")) { $stmt->bind_param("s", $code); $code = "C%"; $stmt->execute(); /* ?????????? ?????????? ??? ?????????????? ?????????*/ $stmt->bind_result($col1, $col2); /* ??????? ???????? */ while ($stmt->fetch()) { printf("%s %s\n", $col1, $col2); } /* ???????? ????????? */ $stmt->close(); } /* ???????? ??????????? */ $mysqli->close(); ?>
? ???? ?????? ?? ??????? ????? ???????????? ? ??????????? ext/mysqli, ? ????? ??????? ????????? ??????? ??? ????????. ? ????? ??????? ?? ?????? ????????, ??? ???????????? ? ???????? ?????? ?? ?????????????? ????????? MySQL ? ?????? ??????????? ???????? ????????????? ????????-???????????????? ?????????? ? ext/mysqli.
ext/mysql — ?????? ?????????? PHP ??? ?????? ? MySQL. ?? ???????????? ???? ???????????? MySQL ?????? 4.1 ? ????.
ext/mysqli — ????? ?????????? PHP 5 ??? ?????? ? MySQL. ???????????? ??????????? MySQL ?????? ?? 3.22 ?? 5.0
?????????? ?????????? MySQL — ????????? MySQL RDBMS (MySQL Relational DataBase Management System — ??????? ?????????? ??????????? ????? ?????? MySQL — ????. ???????????), ??????? ????????? ?????????? ???????? ? RDBMS.
?????? MySQL — ????????? MySQL RDBMS, ??????? ???????????? ? ???????? ?? ???????, ????????? ???????? ?????????????? ?????? ?????? ???? ? ?.?.
[1] — ??????? mysql_pconnect() ???? ??????? ??? ?????????????? ????????? ?????????? ?????? ?? ???????????? ? ?????? ?????????? ? ???????? MySQL. ? ?????????, ??-?? ?????????????? ????? ????????????? ??????? Apache ? PHP, ??????? ?????? ?? ?????, ???????????? pconnect, ??? ?????? ?????????? ?????? MySQL ??????? ??????????? ?????????????? ??????????, ??????? ?????? ???????? ??????????? ???????? ?????? ? ???? ??????.
[2] — ??????????? ??????????????? ?????????? ????????? ???????????? ??????? ??????? ????????????? ??????????? ? ????? ?????? (???? ?????????? ?????????? ? ?????????? ?????????? ? ???????????????? ????? php.ini). ??????????? ?????????? ?? ????????? ???????? ?? ?????????? ????????. ????????? ???????? ?????????? ? ????? MySQL ?????????? ???????????? ???????????, ???? ???????? ?????????? ?? ??? ???? ?????? ? ?????????? ???????.
[3] — ??? ?????????? ??? ??? ????????? ? ?????? ??????????. ? ?? ????? ??? ????? ???????????? ???? ?????? ???? ????????????? ??????????, ?? MySQL 4.1, ?? PHP 5.0 ?? ????? ?????????? ??????? (?????? ????????? ?? ?????? PHP 5.0.0 — ????. ???????????). ?????, ?????????????? ????? ????????????, ??????? ?? ????? ????????? ?????????? ?????????? ?????????? MySQL, ??? ??? ??????????????.
[4] — ????? ???? «SQL-??????» ????????, ????? ?????? ?????? ? ??????, ????????? ??? ????????? ??????????? ?/??? ????????????? ????????. ?????, ??? ???????, ??? ??????? ?????? ? PHP ??????? ???? «DELETE FROM grades WHERE class_name=»test_$class»». ????????? ????? ???????? ???????? ??? ?????????? $class ? ???????? ??????????? an attacker who can gain control over the value of $class can force unintended deletes to occur by changing the value of $class to something like «oops» or class_name LIKE «%»».
??? ????? (Zak Greant) — ???????????????? ???????? ????????? Open Source, ???????? ? ???????????. ?? ???????? ? MySQL AB ?????????????? ??????????. ??? ???????????? ??? ?????????? PHP ??? ?????? ? MySQL ? ???????? ????????? PHP Functions Essential Reference.
????? ?????? (Georg Richter) — ????????? ?????????? mysqli. ?? ????? ???????????? ?????????? mysql ? ncurses. ?? ???????? ? MySQL AB ??????? ????????????? ? ???????? ?????? Apache Software Foundation.
?c??????: phpclub.net
Tags: apache, bind, mysql, SQL