Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (MySQL)"— Presentation transcript:

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

2 PHP Programming PHP Programming - 2 - MySQL client MySQL server MySQL util

3 PHP Programming PHP Programming - 3 - 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: 3.22.20a-log Type 'help' for help. mysql>

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

5 PHP Programming PHP Programming - 5 - Entering Queries mysql> select version(), current_date; +------------+------------------+ | version() | current_date | +-------------+-----------------+ | 3.22.20a-log| 1999-03-19 | +-------------+-----------------+ 1 row in set (0.01 sec) mysql>

6 PHP Programming PHP Programming - 6 - 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) | +-----------+-----------+ | 0.707107 | 25 | +------------+-----------+ mysql> select version(); select now(); +------------+ | version() | +------------+ | 3.22.20a-log| +------------+ +-----------------------+ | now() | +-----------------------+ | 1999-03-19 00:15:33 | +-----------------------+

7 PHP Programming PHP Programming - 7 - Cancel mysql> select -> user() -> \c mysql> mysql> select user() -> ; +-------------------+ | user() | +-------------------+ | joesmith@localhost | +-------------------+

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

9 PHP Programming PHP Programming - 9 - 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);

10 PHP Programming PHP Programming - 10 - 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 | | +--------+------------+-----+------+--------+-----+

11 PHP Programming PHP Programming - 11 - mysql> insert into pet -> values('Puffball', 'Diane', 'hamster', 'f', '1999-03-30', NULL); mysql> load data local infile "pet.txt" into table pet; Pet.txt nameownerspeciessexbirthdeath FluffyHaroldcatf1993-02-04 ClawsGwencatm1994-03-17 BuffyHarolddogf1989-05-13 FangBennydogm1990-08-27 BowserDiannedogm1998-08-311995-07-29 ChirpyGwenbirdf1998-09-11 WhistlerGwenbird 1997-12-09 SlimBennysnakem1996-04-29

12 PHP Programming PHP Programming - 12 - serach mysql> select * from pet; +-------+------+--------+----+--------+---------+ | name | owner | species | sex | birth | death | +-------+------+--------+----+--------+---------+ | Fluffy | Harold | cat | f | 1993-02-04 | NULL | | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | | Fang | Benny | dog | m | 1990-08-27 | NULL | | Bowser | Diane | dog | m | 1998-08-31 |1995-07-29| | Chirpy | Gwen | bird | f | 1998-09-11 | NULL | | Whistler | Gwen | bird | NULL| 1997-12-09 | NULL | | Slim | Benny | snake | m | 1996-04-29 | NULL | | Puffball | Diane | hamster | f | 1999-03-30 | NULL | +-------+------+--------+----+--------+---------+ mysql> delete from pet; mysql> load data local infile "pet.txt" into table pet;

13 PHP Programming PHP Programming - 13 - update mysql> update pet set birth="1989-08-31" where name="Bowser"; mysql> select * from pet where name = "Bower"; +--------+-------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +--------+-------+---------+------+------------+------------+ | Bowser | Diane | dog | m |1989-08-31 |1995-07-29 | +--------+-------+---------+------+------------+------------+ mysql> select * from pet where birth >= "1998-1-1" +----------+-------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +----------+-------+---------+-------+-----------+-------+ | Chirp y | Gwen | bird | f |1998-09-11 | NULL | | Puffball | Diane | hamster | f |1999-03-30 | NULL | +----------+-------+---------+-------+------------+------+

14 PHP Programming PHP Programming - 14 - mysql> select * from pet where species = "dog" AND sex = "f"; +-------+--------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +-------+--------+---------+------+------------+-------+ | Buffy | Harold | dog | f |1989-05-13 | NULL | +-------+--------+---------+------+------------+-------+ mysql> select * from pet where species = "snake" or species = "bird"; +----------+-------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +----------+-------+---------+------+------------+-------+ | Chirpy | Gwen | bird | f |1998-09-11 | NULL | | Whistler | Gwen | bird |NULL |1997-12-09 | NULL | | Slim | Benny | snake | m |1996-04-29 | NULL | +----------+-------+---------+------+------------+-------+

15 PHP Programming PHP Programming - 15 - 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 | 1994-03-17 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | +-------+--------+---------+------+------------+-------+ mysql> select name, birth from pet; +----------+------------+ | name | birth | +----------+------------+ | Fluffy | 1993-02-04 | | Claws | 1994-03-17 | | Buffy | 1989-05-13 | | Fang | 1990-08-27 | | Bowser | 1989-08-31 | | Chirpy | 1998-09-11 | | Whistler | 1997-12-09 | | Slim | 1996-04-29 | | Puffball | 1999-03-30 | +----------+------------+

16 PHP Programming PHP Programming - 16 - 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 | +--------+

17 PHP Programming PHP Programming - 17 - mysql> SELECT name, species, birth FROM pet -> WHERE species = "dog" OR species = "cat"; +--------+---------+------------+ | name | species | birth | +--------+---------+------------+ | Fluffy | cat |1993-02-04 | | Claws | cat |1994-03-17 | | Buffy | dog | 1989-05-13 | | Fang | dog |1990-08-27 | | Bowser | dog | 1989-08-31 | +--------+---------+------------+

18 PHP Programming PHP Programming - 18 - mysql> select name, birth from pet order by birth; +----------+------------+ | name | birth | +----------+------------+ | Buffy | 1989-05-13| | Bowser | 1989-08-31| | Fang |1990-08-27 | | Fluffy | 1993-02-04 | | Claws | 1994-03-17 | | Slim | 1996-04-29 | | Whistler | 1997-12-09 | | Chirpy | 1998-09-11 | | Puffball | 1999-03-30 | +----------+------------+ mysql> select name, birth from pet order BY birth desc; +----------+------------+ | name | birth | +----------+------------+ | Puffball | 1999-03-30 | | Chirpy | 1998-09-11 | | Whistler | 1997-12-09 | | Slim | 1996-04-29 | | Claws | 1994-03-17 | | Fluffy | 1993-02-04 | | Fang | 1990-08-27 | | Bowser | 1989-08-31 | | Buffy | 1989-05-13 | +----------+------------+

19 PHP Programming PHP Programming - 19 - mysql> SELECT name, species, birth FROM pet ORDER BY species, birth DESC; +----------+---------+------------+ | name | species | birth | +----------+---------+------------+ | Chirpy | bird |1998-09-11 | | Whistler | bird |1997-12-09 | | Claws | cat |1994-03-17 | | Fluffy | cat |1993-02-04 | | Fang | dog |1990-08-27 | | Bowser | dog |1989-08-31 | | Buffy | dog |1989-05-13 | | Puffball | hamster |1999-03-30 | | Slim | snake |1996-04-29 | +----------+---------+------------+

20 PHP Programming PHP Programming - 20 - 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 | +----------+-------------------------------------------+

21 PHP Programming PHP Programming - 21 - 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 | 1993-02-04 | 2 | | Claws | 1994-03-17 | 3 | | Buffy | 1989-05-13 | 5 | | Fang | 1990-08-27 | 8 | | Bowser | 1989-08-31 | 8 | | Chirpy | 1998-09-11 | 9 | | Whistler | 1997-12-09 | 12 | | Slim | 1996-04-29 | 4 | | Puffball | 1999-03-30 | 3 | +----------+------------+--------------+

22 PHP Programming PHP Programming - 22 - 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;

23 PHP Programming PHP Programming - 23 - 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 | +-----------+---------------+

24 PHP Programming PHP Programming - 24 - Pattern match mysql> SELECT * FROM pet WHERE name LIKE "b%"; +--------+--------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +--------+--------+---------+------+------------+------------+ | Buffy | Harold | dog | f | 1989-05-13 | NULL | | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | +--------+--------+---------+------+------------+------------+ mysql> SELECT * FROM pet WHERE name LIKE "%fy"; +--------+--------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +--------+--------+---------+------+------------+-------+ | Fluffy | Harold | cat | f | 1993-02-04 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | +--------+--------+---------+------+------------+-------+

25 PHP Programming PHP Programming - 25 - mysql> SELECT * FROM pet WHERE name LIKE "%w%"; +----------+-------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +----------+-------+---------+------+------------+------------+ | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | | Whistler | Gwen | bird | NULL | 1997-12-09 | NULL | +----------+-------+---------+------+------------+------------+ mysql> SELECT * FROM pet WHERE name LIKE "_ _ _ _ _"; +-------+--------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +-------+--------+---------+------+------------+-------+ | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Buffy | Harold | dog | f | 1989-05-13 | NULL | +-------+--------+---------+------+------------+-------+

26 PHP Programming PHP Programming - 26 - 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.

27 PHP Programming PHP Programming - 27 - mysql> SELECT * FROM pet WHERE name REGEXP "^[bB]"; +--------+--------+---------+------+------------+------------+ | name | owner | species | sex | birth | death | +--------+--------+---------+------+------------+------------+ | Buffy | Harold | dog | f | 1989-05-13 | NULL | | Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 | +--------+--------+---------+------+------------+------------+ mysql> SELECT * FROM pet WHERE name REGEXP "fy$"; +--------+--------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +--------+--------+---------+------+------------+-------+ | Fluffy | Harold | cat | f |1993-02-04 | NULL | | Buffy | Harold | dog | f |1989-05-13 | NULL | +--------+--------+---------+------+------------+-------+ ^.....$ = ^.{5}$

28 PHP Programming PHP Programming - 28 - 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

29 PHP Programming PHP Programming - 29 - 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 | +------+----------+

30 PHP Programming PHP Programming - 30 - 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 | +---------+------+----------+

31 PHP Programming PHP Programming - 31 - Using several table mysql> CREATE TABLE event (name VARCHAR(20), date DATE, -> type VARCHAR(15), remark VARCHAR(255)); Event.txt Fluffy 1995-05-15 litter 4 kittens, 3 female, 1 male Buffy 1993-06-23 litter 5 puppies, 2 female, 3 male Buffy 1994-06-19 litter 3 puppies, 3 female Chirpy 1999-03-21 vet needed beak straightened Slim 1997-08-03 vet broken rib Bowser 1991-10-12 kennel Fang 1991-10-12 kennel Fang 1998-08-28 birthday Gave him a new chew toy Claws 1998-03-17 birthday Gave him a new flea collar Whistler 1998-12-09 birthday First birthday

32 PHP Programming PHP Programming - 32 - 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";

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

34 PHP Programming PHP Programming - 34 - 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

35 PHP Programming PHP Programming - 35 - 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

36 PHP Programming PHP Programming - 36 - 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

37 PHP Programming PHP Programming - 37 - 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)

38 PHP Programming PHP Programming - 38 - example1 http://kr.php.net/manual/en/book.mysql.php MySQL:connect_db

39 PHP Programming PHP Programming - 39 - example2 MySQL: create_db

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

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

42 PHP Programming PHP Programming - 42 - 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); ?>

43 PHP Programming PHP Programming - 43 - - end – Thanks You!!


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

Similar presentations


Ads by Google