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!!