數(shù)據(jù)庫的增刪改查,也就是數(shù)據(jù)存儲的操作,應(yīng)該是php最重要的功能。就實(shí)現(xiàn)來說不算是什么復(fù)雜和難搞的技術(shù)。但是俗話說的好,最簡單的也是最復(fù)雜的。當(dāng)遇到一些復(fù)雜的業(yè)務(wù)邏輯時(shí),這簡單的增刪改查,也會變的極其復(fù)雜,甚至變成整個(gè)項(xiàng)目的核心技術(shù)。
創(chuàng)新互聯(lián)長期為上1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為德清企業(yè)提供專業(yè)的做網(wǎng)站、成都網(wǎng)站建設(shè),德清網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
其實(shí)說到這里,你應(yīng)該就能理解了,PHP,其實(shí)也是所有編程語言中,最復(fù)雜最難搞的,其實(shí)是業(yè)務(wù)邏輯。你要實(shí)現(xiàn)一個(gè)功能,只要是能實(shí)現(xiàn)的,一般網(wǎng)上都有會一些demo,但是你要處理的業(yè)務(wù)需求,就需要用自己的經(jīng)驗(yàn)來解決了,甚至有些客戶連自己真正的需求也不知道,他們只能說出他們所想要實(shí)現(xiàn)的功能大概長什么樣子,功能怎么實(shí)現(xiàn),這可不是查查資料就可以找到的。
一、PHP操作MySql數(shù)據(jù)庫
新增數(shù)據(jù)
?php
$query
=
"INSERT
INTO
grade
(name,email,point,regdate)
VALUE
('
李三','yc60.com@gmail.com',,NOW())"
;
@mysql_query($query)
or
die(
'添加數(shù)據(jù)出錯(cuò):'
.mysql_error());
?
修改數(shù)據(jù)
?php
$query
=
"UPDATE
grade
SET
name='小可愛'
WHERE
id=6"
;
@mysql_query($query)
or
die(
'修改出錯(cuò):'
.mysql_error());
?
刪除數(shù)據(jù)
?php
$query
=
"DELETE
FROM
grade
WHERE
id=6";
@mysql_query($query)
or
die(
'刪除錯(cuò)誤:'
.mysql_error());
?
顯示數(shù)據(jù)
?php
$query
=
"SELECT
id,name,email,point
FROM
grade";
$result
=
@mysql_query($query)
or
die(
'查詢語句出錯(cuò):'
.mysql_error());
while
(!!
$row
=
mysql_fetch_array($result))
{
echo
$row[
'id'
].
'----'
.$row['name'
].'----'
.$row
['email'
].
'----'
.$row['point'
];
echo
'br
/
';
}
?
二、其他常用函數(shù)
mysql_f
etch_row()
:從結(jié)果集中取得一行作為枚舉數(shù)組
mysql_f
etch_assoc()
:
從結(jié)果集中取得一行作為關(guān)聯(lián)數(shù)組
mysql_f
etch_array()
:
從結(jié)果集中取得一行作為關(guān)聯(lián)數(shù)組,或數(shù)字?jǐn)?shù)組,或二者兼有
mysql_f
etch_lengths
()
:
取得結(jié)果集中每個(gè)輸出的長度
mysql_f
ield_name():
取得結(jié)果中指定字段的字段名
mysql_num_rows():
取得結(jié)果集中行的數(shù)目
mysql_num_f
ields():取得結(jié)果集中字段的數(shù)目
mysql_get_client_inf
o()
:
取得
MySQL
客戶端信息
mysql_get_host_info():
取得
MySQL
主機(jī)信息
mysql_get_proto_info():
取得
MySQL
協(xié)議信息
mysql_get_server_inf
o()
:
取得
MySQL
服務(wù)器信息
php有專門的sql server操作函數(shù),舉個(gè)簡單的例子,是查詢的
$serverName?=?"localhost";?//數(shù)據(jù)庫服務(wù)器地址
$uid?=?"root";?//數(shù)據(jù)庫用戶名
$pwd?=?"123456";?//數(shù)據(jù)庫密碼
$connectionInfo?=?array("UID"=$uid,?"PWD"=$pwd,?"Database"='databasename');
$conn?=?sqlsrv_connect(?$serverName,?$connectionInfo);
if(?$conn?==?false){
echo?"連接數(shù)據(jù)庫失?。?;
die(?print_r(?sqlsrv_errors(),?true));
}
$sql?=?"select?*?from?user";
$query?=?sqlsrv_query(?$conn,?$sql?,?array(),?array(?"Scrollable"?=?SQLSRV_CURSOR_KEYSET?));
$num_rows?=?sqlsrv_num_rows($query);
if($num_rows??0){
while?($row?=?sqlsrv_fetch_array($query)){
echo?$row['aaaa'];
}
}
其它的操作也同理,舉一反三
class sqlHelper{ \x0d\x0a public $conn; \x0d\x0a public $dbname="數(shù)據(jù)庫名稱"; \x0d\x0a public $username="數(shù)據(jù)庫用戶名"; \x0d\x0a public $password="數(shù)據(jù)庫密碼"; \x0d\x0a public $host="localhost"; \x0d\x0a //連接數(shù)據(jù)庫 \x0d\x0a public function __construct(){ \x0d\x0a $this-conn=mysql_connect($this-host,$this-username,$this-password); \x0d\x0a if(!$this-conn){ \x0d\x0a die("連接失敗".mysql_error()); \x0d\x0a } \x0d\x0a mysql_select_db($this-dbname,$this-conn); \x0d\x0a } \x0d\x0a //執(zhí)行查詢語句 \x0d\x0a public function execute_dql($sql){ \x0d\x0a $res=mysql_query($sql,$this-conn); \x0d\x0a return $res; \x0d\x0a } \x0d\x0a //執(zhí)行增填改語句 \x0d\x0a public function execute_dml($sql){ \x0d\x0a $b=mysql_query($sql,$this-conn); \x0d\x0a if(!$b){ \x0d\x0a return 3; \x0d\x0a }else{ \x0d\x0a if(mysql_affected_rows($this-conn)){ \x0d\x0a return 1;//表示OK \x0d\x0a }else{ \x0d\x0a return 2;//表示沒有行收到影響 \x0d\x0a } \x0d\x0a } \x0d\x0a }\x0d\x0a}
?php
$con = mysql_connect("localhost:3306","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$result = mysql_query("SELECT * FROM user");
echo "table border='1'
tr
thUsername/th
thPassword/th
/tr";
while($row = mysql_fetch_array($result)) {
echo "tr";
echo "td" . $row['username'] . "/td";
echo "td" . $row['password'] . "/td";
echo "/tr";
}
echo "/table";
mysql_close($con);
?
從服務(wù)器中獲取用戶所有信息(SQL SELECT語句)并以表格形式出現(xiàn)
?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
mysql_query("DELETE FROM user WHERE username = '$_POST[username]'");
mysql_close($con);
?
刪除該用戶所有信息delete.php
?php
$con = mysql_connect("localhost:3306","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$sql = "INSERT INTO user (username,password)
VALUES
('$_POST[username]','$_POST[password]')";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
?
注冊一個(gè)新用戶insert.php
?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
mysql_query("UPDATE user SET password = '$_POST[password]' WHERE username = '$_POST[username]'");
mysql_close($con);
?
修改一個(gè)用戶密碼update.php
html
head
titleFORM/title
/head
body
br /
h1Insert:/h1
form action="insert.php" method="post"
username:input type="name" name="username"/
br /
password:input type="password" name="password"/
input type="submit" value="submit"/
/form
br /hr /br /
h1Delete/h1
form action="delete.php" method="post"
username:input type="name" name="username" /
br /
Are you sure?input type="submit" value="sure" /
/form
br /hr /br /
h1Update/h1
form action="update.php" method="post"
username:input type="name" name="username"/
br /
You want to change your password into:input type="password" name="password"/
input type="submit" value="submit"/
/form
br /hr /br /
/body
/html
以上三個(gè)功能的提交源Operate.html
當(dāng)前名稱:php數(shù)據(jù)庫的增刪改 php+mysql增刪改查
網(wǎng)站鏈接:http://jinyejixie.com/article22/dossdcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、網(wǎng)站制作、定制開發(fā)、自適應(yīng)網(wǎng)站、App設(shè)計(jì)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)