php運算元據庫mysql

2021-07-30 13:15:09 字數 2399 閱讀 1851

mysql_connect在將來的php版本會被廢棄,建議用mysqli或者pdo代替

/*

mysql> create table people(

-> id int unsigned primary key auto_increment,

-> name varchar(20),

-> age int

-> );

*/header("content-type:text/html;charset=utf-8");

//連線資料庫

$link = mysql_connect("localhost","root","123456");

//選擇資料庫

mysql_select_db("t2");

//設定字符集(client字符集和conn.字符集);

mysql_query("set names utf8");

$name = "user4";

$age = "213";

//sql語句

$sqlselect = "insert into people(name,age) values('',''),('','')";

//執行sql語句

$result = mysql_query($sqlselect);

if($result)else

//mysql_free_result僅需要在考慮到返回很大的結果集時會占用多少記憶體時呼叫。在指令碼結束後所有關聯的記憶體都會被自動釋放。

//mysql_free_result($result);

//關閉資料庫

mysql_close($link);

header("content-type:text/html;charset=utf-8");

//連線資料庫

$link = mysql_connect("localhost","root","123456");

//選擇資料庫

mysql_select_db("t2");

//設定字符集(client字符集和conn.字符集);

mysql_query("set names utf8");

//sql語句

$sqlselect = "delete from people where id>5";

//執行sql語句

$result = mysql_query($sqlselect);

if($result)else

mysql_close($link);

header("content-type:text/html;charset=utf-8");

//連線資料庫

$link = mysql_connect("localhost","root","123456");

//選擇資料庫

mysql_select_db("t2");

//設定字符集(client字符集和conn.字符集);

mysql_query("set names utf8");

//sql語句

$sqlselect = "update people set name='linei2' where id=61";

//執行sql語句

$result = mysql_query($sqlselect);

if($result)else

mysql_close($link);

header("content-type:text/html;charset=utf-8");

//連線資料庫

$link = mysql_connect("localhost","root","123456");

//選擇資料庫

mysql_select_db("t2");

//設定字符集(client字符集和conn.字符集);

mysql_query("set names utf8");

//sql語句

$sqlselect = "select * from people";

//執行sql語句

$result = mysql_query($sqlselect);

while ($row = mysql_fetch_assoc($result))".' --- '."".' -- '."".'';}

echo

'查詢到的行數:'.mysql_num_rows($result);

//mysql_free_result僅需要在考慮到返回很大的結果集時會占用多少記憶體時呼叫。在指令碼結束後所有關聯的記憶體都會被自動釋放。

mysql_free_result($result);

mysql_close($link);

php 運算元據庫

本例是用php連線乙個mysql資料庫操作的演示,實現連線開啟乙個庫,並讀取資料的基本功能。資料庫名稱為 dbname 表名為 person 分別有7個字段 id userid age tel email address 伺服器 資料庫編碼 均採用 utf 8 mysql query set nam...

PHP運算元據庫

很簡單,只需要幾行 就搞定!1.連線資料庫 pdo new pdo mysql host localhost dbname 資料庫名 使用者名稱 密碼 2.準備sql語句 result pdo prepare select from user where id 3.執行哦sql語句 result e...

PHP運算元據庫

1.1.1.連線資料庫基本步驟 1.連線資料庫 2.準備sql語句 3.執行sql語句 4.獲取執行的結果並分析 5.關閉資料庫 1.1.2.運算元據庫常用api mysqli connect ip,使用者名稱,密碼,資料庫名 連線資料庫 mysqli query link,sql 執行sql語句 ...