130 likes | 280 Views
PHP +SQL 13. PHP + MySQL PHPMyAdmin Practice: Vote system. PHP +SQL 13. PHP + MySQL PHPMyAdmin Practice: Vote system. MVC = Model + View + Controller. The separation of the main layers, also one of the most important software development principle/pattern
E N D
PHP+SQL13. PHP + MySQL PHPMyAdmin Practice: Vote system OE NIK, 2013
PHP+SQL13. PHP + MySQL PHPMyAdmin Practice: Vote system OE NIK, 2013
MVC = Model + View + Controller • The separation of the main layers, also one of the most important software development principle/pattern • PHP: very weak in the beginning, nowadays it is getting better and better (Symfony/CodeIgniter, Doctrine, APC/Memcached, Assetic) Display layer(HTML + template) Business logic layer(PHP) Data (resource) layer(ORM) OE NIK, 2013
Accessing databases in PHP • The physical access of the database is done by database-server-dependent modules (MySQL, MSSQL, Oracle, Sybase, PostgreSQL, Firebird) • The physical communication of the SQL commands and the TCP communication are done using functions with different names and parameter syntax alternative: DBX • ODBC: Access any standardized database server • PDO: PHP Data Objects (similar to Java DAO), MVC-like mapping, it connects the Model and the Control layers using SQL-independent methods Doctrine is a lot better from the development side, but PDO is a lot faster ... We’ll skip this due to lack of OOP PHP. . . OE NIK, 2013
PHP and MySQL • libmysqld vs mysqlnd: physical connection • ext-mysql / ext-mysqli: Direct connection to a MySQL server, possibility to send out SQL commands directly • mysql_* old, only procedural, but: a little faster, more conventional, but deprecated – not suggestedmysqli_* new, possibility to work with OO, supports prepared statements and the execution of multiple queries and transactions(we will use this one, but WITHOUT OOP ) • When using them the procedural style, the mysql_* and the mysqli_* commands have different parameter order! OE NIK, 2013
MySQL commands • $conn=mysqli_connect(servername,username,password,dbname); • $res=mysqli_query($conn, $query); • $num=mysqli_insert_id($conn); // INSERT • $num=mysqli_affected_rows($conn); // UPD, DEL, INS • $num=mysqli_num_rows($res); • $row=mysqli_fetch_assoc($res); Check the databases slides onhttp://users.nik.uni-obuda.hu/szabozs/ !!! OE NIK, 2013
Important things • $str=mysqli_real_escape_string($conn, $input); (prepared statement would be better. Please note that addslashes() is not safe, magic_quotes_gpc is ALWAYS OFF!) • $str=mysqli_error($conn); • $obj=mysqli_fetch_object($res); • mysql_set_charset($conn, "utf8"); (no mysql_query("set names 'utf8' ", $conn); ) OE NIK, 2013
PHP+SQL13. PHP + MySQL PHPMyAdmin Practice: Vote system OE NIK, 2013
PHPMyAdmin OE NIK, 2013
PHP+SQL13. PHP + MySQL PHPMyAdmin Practice: Vote system OE NIK, 2013
To do • create database php;grant all privileges on php.* to phpuser@localhost identified by 'phppw'; • Create tables: questions (qu_id, qu_text) + choices (cho_id, cho_qu, cho_text, cho_num) • Insert some example questions and possible choices! • Actions: LISTQ, LISTC, VOTE, ADDQ, ADDC (+DELQ, DELC) • HTML files: Question-row, Choice-row, Add question form, Add choice form, Back link OE NIK, 2013