PHP全棧學習筆記28
資料庫Mysql概述,資料庫操作,資料表操作,資料型別,管理資料庫
order by asc; 升序 desc 降序
sql標準語言:
資料查詢語言 select
資料定義語言 create/alter/drop
資料操作語言 insert delete update
資料控制語言 grant revoke 為使用者賦予特權,收回特權
mysql支援的資料型別為主要為3類:
數字型別,字串型別,日期和時間型別。
數字型別分整數型和浮點型兩種:
整數資料型別:
tinyint bit bool smallint mediumint int bigint
浮點資料型別:
float, double, decimal
字串型別:
char varchar, text blob, set enum,
日期和時間資料型別:
date time datetime timestamp year
php資料庫程式設計
mysql_connect()函式連線mysql伺服器
<?php $conn = mysql_connect("localhost", "root", "root") or die("連線失敗".mysql_error()); ?>
mysql_select_db()函式選擇mysql資料庫
<?php $conn = mysql_connect("localhost","root","root"); $select = mysql_select_db("dashu", $conn); if($select){ echo "成功"; } ?>
mysql_query()函式
mysql_fetch_array()函式將結果集返回到陣列中
mysql_assoc將陣列採用關聯索引
mysql_num:陣列採用數字索引
mysql_both:包含關聯和數字索引的陣列
mysql_fetch_row()函式從結果集中獲取一行作為列舉陣列
while($result = mysql_fetch_row($arr)){ $result[1];
mysql_num_rows()函式獲取查詢結果集中的記錄數
insert update delete select
$arr = mysql_query("select * from tb", $conn); while($result = mysql_fetch_array($arr)){ mysql_num_rows($arr)
mysql_free_result($sql); // 關閉記錄集 mysql_close($conn); // 關閉mysql資料庫伺服器
<a href="xx.php?id=<?php echo $row->id;?>"> $id=$_GET[id];
搜尋:
<?php include_once("conn/conn.php"); // 包含資料庫連線檔案 if(isset($_POST['sub'])){ if($_POST['text'] == "" || $_POST['text'] == "請輸入關鍵字" ) { echo "<script>alert('請輸入查詢內容');</script>"; }else{ $sql = "select * from tb where name like '% ".$_POST['text']." %'"; $rs = mysql_query($sql, $conn); ?> <?php while($rst = mysql_fetch_row($rs)){ ?>
cookie和session
<?php setcookie("TMCookie", 'xx.com'); ...(, time()+60);
session會話
session設定時間
客戶端沒有禁止cookie
<?php $time=1*60; session_set_cookie_params($time); session_start(); $_SESSION[username] = "dashu"; ?>
不建議使用這個函式
<?php session_start(); $time = 1*60; setcookie(session_name(), session_id(), time()+$time, "/"); $_SESSION['user']="da"; ?>
session資料庫儲存
session_set_save_handler()
function _session_open($save_path, $session_name){ global $handle; $handle = mysql_connect('localhost','root','root') or die('連線失敗'); mysql_select_db('db',$handle) or die('失敗'); return (true); } }
function _session_close(){ global $handle; mysql_close($handle); return(true); }
<?php header("content-type:image/png");//設定頁面編碼 $im = imagecreate(65,25); imagefill($im, 0, 0, imagecolorallocate($im,200, 200, 200)); $validatorCode=$_GET['code']; imagestring($im,rand(3,5),10,3,substr($validatorCode,0,1),imagecolorallocate($im,0,rand(0,255),rand(0,255))); imagestring($im,rand(3,5),25,6,substr($validatorCode,1,1),imagecolorallocate($im,rand(0,255),0,rand(0,255))); imagestring($im,rand(3,5),36,9,substr($validatorCode,2,1),imagecolorallocate($im,rand(0,255),rand(0,255),0)); imagestring($im,rand(3,5),48,12,substr($validatorCode,3,1),imagecolorallocate($im,0,rand(0,255),rand(0,255))); for($i=0;$i<200;$i++){ imagesetpixel($im,rand()%70,rand()%30,imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255))); } imagepng($im); imgedestory(); ?>