#PHP 5.2.9以後 $mysqli = new mysqli($db_host, $db_user, $db_password, $db_name); if ($mysqli->connect_error) { die('無法連上資料庫 (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error); } #設定資料庫語系 $mysqli->set_charset("utf8");
$mysqli->close();
$mysqli->query("insert into,update delete") //查詢 $mysqli->affected_row //傳回更改筆數 $mysqli->insert_id //傳回insert 指令所產生之流水號
$result->num_rows //查詢結果的資料筆數 $result->field_count //欄位數量 $mysqli->errno $mysqli->error //取得select時錯誤訊息
$row=$result->fetch_row(); //陣列形態,可用$row[$n]存取 $row=$result->fetch_array(); //關聯陣列,用$row[$n]或$row[$colname]存取 //,名稱區分大小寫 $row=$result->fetch_assoc(); //關聯陣列,只能用$row[$colname]存取 $row=$result->fetch_abject(); //以物件方式存取,$row->colname ##每次呼叫都會傳回下一筆,直到最後傳回FALSE $result->data_seek($row_number)跳到指定欄位
#資料過濾 #http://php.net/manual/en/mysqli.real-escape-string.php $_POST['title'] = $mysqli->real_escape_string($_POST['title']);//字串 $_POST['target'] = intval($_POST['target']);//數字
#過濾撈出資料 $DBV['sn'] = intval($DBV['sn']); //http://www.w3school.com.cn/php/func_string_htmlspecialchars.asp $DBV['title'] = htmlspecialchars($DBV['title'], ENT_QUOTES); // 轉換雙引號和單引號
######################################## # 檢查資料表是否存在 ######################################## function chk_isTable($tbl_name="") { global $mysqli; if(!$tbl_name)return; $sql = "SHOW TABLES LIKE '{$tbl_name}'"; //die($sql); $result = $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate)); if ($result->num_rows)return true; //欄位存在 return false;//欄位不存在 }
######################################## # 檢查某欄位是否存在 ######################################## function chk_isColumn($col_name="",$tbl_name="") { global $mysqli; if(!$col_name and $tbl_name)return; //SHOW COLUMNS FROM `show_kind` LIKE 'sn1' $sql = "SHOW COLUMNS FROM `{$tbl_name}` LIKE '{$col_name}'"; $result = $mysqli->query($sql) or die(printf("Error: %s <br>".$sql, $mysqli->sqlstate)); if ($result->num_rows)return true; //欄位存在 return false;//欄位不存在 }