1 / 16

PHP VI Adatbázisok, MySQL

PHP VI Adatbázisok, MySQL. phpMyAdmin. Wamp ikon. phpMyAdmin - Adatbázisok. Új adatbázis. Adatbázisok. phpMyAdmin - Tábák. Törlés. Táblák. Új tábla. phpMyAdmin - Táblák. Hozzuk létre a ‘Persons’ táblát, 2 mezővel: id (bigint, not null, auto_increment, primary)

Download Presentation

PHP VI Adatbázisok, MySQL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP VIAdatbázisok, MySQL

  2. phpMyAdmin Wamp ikon

  3. phpMyAdmin - Adatbázisok Új adatbázis Adatbázisok

  4. phpMyAdmin - Tábák Törlés Táblák Új tábla

  5. phpMyAdmin - Táblák • Hozzuk létre a ‘Persons’ táblát, 2 mezővel: • id (bigint, not null, auto_increment, primary) • name (varchar 255, not null, unique) Létrehozás

  6. phpMyAdmin - Táblák • Hozzuk létre a ‘Currency’ táblát! create table currency ( id bigint not null auto_increment, name varchar(255) not null, rate double not null, primary key (id), unique (name) ) SQL parancsok

  7. SQL parancsok • Tábla létrehozása: • create table (....) • Tábla törlése: • drop table • Rekord beszúrása táblába: • insert into table(...) values(...) • Lekérdezés: • select col1, col2... from table where .... • Törlés: • delete from table • Módosítás: • update table set(...)

  8. Feladatok insert into persons(name) values(‘Nikolaj’); insert into persons(name) values(‘Svan’); insert into persons(name) values(‘Johann’); select * from persons; select * from persons where name like ‘Nik%’; select id from persons where name = ‘Svan’; update persons set name = ‘Sven’ where id = 2; delete from persons where name = ‘Johann’;

  9. Idegen kulcsok • Egy táblának egy mezője hivatkozik egy másik tábla mezőjére. Pl ha tároljuk, hogy kinek mennyi pénze van egy adott pénznemből, akkor hivatkoznunk kell a személyre, illetve a pénznemre. create table money ( person_id bigint not null, currency_id bigint not null, value double not null, foreign key(person_id) references persons(id) on update cascade on delete restrict, foreign key(currency_id) references currency(id) on update cascade on delete restrict);

  10. SQL • http://www.w3schools.com/sql/ • http://dev.mysql.com/doc/refman/5.0/en/ • http://hu.wikipedia.org/wiki/SQL

  11. Feladatok • Töröljük eddigi tábláinkat (persons, currency, money) • A db.sql szöveges állomány az előző órai adatbázist tartalmazza. A phpMyAdmin-nal futtasuk le a fájlban található utasításokat. (Copy/Paste –eljük be a teljes tartalmat és GO)

  12. PHP + MySQL • Kapcsolódás: • $conn = mysql_connect($host, $user, $pass); • Adatbázis választás: • mysql_select_db($db, $conn); • Lekérdezés: • $result = mysql_query($query, $conn); • Adatok kinyerése: • $record = mysql_fetch_array($result); • Memória felszabadítás: • mysql_free_result($result); • Kapcsolat megszűntetése: • mysql_close($conn);

  13. Feladatok $conn = mysql_connect(‘localhost’, ‘root’); if (!$conn) die(‘connection error: ’ . mysql_error()); if (!mysql_select_db(‘mysql’, $conn)) die(‘database error: ’ . mysql_error()); $result = mysql_query(‘select id, name from persons’, $conn); if (!result) die(‘query error: ’ . mysql_error()); while ($rec = mysql_fetch_array($result)) echo ‘id=‘ . $rec[‘id’] . ‘; name=‘ . $rec[‘name’] . ‘<br/>’; mysql_free_result($result); mysql_close($conn);

  14. Biztonság • Hozzuk létre a registration.sql –ben megadott táblát, valamint használjuk a login.html és auth.php állományokat. • Teszteljük le a 3 felhasználót, hogy helyesen működik-e a beléptető. • Használjuk a következőket: • Username: fake' or 1=1 or '1'='1 • Password: fake • Kommentezzük ki az echo $query sort! • $u=addslashes($u); $p=addslashes($p);

  15. Serialize • Az előző órán obejtkum serialize: • __wakeup(), __sleep() metódus • Adatbázis kapcsolatokat nem mentünk el! Helyette: class Connection { protected $conn private $host, $user, $pass, $db; public function __sleep() { return array(‘host’, ‘user’, ‘pass’, ‘db’); } public function __wakeup() { $this->connect(); } private function connect() { $this->conn = mysql_connect(.........); }

  16. Feladatok • Készítsük el az előző órai DAO interfészt megvalósító MySQL adatbázison működő osztályunkat MySqlDAO néven.

More Related