Skip to content

0327notes #23

@shiaoyi

Description

@shiaoyi

0327

SQL

Select X From table Where
Delete from table Where
Update table set X Where
Insert into table() values (X)

phpMyAdmin

位址:localhost:8080/phpmyadmin/
default account: root
defaul password: 空

  • 定義:用php寫的寫的網頁,用網頁的介面管理你的資料庫
  • 操作:創一個資料庫mentor->使用utf8_unicode_ci避免亂碼
  • 型態:
    INT->整數
    TEXT->很大的純文字欄位,一次會配65000字元的大小,浪費空間,但很方便
    VARCHAR->可以指定大小,很省空間
  • id要按Auto Increment(AI)來自動增加

php連資料庫

把下列程式碼寫在yeee檔案夾裡的conn.php

<?php
$servername = "localhost";
$username = "username"; //root
$password = "password"; //空
$dbname = "myDB"; //mentor

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection 有error就die,然後把錯誤列出來
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

讀取指定的sql用query

$sql = "SELECT * from users";
$result = $conn->query($sql); 

讀取每列資訊用fetch_assoc,計算有幾列用num_rows,要抓資訊裡的某一項要用['項目']

if($result->num_rows>0){
    while($rows = $result->fetch_assoc()){
        echo 'id: ' . $rows['id'] . ', username: ' . $rows['username'] . '<br>';
    }
}else{
    echo '0 results';
}

需要某檔案時用require,就可引用檔案裡的變數

require("conn.php");
  • 如果require很多檔案,當有可能造成檔案裡的內容有些重複時,可以用require_once來讓內容不會被重複讀取

php程式碼連到資料庫時要用什麼編碼去連線

用中文

mysqli_set_charset($conn,"utf8")
  • 編碼有三階段一為資料庫裡、二為php連線至資料庫、三為前端用meta設定

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions