Head First PHP&MySQL学習ノート(二)——Connect MySQL

5515 ワード

MySQL
MySQLへのログイン
$
mysql -h localhost -u root -p
-h:サーバアドレス、ネイティブにインストールされている場合は、ネイティブにログインしてlocalhostを直接使用できます.
-u:ユーザーを指定します.MySQLはインストール時に管理者のユーザー名とパスワードを入力するように要求します.このユーザー名を使用すればいいです.
-p:パスワードを入力する必要があります.コマンドを入力すると、ユーザーのパスワードを入力するよう求められます.
sunny@sunny-virtualbox:~$
 mysql -h localhost -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or\g.
Your MySQL connection id is 48
Server version: 5.5.37-0ubuntu0.14.04.1 (Ubuntu)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 
show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aliendatabase |
| mysql |
| performance_schema |
| phpmyadmin |
+--------------------+
5 rows in set (0.08 sec)
mysql>
 use aliendatabase;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
 select * from aliens_abduction;
+------------+-----------+------------------+----------+----------+--------------------------+----------------------------------------+--------------+--------------------------------------+-----------------------+
| first_name | last_name | when_it_happened | how_long | how_many | alien_description | what_they_did | fang_spotted | other | email |
+------------+-----------+------------------+----------+----------+--------------------------+----------------------------------------+--------------+--------------------------------------+-----------------------+
| Salley | Jones | 3 days ago | 1 day | four | green with six tentacles | We just talked and palyed with the dog | yes | I may have seen your dog, Contact me | [email protected] |
| maggie | xiang | 5 days ago | 2 days | 4 | 3 heads, 6 arms | Talk with them | yes | Contact me, Please! | [email protected] |
+------------+-----------+------------------+----------+----------+--------------------------+----------------------------------------+--------------+--------------------------------------+-----------------------+
2 rows in set (0.00 sec)
mysql>
データベースの作成
mysql> 
CREATE DATABASE aliendatabase;
mysql> 
USE aliendatabase;
テーブルの作成
mysql>
 CREATE TABLE 
aliens_abduction (
 first_name varchar(30),
 last_name varchar(30),
 when_it_happened varchar(30),
 how_long varchar(30),
 how_many varchar(30),
 alien_description varchar(100),
 what_they_did varchar(100),
 fang_spotted varchar(10),
 other varchar(100),
 email varchar(50)
);
データの挿入
mysql> INSERT INTO
 
aliens_abduction(first_name, last_name, when_it_happened, how_long, how_many, alien_description, what_they_did, fang_spotted, other, email) VALUES('Salley', 'Jones', '3 days ago', '1 day', 'four', 'green with six tentacles', 'We just talked and palyed with the dog', 'yes', 'I may have seen your dog, Contact me', '[email protected]')
MySQL in PHP
mysqli_接続()MySQLデータベースへの接続
mysqli_Query()クエリーを実行します(ここでのクエリーは、追加、削除、変更、検索の「検索」ではなく、すべての操作を指します).
mysqli_close()MySQLデータベース接続を閉じる
Tips:MySQLと対話するPHP関数はすべて「mysqli_」冒頭の.(以前のMySQLと通信するPHP関数のセットはすべて「mysql_」冒頭の、「i」はありません.この「i」は「improved」の改善を表す.mysqliシリーズ関数を使用する傾向があります)
<?php
  $first_name = $_POST['firstname'];
  $last_name = $_POST['lastname'];
  $when_it_happened = $_POST['whenithappened'];
  $how_long = $_POST['howlong'];
  $how_many = $_POST['howmany'];
  $alien_description = $_POST['aliendescription'];
  $what_they_did = $_POST['whattheydid'];
  $fang_spotted = $_POST['fangspotted'];
  $email = $_POST['email'];
  $other = $_POST['other'];

  $dbc = mysqli_connect('data.aliensabductedme.com', 'owen', 'aliensrool', 'aliendatabase')
    or die('Error connecting to MySQL server.');

  $query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, " .
    "how_many, alien_description, what_they_did, fang_spotted, other, email) " .
    "VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
    "'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";

  $result = mysqli_query($dbc, $query)
    or die('Error querying database.');

  mysqli_close($dbc);
?>

Note:
PHPのSQL文はセミコロンで終わりません.これはPHPスクリプト文とは異なります.
PHPはC,C++およびUnix Shellスタイル(Perlスタイル)のコメントをサポートします.
<?php
    echo "This is a test"; // This is a one-line c++ style comment
    /* This is a multi line comment
       yet another line of comment */
    echo "This is yet another test";
    echo 'One Final Test'; # This is a one-line shell-style comment
?>