-
-
Save summer10920/ea13f8d10c357e7a7f0ec0bcb56aec0c to your computer and use it in GitHub Desktop.
20200513-php-baseclass-10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$dblink = new PDO("mysql:host=127.0.0.1;dbname=php_study;charset=utf8", "root", ""); | |
//宣告PDO連結,之後每次要處理SQL連線,透過這個變數$dblink來處理,這是一個class大物件 | |
/* | |
$sqlcode = " | |
CREATE TABLE php_study.ch8_animal ( | |
id INT UNSIGNED AUTO_INCREMENT, | |
name TEXT, | |
weight INT, | |
info TEXT, | |
date DATE, | |
PRIMARY KEY (id) | |
); | |
"; | |
$result = $dblink->query($sqlcode); | |
if(!$result) print_r($dblink->errorInfo());// 找錯誤問題的方法 | |
//目前為止可以透過$dblink來進行SQL指令傳遞,接著請把上面2行都註解,只留PDO宣告就好 | |
*/ | |
//$result = $dblink->query("INSERT INTO ch8_animal VALUES (null,'熊貓',125,'黑白色的熊',NOW())"); //新增 | |
//$result = $dblink->query("UPDATE ch8_animal SET weight=185 WHERE name='熊貓'"); //修改 | |
//$result = $dblink->query("DELETE FROM ch8_animal WHERE id=1"); //刪除 | |
// create animal*3 | |
/* | |
$sql = " | |
INSERT INTO ch8_animal VALUES | |
(null,'藪貓',52,'夜行性動物,喜歡狩獵遊戲,口頭禪是好厲害唷',NOW()), | |
(null,'河馬',155,'個性不算溫和,咬合力很強唷',NOW()), | |
(null,'浣熊',123,'由於會偷人類人物,所以常常被說是個小偷',NOW()), | |
(null,'耳廓狐',33,'擅長透過挖洞來尋找潛藏在地底下的獵物',NOW()) | |
; | |
"; | |
$result = $dblink->query($sql); | |
*/ | |
//select | |
$result = $dblink->query("SELECT * FROM ch8_animal WHERE 1"); | |
if (!$result) print_r($dblink->errorInfo()); // 找錯誤問題的方法 | |
// by fetch | |
/* | |
while ($row = $result->fetch()) { | |
print_r($row); | |
} | |
*/ | |
// by fetchAll | |
$row = $result->fetchAll(); | |
print_r($row); | |
/* one line finish with fetchAll | |
$result = $dblink->query("SELECT * FROM ch5_animal WHERE 1")->fetchAll(); | |
print_r($result); | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment