?php
創(chuàng)新互聯(lián)公司專(zhuān)注為客戶(hù)提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、靖州網(wǎng)絡(luò)推廣、微信小程序開(kāi)發(fā)、靖州網(wǎng)絡(luò)營(yíng)銷(xiāo)、靖州企業(yè)策劃、靖州品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供靖州建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:www.cdcxhl.com
$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ù)器中獲取用戶(hù)所有信息(SQL SELECT語(yǔ)句)并以表格形式出現(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);
?
刪除該用戶(hù)所有信息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);
?
注冊(cè)一個(gè)新用戶(hù)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è)用戶(hù)密碼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
一、PHP操作MySql數(shù)據(jù)庫(kù)
新增數(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='小可愛(ài)'
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(
'查詢(xún)語(yǔ)句出錯(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è)輸出的長(zhǎng)度
mysql_f
ield_name():
取得結(jié)果中指定字段的字段名
mysql_num_rows():
取得結(jié)果集中行的數(shù)目
mysql_num_f
ields():取得結(jié)果集中字段的數(shù)目
mysql_get_client_inf
o()
:
取得
MySQL
客戶(hù)端信息
mysql_get_host_info():
取得
MySQL
主機(jī)信息
mysql_get_proto_info():
取得
MySQL
協(xié)議信息
mysql_get_server_inf
o()
:
取得
MySQL
服務(wù)器信息
class sqlHelper{ \x0d\x0a public $conn; \x0d\x0a public $dbname="數(shù)據(jù)庫(kù)名稱(chēng)"; \x0d\x0a public $username="數(shù)據(jù)庫(kù)用戶(hù)名"; \x0d\x0a public $password="數(shù)據(jù)庫(kù)密碼"; \x0d\x0a public $host="localhost"; \x0d\x0a //連接數(shù)據(jù)庫(kù) \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í)行查詢(xún)語(yǔ)句 \x0d\x0a public function execute_dql($sql){ \x0d\x0a $res=mysql_query($sql,$this-conn); \x0d\x0a return $res; \x0d\x0a } \x0d\x0a //執(zhí)行增填改語(yǔ)句 \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;//表示沒(méi)有行收到影響 \x0d\x0a } \x0d\x0a } \x0d\x0a }\x0d\x0a}