Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (MySQL)

Slides:



Advertisements
Similar presentations
PHP: Date() Function The PHP date() function formats a timestamp to a more readable date and time.
Advertisements

PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
Introduction to MySQL. 2 Road Map Introduction to MySQL Connecting and Disconnecting Entering Basic Queries Creating and Using a Database.
介紹 元智大學電機工程所 碩士班一年級 蕭觀華 學號 : MySQL 介紹大綱 What is MySQL ? How to install on Linux Tutorial Introduction Database Administration MySQL Perl API Q&A.
Introduction to MySQL. 2 Road Map  Introduction to MySQL  Connecting and Disconnecting  Entering Basic Queries  Creating and Using a Database.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
Quick-and-dirty.  Commands end in a semi-colon ◦ If you forget, another prompt line shows up  Either continue the command or…  End it with a semi-colon.
How to Use MySQL CS430 March 18, SQL: “Structured Query Language”—the most common standardized language used to access databases. SQL has several.
Manipulating MySQL Databases with PHP. PHP and mySQL2 Objectives Connect to MySQL from PHP Learn how to handle MySQL errors Execute SQL statements with.
Objectives Connect to MySQL from PHP
Intermediate PHP & MySQL
PHP and MySQL Web Development tMyn1 PHP and MySQL Web Development When you install PHP, you can select from a number of extensions. The MySQL support in.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
MySQL Dr. Hsiang-Fu Yu National Taipei University of Education
PHP1-1 PHP & SQL Xingquan (Hill) Zhu
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
1 Chapter 8 – Working with Databases spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
INTERNET APPLICATION DEVELOPMENT For More visit:
Sayed Ahmed Computer Engineering, BUET, Bangladesh MSC, Computer Science, U of Manitoba, Canada
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
PHP – MySQL Extensions. Table used in most examples CREATE TABLE product ( rowID INT NOT NULL AUTO_INCREMENT, productid VARCHAR(8) NOT NULL, name VARCHAR(25)
In the next lectures you will learn  What is SQL  How to access mySQL database  How to create a basic mySQL database  How to use some basic queries.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) SQL and MySQL James Wang.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
Comp 519: Web Programming Autumn 2014 Advanced SQL and PHP Advanced queries Querying more than one table Searching tables to find information Aliasing.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Introduction to MySQL Lab no. 10 Advance Database Management System.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Chapter 6 PHP Interacts with Mysql Database. Introduction In PHP, there is no consolidated interface. Instead, a set of library functions are provided.
MySQL Databases & PHP Integration Using PHP to write data to, and retrieve data from, a MySQL database.
1. Connecting database from PHP 2. Sending query 3. Fetching data 4. Persistent connections 5. Best practices.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
Session 7: Getting PHP to Talk to MySQL. Objectives Connecting to the Database Building & Executing the SQL SELECT Query Fetching & Displaying the data.
Chapter 5 MYSQL Database. Introduction to MYSQL MySQL is the world's most popular open-source database. Open source means that the source code, the programming.
Most information comes from Chapter 3, MySQL Tutorial: 1 MySQL: Part.
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
CpSc 462/662: Database Management Systems (DBMS) MySQL.
SQL has several parts: Major ones: DDL – Data Definition Language {Defining, Deleting, Modifying relation schemas} DML – Data Manipulation Language {Inserting,
Connecting, Queries, Best Practices Tran Anh Tuan Edit from Telerik Software Academy
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
Mysql YUN YEO JOONG. 1 Connecting to and Disconnecting from the Server 1 Connecting to and Disconnecting from the Server shell> mysql – h host -u user.
MySQL API( c ) & SQL2 강동훈.
ITX2000 Remote hosts and web servers Prof. Xiaohong (Sharon) Gao Room: T125 Ext: Week 3 – MySQL – Statements.
1 PHP and MySQL Web Development When you install PHP, you can select from a number of extensions. The MySQL support in PHP consists of a number of functions.
INF1343, Winter 2012 Data Modeling and Database Design Yuri Takhteyev Faculty of Information University of Toronto This presentation is licensed under.
CCT1343, Week 3 SQL Queries Using Multiple Tables This presentation is licensed under Creative Commons Attribution License, v To view a copy of this.
Database Design and Implementation
CCT395, Week 2 Introduction to SQL Yuri Takhteyev September 15, 2010
Chapter 5 Introduction to SQL.
MySQL: Part II.
Data Modeling and Database Design INF1343, Winter 2012 Yuri Takhteyev
CCT1343, Week 2 Single-Table SQL Yuri Takhteyev University of Toronto
Introduction to MySQL.
Introduction to MySQL.
Chapter 7 Working with Databases and MySQL
Web Systems Development (CSC-215)
MySQL Dr. Hsiang-Fu Yu National Taipei University of Education
MySQL Database System Installation Overview SQL summary
Introduction to MySQL.
Presentation transcript:

Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (MySQL)

PHP Programming PHP Programming MySQL client MySQL server MySQL util

PHP Programming PHP Programming Basic MySQL Help shell> mysql –help Sever connect shell> mysql -h host -u user -p Enter password: ******** shell> mysql -h host -u user -p Enter password: ******** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 459 to server version: a-log Type 'help' for help. mysql>

PHP Programming PHP Programming anonymous user login shell> mysql Disconnect mysql> QUIT Bye Ctrl+D

PHP Programming PHP Programming Entering Queries mysql> select version(), current_date; | version() | current_date | | a-log| | row in set (0.01 sec) mysql>

PHP Programming PHP Programming Reserved command using method mysql> SELECT VERSION(), CURRENT_DATE; mysql> select version(), current_date; mysql> seLect vErSiOn(), current_DATE; All same command mysql> select sin(pi()/4), (4+1)*5; | sin(pi()/4) | (4+1)*5) | | | 25 | mysql> select version(); select now(); | version() | | a-log| | now() | | :15:33 |

PHP Programming PHP Programming Cancel mysql> select -> user() -> \c mysql> mysql> select user() -> ; | user() | | |

PHP Programming PHP Programming Make Database mysql> SHOW DATABASES; | Database | | mysql | | test | | tmp | mysql> USE test Database changed

PHP Programming PHP Programming mysql> create database menagerie; mysql> use menagerie Database changed shell> mysql -h host -u user -p menagerie Enter password: ******** mysql> show tables; Empty set (0.00 sec) mysql> create table pet (name varchar(20), owner varchar(20), -> species varchar(20), sex char(2), birth date, death date);

PHP Programming PHP Programming mysql> show tables; | Tables in menagerie | | pet | mysql> describe pet; | Field | Type | Null | Key | Default | Extra | | name | varchar(20) | YES| | NULL | | | owner | varchar(20) | YES | | NULL | | | species | varchar(20) | YES | | NULL | | | sex | char(2) | YES | | NULL | | | birth | date | YES | | NULL | | | death | date | YES | | NULL | |

PHP Programming PHP Programming mysql> insert into pet -> values('Puffball', 'Diane', 'hamster', 'f', ' ', NULL); mysql> load data local infile "pet.txt" into table pet; Pet.txt nameownerspeciessexbirthdeath FluffyHaroldcatf ClawsGwencatm BuffyHarolddogf FangBennydogm BowserDiannedogm ChirpyGwenbirdf WhistlerGwenbird SlimBennysnakem

PHP Programming PHP Programming serach mysql> select * from pet; | name | owner | species | sex | birth | death | | Fluffy | Harold | cat | f | | NULL | | Claws | Gwen | cat | m | | NULL | | Buffy | Harold | dog | f | | NULL | | Fang | Benny | dog | m | | NULL | | Bowser | Diane | dog | m | | | | Chirpy | Gwen | bird | f | | NULL | | Whistler | Gwen | bird | NULL| | NULL | | Slim | Benny | snake | m | | NULL | | Puffball | Diane | hamster | f | | NULL | mysql> delete from pet; mysql> load data local infile "pet.txt" into table pet;

PHP Programming PHP Programming update mysql> update pet set birth=" " where name="Bowser"; mysql> select * from pet where name = "Bower"; | name | owner | species | sex | birth | death | | Bowser | Diane | dog | m | | | mysql> select * from pet where birth >= " " | name | owner | species | sex | birth | death | | Chirp y | Gwen | bird | f | | NULL | | Puffball | Diane | hamster | f | | NULL |

PHP Programming PHP Programming mysql> select * from pet where species = "dog" AND sex = "f"; | name | owner | species | sex | birth | death | | Buffy | Harold | dog | f | | NULL | mysql> select * from pet where species = "snake" or species = "bird"; | name | owner | species | sex | birth | death | | Chirpy | Gwen | bird | f | | NULL | | Whistler | Gwen | bird |NULL | | NULL | | Slim | Benny | snake | m | | NULL |

PHP Programming PHP Programming mysql> select * from pet where (species = "cat" AND sex = "m") -> OR (species = "dog" AND sex = "f"); | name | owner | species | sex | birth | death | | Claws | Gwen | cat | m | | NULL | | Buffy | Harold | dog | f | | NULL | mysql> select name, birth from pet; | name | birth | | Fluffy | | | Claws | | | Buffy | | | Fang | | | Bowser | | | Chirpy | | | Whistler | | | Slim | | | Puffball | |

PHP Programming PHP Programming mysql> select owner from pet; | owner | | Harold | | Gwen | | Harold | | Benny | | Diane | | Gwen | | Benny | | Diane | mysql> select distinct owner from pet; | owner | | Benny | | Diane | | Gwen | | Harold |

PHP Programming PHP Programming mysql> SELECT name, species, birth FROM pet -> WHERE species = "dog" OR species = "cat"; | name | species | birth | | Fluffy | cat | | | Claws | cat | | | Buffy | dog | | | Fang | dog | | | Bowser | dog | |

PHP Programming PHP Programming mysql> select name, birth from pet order by birth; | name | birth | | Buffy | | | Bowser | | | Fang | | | Fluffy | | | Claws | | | Slim | | | Whistler | | | Chirpy | | | Puffball | | mysql> select name, birth from pet order BY birth desc; | name | birth | | Puffball | | | Chirpy | | | Whistler | | | Slim | | | Claws | | | Fluffy | | | Fang | | | Bowser | | | Buffy | |

PHP Programming PHP Programming mysql> SELECT name, species, birth FROM pet ORDER BY species, birth DESC; | name | species | birth | | Chirpy | bird | | | Whistler | bird | | | Claws | cat | | | Fluffy | cat | | | Fang | dog | | | Bowser | dog | | | Buffy | dog | | | Puffball | hamster | | | Slim | snake | |

PHP Programming PHP Programming Date calculation mysql> select name, (to_days(now())-to_days(birth))/365 from pet; | name | (TO_DAYS(NOW())-TO_DAYS(birth))/365 | | Fluffy | 6.15 | | Claws | 5.04 | | Buffy | 9.88 | | Fang | 8.59 | | Bowser | 9.58 | | Chirpy | 0.55 | | Whistler | 1.30 | | Slim | 2.92 | | Puffball | 0.00 |

PHP Programming PHP Programming mysql> select name, (to_days(now())-to_days(birth))/365 as age -> from pet order by name; Order by age mysql> select name, (to_days(death)-to_days(birth))/365 as age -> from pet where death is not null order by age; mysql> select name, birth, month(birth) from pet; | name | birth | MONTH(birth) | | Fluffy | | 2 | | Claws | | 3 | | Buffy | | 5 | | Fang | | 8 | | Bowser | | 8 | | Chirpy | | 9 | | Whistler | | 12 | | Slim | | 4 | | Puffball | | 3 |

PHP Programming PHP Programming mysql> select name, birth from pet where month(birth) = 10; mysql> SELECT name, birth FROM pet -> WHERE MONTH(birth) = MONTH(DATE_ADD(NOW(), INTERVAL 1 MONTH)); mysql> SELECT name, birth FROM pet -> WHERE MONTH(birth) = MOD(MONTH(NOW()),12) + 1;

PHP Programming PHP Programming NULL value mysql> SELECT 1 = NULL, 1 != NULL, 1 NULL; |1 = NULL |1 != NULL |1 NULL | | NULL | NULL | NULL | NULL | mysql> SELECT 1 IS NULL, 1 IS NOT NULL; | 1 IS NULL| 1 IS NOT NULL | | 0 | 1 |

PHP Programming PHP Programming Pattern match mysql> SELECT * FROM pet WHERE name LIKE "b%"; | name | owner | species | sex | birth | death | | Buffy | Harold | dog | f | | NULL | | Bowser | Diane | dog | m | | | mysql> SELECT * FROM pet WHERE name LIKE "%fy"; | name | owner | species | sex | birth | death | | Fluffy | Harold | cat | f | | NULL | | Buffy | Harold | dog | f | | NULL |

PHP Programming PHP Programming mysql> SELECT * FROM pet WHERE name LIKE "%w%"; | name | owner | species | sex | birth | death | | Claws | Gwen | cat | m | | NULL | | Bowser | Diane | dog | m | | | | Whistler | Gwen | bird | NULL | | NULL | mysql> SELECT * FROM pet WHERE name LIKE "_ _ _ _ _"; | name | owner | species | sex | birth | death | | Claws | Gwen | cat | m | | NULL | | Buffy | Harold | dog | f | | NULL |

PHP Programming PHP Programming REGEXP Return value is not same select * from pet where name regexp "ffy“; select * from pet where name like "ffy"; regexpcontents.One char *Nth over repeat ^String of start $String of end [,][ ]inner character match {,} {n}th repeat.

PHP Programming PHP Programming mysql> SELECT * FROM pet WHERE name REGEXP "^[bB]"; | name | owner | species | sex | birth | death | | Buffy | Harold | dog | f | | NULL | | Bowser | Diane | dog | m | | | mysql> SELECT * FROM pet WHERE name REGEXP "fy$"; | name | owner | species | sex | birth | death | | Fluffy | Harold | cat | f | | NULL | | Buffy | Harold | dog | f | | NULL | ^.....$ = ^.{5}$

PHP Programming PHP Programming mysql> SELECT COUNT(*) FROM pet; | COUNT(*) | | 9 | mysql> SELECT owner, COUNT(*) FROM pet GROUP BY owner; | owner | COUNT(*) | | Benny | 2 | | Diane | 2 | | Gwen | 3 | | Harold | 2 | mysql> SELECT owner, COUNT(owner) FROM pet; ERROR 1140 at line 1: Mixing of GROUP columns (MIN(),MAX(),COUNT()...) with no GROUP columns is illegal if there is no GROUP BY clause

PHP Programming PHP Programming mysql> SELECT species, COUNT(*) FROM pet GROUP BY species; | species | COUNT(*) | | bird | 2 | | cat | 2 | | dog | 3 | | hamster | 1 | | snake | 1 | mysql> SELECT sex, COUNT(*) FROM pet GROUP BY sex; | sex | COUNT(*) | | NULL | 1 | | f | 4 | | m | 4 |

PHP Programming PHP Programming mysql> SELECT species, sex, COUNT(*) FROM pet GROUP BY species, sex; | species | sex |COUNT(*) | | bird | NULL| 1 | | bird | f | 1 | | cat | f | 1 | | cat | m | 1 | | dog | f | 1 | | dog | m | 2 | | hamster | f | 1 | | snake | m | 1 | mysql> SELECT species, sex, COUNT(*) FROM pet -> WHERE species = "dog" OR species = "cat" -> GROUP BY species, sex; | species | sex | COUNT(*) | | cat | f | 1 | | cat | m | 1 | | dog | f | 1 | | dog | m | 2 |

PHP Programming PHP Programming Using several table mysql> CREATE TABLE event (name VARCHAR(20), date DATE, -> type VARCHAR(15), remark VARCHAR(255)); Event.txt Fluffy litter 4 kittens, 3 female, 1 male Buffy litter 5 puppies, 2 female, 3 male Buffy litter 3 puppies, 3 female Chirpy vet needed beak straightened Slim vet broken rib Bowser kennel Fang kennel Fang birthday Gave him a new chew toy Claws birthday Gave him a new flea collar Whistler birthday First birthday

PHP Programming PHP Programming mysql> LOAD DATA LOCAL INFILE "event.txt" INTO TABLE event; mysql> SELECT pet.name, (TO_DAYS(date) - TO_DAYS(birth))/365 AS age, -> remark FROM pet, event -> WHERE pet.name = event.name AND type = "litter"; | name | age | remark | | Fluffy | 2.27 | 4 kittens, 3 female, 1 male | | Buffy | 4.12 | 5 puppies, 2 female, 3 male | | Buffy | 5.10 | 3 puppies, 3 female | mysql> SELECT p1.name, p1.sex, p2.name, p2.sex, p1.species -> FROM pet AS p1, pet AS p2 -> WHERE p1.species = p2.species AND p1.sex = "f" AND p2.sex = "m";

PHP Programming PHP Programming | name | sex | name | sex | species | | Fluffy | f | Claws | m | cat | | Buffy | f | Fang | m | dog | | Buffy | f | Bowser| m | dog |

PHP Programming PHP Programming Batch processing shell> mysql < batch-file shell> mysql -h host -u user -p < batch-file Enter password: ******** shell> mysql < batch-file | less shell> mysql mysql.out General modeBatch Mode | species | | bird | | cat | | dog | | hamster | | snake | species bird cat dog hamster snake

PHP Programming PHP Programming FunctionDescription mysql_affected_rows() Returns the number of affected rows in the previous MySQL operation mysql_change_user()Deprecated. Changes the user of the current MySQL connection mysql_client_encoding()Returns the name of the character set for the current connection mysql_close()Closes a non-persistent MySQL connection mysql_connect()Opens a non-persistent MySQL connection mysql_create_db() Deprecated. Creates a new MySQL database. Use mysql_query() instead mysql_data_seek()Moves the record pointer mysql_db_name()Returns a database name from a call to mysql_list_dbs() mysql_db_query() Deprecated. Sends a MySQL query. Use mysql_select_db() and mysql_query() instead mysql_drop_db()Deprecated. Deletes a MySQL database. Use mysql_query() instead mysql_errno()Returns the error number of the last MySQL operation mysql_error()Returns the error description of the last MySQL operation mysql_escape_string() Deprecated. Escapes a string for use in a mysql_query. Use mysql_real_escape_string() instead mysql_fetch_array() Returns a row from a recordset as an associative array and/or a numeric array mysql_fetch_assoc()Returns a row from a recordset as an associative array mysql_fetch_field()Returns column info from a recordset as an object mysql_fetch_lengths()Returns the length of the contents of each field in a result row mysql_fetch_object()Returns a row from a recordset as an object mysql_fetch_row()Returns a row from a recordset as a numeric array mysql_field_flags()Returns the flags associated with a field in a recordset

PHP Programming PHP Programming mysql_field_len()Returns the maximum length of a field in a recordset3 mysql_field_name()Returns the name of a field in a recordset3 mysql_field_seek()Moves the result pointer to a specified field3 mysql_field_table()Returns the name of the table the specified field is in3 mysql_field_type()Returns the type of a field in a recordset3 mysql_free_result()Free result memory3 mysql_get_client_info()Returns MySQL client info4 mysql_get_host_info()Returns MySQL host info4 mysql_get_proto_info()Returns MySQL protocol info4 mysql_get_server_info()Returns MySQL server info4 mysql_info()Returns information about the last query4 mysql_insert_id() Returns the AUTO_INCREMENT ID generated from the previous INSERT operation 3 mysql_list_dbs()Lists available databases on a MySQL server3 mysql_list_fields() Deprecated. Lists MySQL table fields. Use mysql_query() instead 3 mysql_list_processes()Lists MySQL processes4 mysql_list_tables() Deprecated. Lists tables in a MySQL database. Use mysql_query() instead 3 mysql_num_fields()Returns the number of fields in a recordset3 mysql_num_rows()Returns the number of rows in a recordset3 mysql_pconnect()Opens a persistent MySQL connection3 mysql_ping() Pings a server connection or reconnects if there is no connection 4 mysql_query()Executes a query on a MySQL database3 mysql_real_escape_string()Escapes a string for use in SQL statements4 mysql_result()Returns the value of a field in a recordset3 mysql_select_db()Sets the active MySQL database3 mysql_stat()Returns the current system status of the MySQL server4 mysql_tablename() Deprecated. Returns the table name of field. Use mysql_query() instead 3 mysql_thread_id()Returns the current thread ID4 mysql_unbuffered_query()Executes a query on a MySQL database (without fetching / buffering the result) 4

PHP Programming PHP Programming MySQL Constant ConstantDescriptionPHP MYSQL_CLIENT_COMPRESSUse compression protocol4.3 MYSQL_CLIENT_IGNORE_SPACEAllow space after function names4.3 MYSQL_CLIENT_INTERACTIVE Allow interactive timeout seconds of inactivity before closing the connection 4.3 MYSQL_CLIENT_SSLUse SSL encryption (only available with version 4+ of the MySQL client library) 4.3 ConstantDescription PHP MYSQL_ASSOC Columns are returned into the array with the fieldname as the array index MYSQL_BOTH Columns are returned into the array having both a numerical index and the fieldname as the array index MYSQL_NUMColumns are returned into the array having a numerical index (index starts at 0)

PHP Programming PHP Programming example1 MySQL:connect_db

PHP Programming PHP Programming example2 MySQL: create_db

PHP Programming PHP Programming example3 MySQL:list_db Database. "\n"; } ?>

PHP Programming PHP Programming zboard <? $dbconn = mysql_connect("localhost","host","123456") || die("Not connect MySQL DB server!"); echo("$dbconn"); mysql_close($dbconn); ?>

PHP Programming PHP Programming zboard <? $dbconn = mysql_connect("localhost","host","123456") || die("Not connect MySQL DB server!"); $status = mysql_select_db("mysql",$dbconn); if(!$status) { $errNO = mysql_errno($dbconn); $errMSG = mysql_error($dbconn); echo("Failed to server's sample DB! "); echo("Error code $errNO : $errMSG "); exit; } else { echo("Connct sucess Database! "); } mysql_close($dbconn); ?>

PHP Programming PHP Programming end – Thanks You!!