PHP + MySQL

About

Erickson

Junior Student

  • C / C++
  • Web
    • PHP
    • JavaScript
    • MySQL
  • Java (Novice)
  • Android App (Novice)

HTTP Server

Client

HTTP Server

Client

1.HTTP REQUEST

HTTP Server

Client

2.HTTP RESPONSE

Database

HTTP Server

Client

Database

Client

1.HTTP Request

HTTP Server

Database

Client

2.Request Data

HTTP Server

Database

Client

HTTP Server

3.Give Data

Database

Client

HTTP Server

4.HTTP Response

Database

Client

HTTP Server

  • MySQL

  • MariaDB

  • MSSQL

  • Apache

  • Nginx

  • IIS

  • lighttpd

  • Opera Next

  • Chrome

  • Firefox

  • Safari

HTTP Server

  • PHP

    Nginx, Apache , IIS
  • ASP.NET

    IIS, Apache(Not Recommend)
  • Ruby (Ruby on Rails)

    Nginx, Apache

Build Environment

PHP

MySQL

Ajax(using jQuery)

Build Environment

  • Apache
  • PHP
  • MySQL

Download XAMPP

And Install  it

XAMPP CP

http://localhost

Where is website root?

/xampp/apache/conf/httpd.conf

PHP

PHP  Syntax

<?php 
    //這是註解

    /*
        這是
        多行註解
    */

    echo "這是輸出用的";
?>

PHP  Syntax

  • echo
  • variable
  • array
  • loop(for / while  / do-while / foreach)
  • array with loop (foreach)
  • single & double quote

PHP  Syntax - Variable

<?php
    $myVariable = '我是變數';    //宣告一個變數
    echo $myVariable . '<br/>';
    $myVariable = 5;    //改變值
    echo $myVariable . '<br/>';
?>
我是變數
5

Variable in PHP is weakly 

可是我想宣告很多變數呢?

像是要把很多的人名字都記下來

<?php
    $myVariable1 = '隔壁老王';
    $myVariable2 = '小白';
    $myVariable3 = '眉毛';
    $myVariable4 = '肥姿姿';
    $myVariable5 = '胖嘟嘟';
    $myVariable6 = '鍇嘉';
    $myVariable7 = '黑黑der志宇';
    $myVariable8 = '柏宇神';
    $myVariable9 = '小橘子';
    $myVariable10 = 'Orange 神';
    .
    .
    .
    .
    .
    .
    $myVariable100 = '右鈞神';
?>

(拼命的打變數)

PHP  Syntax - Array

0

1

2

3

4

99

......

為了減少工程師爆肝機會

所以有了Array這樣的用法

myVariable

PHP  Syntax - Array

0

1

2

3

4

99

......

為了減少工程師爆肝機會

所以有了Array這樣的用法

myVariable

index, key

PHP  Syntax - Array

0

1

2

3

4

99

......

為了減少工程師爆肝機會

所以有了Array這樣的用法

myVariable[0]

PHP  Syntax - Array

0

1

2

3

4

99

......

為了減少工程師爆肝機會

所以有了Array這樣的用法

myVariable[1]

PHP  Syntax - Array

0

1

2

3

4

99

......

為了減少工程師爆肝機會

所以有了Array這樣的用法

myVariable[2]

PHP  Syntax - Array

0

1

2

3

4

99

......

為了減少工程師爆肝機會

所以有了Array這樣的用法

myVariable[3]

PHP  Syntax - Array

0

1

2

3

4

99

......

為了減少工程師爆肝機會

所以有了Array這樣的用法

myVariable[4]

PHP  Syntax - Array

0

1

2

3

4

99

......

為了減少工程師爆肝機會

所以有了Array這樣的用法

myVariable[99]

PHP  Syntax - Array

<?php
    $myArray = [1, 2, '字串也可以唷'];
    $myArray2 = [1 => 'One',
                 2 => 'two',
                 'string' => '字串也可以當index'];
    $myArray3 = array(1 => 'One ~',
                      2 => 'two ~',
                      'string' => 'string也可以當index');
    $myArray[] = 8;
    $myArray2[] = 9;
    $myArray3[] = 10;
    print_r($myArray);
    print_r($myArray2);
    print_r($myArray3);
?>
Array
(
    [0] => 1
    [1] => 2
    [2] => 字串也可以唷
    [3] => 8
)
Array
(
    [1] => One
    [2] => two
    [string] => 字串也可以當index
    [3] => 9
)
Array
(
    [1] => One ~
    [2] => two ~
    [string] => string也可以當index
    [3] => 10
)

PHP  Syntax - loop

<?php
    for($i = 0 ;$i < 10 ; $i++) {
        echo '$i = ' . $i . '<br>';
    }
?>

PHP  Syntax - loop

<?php
    for($i = 0 ;$i < 10 ; $i++) {
        echo '$i = ' . $i . '<br>';
    }
?>

初始值

PHP  Syntax - loop

<?php
    for($i = 0 ;$i < 10 ; $i++) {
        echo '$i = ' . $i . '<br>';
    }
?>

執行條件

PHP  Syntax - loop

<?php
    for($i = 0 ;$i < 10 ; $i++) {
        echo '$i = ' . $i . '<br>';
    }
?>

執行完後?

PHP  Syntax - loop

<?php
    for($i = 0 ;$i < 10 ; $i++) {
        echo '$i = ' . $i . '<br>';
    }
?>

for-loop執行順序

A

B

C

D

A→B→D→C→(回到B繼續)

$i = 0
$i = 0
$i = 1
$i = 0
$i = 1
$i = 2
$i = 0
$i = 1
$i = 2
$i = 3
$i = 0
$i = 1
$i = 2
$i = 3
$i = 4
$i = 0
$i = 1
$i = 2
$i = 3
$i = 4
$i = 5
$i = 0
$i = 1
$i = 2
$i = 3
$i = 4
$i = 5
$i = 6
$i = 0
$i = 1
$i = 2
$i = 3
$i = 4
$i = 5
$i = 6
$i = 7
$i = 0
$i = 1
$i = 2
$i = 3
$i = 4
$i = 5
$i = 6
$i = 7
$i = 8
$i = 0
$i = 1
$i = 2
$i = 3
$i = 4
$i = 5
$i = 6
$i = 7
$i = 8
$i = 9

PHP  Syntax - loop

<?php
    $i = 0 ;
    while($i < 10) {
        echo '$i = ' . $i . '<br>';
        $i++;
    }
?>

PHP  Syntax - loop

<?php
    $i = 0 ;
    while($i < 10) {
        echo '$i = ' . $i . '<br>';
        $i++;
    }
?>

執行條件

$i = 0
$i = 1
$i = 2
$i = 3
$i = 4
$i = 5
$i = 6
$i = 7
$i = 8
$i = 9

PHP  Syntax - loop

<?php
    $i = 0;
    do{
        echo '$i = ' . $i . '<br>';
        $i++;
    }while($i < 10);
?>

執行條件

PHP  Syntax - loop

<?php
    $i = 0;
    do{
        echo '$i = ' . $i . '<br>';
        $i++;
    }while($i < 10);
?>
$i = 0
$i = 1
$i = 2
$i = 3
$i = 4
$i = 5
$i = 6
$i = 7
$i = 8
$i = 9

But.......

PHP  Syntax - loop

<?php
    $i = 10;
    do{
        echo '$i = ' . $i . '<br>';
        $i++;
    }while($i < 10);
?>
$i = 10

Pratice

試著用loop印100*100乘法表

PHP  Syntax - Array with Loop

<?php
    $array_demo3 = array(
        'name' => 'erickson Juang',
        'department' => 'EE',
        'club' => 'ITAC');

    foreach ($array_demo3 as $key => $value) {
        echo $key . '=' . $value . '<br>';
    }
?>

PHP  Syntax - Array with Loop

<?php
    $array_demo3 = array(
        'name' => 'erickson Juang',
        'department' => 'EE',
        'club' => 'ITAC');

    foreach ($array_demo3 as $key => $value) {
        echo $key . '=' . $value . '<br>';
    }
?>

Array名稱

PHP  Syntax - Array with Loop

<?php
    $array_demo3 = array(
        'name' => 'erickson Juang',
        'department' => 'EE',
        'club' => 'ITAC');

    foreach ($array_demo3 as $key => $value) {
        echo $key . '=' . $value . '<br>';
    }
?>

代替index的變數名稱

PHP  Syntax - Array with Loop

<?php
    $array_demo3 = array(
        'name' => 'erickson Juang',
        'department' => 'EE',
        'club' => 'ITAC');

    foreach ($array_demo3 as $key => $value) {
        echo $key . '=' . $value . '<br>';
    }
?>

代替值的變數名稱

name=erickson Juang
department=EE
club=ITAC

PHP  Syntax - Array with Loop

PHP  Syntax - Array with Loop

<?php
    $array_demo3 = array(
        'name' => "erickson Juang",
        'department' => 'EE',
        'club' => 'ITAC');

    foreach ($array_demo3 as $value) {
        echo $value . '<br>';
    }
?>
erickson Juang
EE
ITAC

PHP  Syntax - Array with Loop

PHP  Syntax

Single&Double Quote

$name = 'erickson';
$club = "ITAC";

echo $name . '<br />';
echo $name . ' is member of ' . $club . '<br />';
erickson
erickson is member of ITAC

PHP  Syntax

Single&Double Quote

$name = 'erickson';
$club = "ITAC";
$statement_1 = "$name is member of $club";
$statement_2 = '$name is member of $club';

echo $statement_1 . '<br>';
echo $statement_2 . '<br>';
erickson is member of ITAC
$name is member of $club

PHP  Syntax

Function

<?php
$ans = 1;
for($i = 2; $i < 99; $i++)
	$ans *= $i;
echo $ans;
?>

PHP  Syntax

Function

<?php
//php function demo

echo factorial(99);

function factorial($n = 0) {
	$ans = 1;
	for($i = 1; $i < $n; $i++)
		$ans *= $i;

	return $ans;
}
?>

Implement Contract Submit

用POST 或 GET   

addMessage.php

PHP  From

  • GET   variables
  • POST  variables
<form role="form" method="GET" action="php_demo2.php">

    <div>
        <label for="Name">Name</label>
        <input type="text" id="Name" 
            name="Name" placeholder="Enter Name">
    </div>

    <div>
        <label for="Email">Email address</label>
        <input type="email" id="Email" 
            name="Email" placeholder="Enter Email">
    </div>

    <label for="Messages">Message</label>
    <textarea rows="5" id="Message" 
        name="Message" placeholder="Message">
    </textarea>

    <input style="margin-top:30px;" 
        type="submit" value="Submit">

</form>
<form role="form" method="GET" action="php_demo2.php">

    <div>
        <label for="Name">Name</label>
        <input type="text" id="Name" 
            name="Name" placeholder="Enter Name">
    </div>

    <div>
        <label for="Email">Email address</label>
        <input type="email" id="Email" 
            name="Email" placeholder="Enter Email">
    </div>

    <label for="Messages">Message</label>
    <textarea rows="5" id="Message" 
        name="Message" placeholder="Message">
    </textarea>

    <input style="margin-top:30px;" 
        type="submit" value="Submit">

</form>

 

 

 

 

 

.

<form role="form" method="GET" action="php_demo2.php">

    <div>
        <label for="Name">Name</label>
        <input type="text" id="Name" 
            name="Name" placeholder="Enter Name">
    </div>

    <div>
        <label for="Email">Email address</label>
        <input type="email" id="Email" 
            name="Email" placeholder="Enter Email">
    </div>

    <label for="Messages">Message</label>
    <textarea rows="5" id="Message" 
        name="Message" placeholder="Message">
    </textarea>

    <input style="margin-top:30px;" 
        type="submit" value="Submit">

</form>

 

 

 

 

.

<form role="form" method="GET" action="php_demo2.php">

    <div>
        <label for="Name">Name</label>
        <input type="text" id="Name" 
            name="Name" placeholder="Enter Name">
    </div>

    <div>
        <label for="Email">Email address</label>
        <input type="email" id="Email" 
            name="Email" placeholder="Enter Email">
    </div>

    <label for="Messages">Message</label>
    <textarea rows="5" id="Message" 
        name="Message" placeholder="Message">
    </textarea>

    <input style="margin-top:30px;" 
        type="submit" value="Submit">

</form>

 

 

.

 

 

 

 

 

 

 

<form role="form" method="GET" action="php_demo2.php">

    <div>
        <label for="Name">Name</label>
        <input type="text" id="Name" 
            name="Name" placeholder="Enter Name">
    </div>

    <div>
        <label for="Email">Email address</label>
        <input type="email" id="Email" 
            name="Email" placeholder="Enter Email">
    </div>

    <label for="Messages">Message</label>
    <textarea rows="5" id="Message" 
        name="Message" placeholder="Message">
    </textarea>

    <input style="margin-top:30px;" 
        type="submit" value="Submit">

</form>

 

 

.

<form role="form" method="GET" action="php_demo2.php">

    <div>
        <label for="Name">Name</label>
        <input type="text" id="Name" 
            name="Name" placeholder="Enter Name">
    </div>

    <div>
        <label for="Email">Email address</label>
        <input type="email" id="Email" 
            name="Email" placeholder="Enter Email">
    </div>

    <label for="Messages">Message</label>
    <textarea rows="5" id="Message" 
        name="Message" placeholder="Message">
    </textarea>

    <input style="margin-top:30px;" 
        type="submit" value="Submit">

</form>

 

 

.

HTTP Server

HTTP

REQUEST

REQUEST URL = action = php_demo2.php

php_demo2.php

<?php
echo "<pre>";
	//GET方法
	var_dump($_GET);
	//POST方法
	var_dump($_POST);
echo "</pre>";
?>
array(3) {
  ["Name"]=>
  string(8) "TestName"
  ["Email"]=>
  string(16) "test@address.com"
  ["Message"]=>
  string(15) "My messages"
}
array(0) {
}

Processing Content

Filter

  • XSS
  • SQL Injection

How To Filter

int preg_match ( string $pattern,
    string $subject 
    [, array $matches 
    [, int $flags = 0 
    [, int $offset = 0 ]]] );
  • pattern       過濾條件(用正規表達式)
  • subject        要過濾的對象
  • match           找到目標後儲存的變數(不一定要有)
  • flags               0或PREG_OFFSET_CAPTURE
  • offset            偏移,從n個字元開始找
$subject = 'testaabbddaabbcc';
$pattern = '/[a-b]+/';
$result = '';
preg_match($pattern, $subject, $result, 0, 0);
print_r($result);
preg_match($pattern, $subject, $result, PREG_OFFSET_CAPTURE, 0);
print_r($result);
Array
(
    [0] => aabb
)
Array
(
    [0] => Array
        (
            [0] => aabb
            [1] => 4
        )

)

Other Ideas

Encode input

string htmlspecialchars ( string $string 
            [, int $flags = ENT_COMPAT | ENT_HTML401 
            [, string $encoding = ini_get("default_charset") 
            [, bool $double_encode = true ]]] )

Other Ideas

Encode input

string htmlspecialchars ( string $string 
            [, int $flags = ENT_COMPAT | ENT_HTML401 
            [, string $encoding = ini_get("default_charset") 
            [, bool $double_encode = true ]]] )

But, We usually use

string htmlspecialchars ( string $string )

 Database

How to Plan?

Column

  • Name - String

  • Email - String

  • Messages - String (LONG)

Create Table via phpmyadmin

(DEMO)

Link Database and PHP

via PDO

PDO (PHP Data Object)

有什麼需要我幫忙的?

幫我去

AA資料庫

找bbb出來

帳號是123

密碼是456

好喔

我要

AA資料庫裡面的bbb

帳號是123

密碼是456

好的

稍等我一下

你要的東西我拿回來了!

創造PDO物件(創造小人)

$dsn = DatabaseType 
       .':host=' . Host 
       . ';dbname=' 
       . DatabaseName;

$dbh = new PDO ($dsn,
                 DatabaseUser, 
                 DatabasePassword);

創造PDO物件(創造小人)

define('Host', '127.0.0.1');
define('DatabaseType', 'mysql');
define('DatabaseUser', 'itac_demo');
define('DatabasePassword', 'password');
define('DatabaseName', 'itac_demo');

$dsn = DatabaseType 
        .':host=' . Host 
        . ';dbname=' . DatabaseName;
$dbh = new PDO ($dsn,
                 DatabaseUser,
                 DatabasePassword);
define('Host', '127.0.0.1');
define('DatabaseType', 'mysql');
define('DatabaseUser', 'itac_demo');
define('DatabasePassword', 'password');
define('DatabaseName', 'itac_demo');

$dsn = DatabaseType 
        .':host=' . Host 
        . ';dbname=' . DatabaseName;
$dbh = new PDO ($dsn,
                 DatabaseUser,
                 DatabasePassword);

.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.

創造PDO物件(創造小人)

通常會把藍框框裡面的東西寫在config.php裡面

config.php 系瞎咪挖溝?

  • 資料庫帳號

  • 資料庫密碼

  • 資料庫伺服器IP

  • 資料庫名稱

  • 網站名稱

通常在寫code的時候,很可能會有一些東西(環境參數)時常用到,像是:

這時就會把他寫在config.php

然後再載入使用

用PDO查資料 (指派任務給小人)

先把命令寫出來

把命令告訴小人

$sql = 'SELECT * FROM `article` WHERE `id` = ?';
$sth = $dbh->prepare($sql);
$sth->execute(array('1'));

讓小人去執行命令(同時把參數放進去)

取得小人所帶回來的資料

$result = $sth->fetch();  //只抓第一筆資料
$result = $sth->fetchAll(); //抓出所有資料

來實際做一下看看吧?

MVC Introduction

.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Controller

  • 決定程式架構

  • 仲介於Model和View
     

接收User的要求,呼叫對應的Model處理要求,最後回傳給View

View - User Interface

  • 使用者接觸的介面
  • 通常稱「前端」

最吃設計能力的部份,除了美觀外也要兼顧UX,好的UX是產品成功的第一步,最成功的例子就是Apple。

UX = user experience,使用者經驗,或使用者體驗。

Model

  • 可以重複使用程式模組
  • 把Controller的部份工作抽出。
  • 通常會把Controller中和資料庫相關的程式抽出做成Model

function即是最簡單的Model實作方法,大型程式通常利用物件(Class)的方式來將實作Model。

.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

index.html

.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

index.html

addMessage.php

addMessage.php

.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

index.html

addMessage.php

.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.

  • 接收留言資料,並存入資料庫

  • 回傳留言記錄

其中可以視為Model的部份:

其中可以視為Controller的部份:

  • 把留言存入資料庫
  • 從資料庫查詢(撈出)留言
  • 接收留言資料
  • 處理留言資料(過濾、encode)
  • 回傳留言紀錄

index.html

addMessage.php

.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.

使用者所看到的畫面

index.html

addMessage.php

.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.

使用者所看到的畫面

 

 

操作介面

 

 

index.html

addMessage.php

.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.

使用者所看到的畫面

 

 

 

資料呈現

 

 

.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

後端程式設計師

.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

前端程式設計師

.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

後端程式設計師

程式語言

  • ASP

  • PHP

  • Python

  • Ruby

  • nodejs

  • java

.

.

.

後端程式設計師

  • 資料庫規劃
  • 程式架構設計

還有...

以及其他跟使用者介面無關的事情..............

前端程式設計師

下一步,你可以....

試著研究各種Framework

PHP

JavaScript

.

.

.

 

CSS

.

.

.

 

Resource

ITAC#7 PHP + MySQL

By Erickson Juang

ITAC#7 PHP + MySQL

  • 1,448