刪除一條記錄簡(jiǎn)單方法,應(yīng)該說(shuō)是最簡(jiǎn)單的入門級(jí)的了.
創(chuàng)新互聯(lián)主要從事網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)聊城,10年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):18980820575
mysqldelete from 表名 where id=1;
query ok,
這樣就可以刪除了,
如果你和php教程 mysql配置只要利用php連接到數(shù)據(jù)庫(kù)教程再用php mysql_query("delete from 表名 where id=1");就可以刪除了。
?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("delete from person where lastname='griffin'");
mysql_close($con);
?
下面看一下關(guān)于mysql delete語(yǔ)法.
刪除數(shù)據(jù)庫(kù)中的數(shù)據(jù)
delete from 語(yǔ)句用于從數(shù)據(jù)庫(kù)表中刪除記錄。
語(yǔ)法
delete from table_name
where column_name = some_value
注釋:sql 對(duì)大小寫不敏感。delete from 與 delete from 等效。
為了讓 php 執(zhí)行上面的語(yǔ)句,我們必須使用 mysql_query( 函數(shù)。該函數(shù)用于向 sql 連接發(fā)送查詢和命令
!--表單文件,拷入index.php--
!DOCTYPE?html
html
head
style
label{display:inline-block;width:100px;margin-bottom:10px;}
/style
titleAdd?students/title
/head
body
!--?數(shù)據(jù)庫(kù)用mysqli?面向過(guò)程調(diào)用方法--
form?method="post"?action="write2db.php"
!--數(shù)據(jù)庫(kù)用mysqli?面向過(guò)程調(diào)用方法
form?method="post"?action="write2db_sqlio.php"
--
!--數(shù)據(jù)庫(kù)用PDO調(diào)用方法
form?method="post"?action="write2db_pdo.php"
--
labelFirst?Name/label
input?type="text"?name="first_name"?/
br?/
labelLast?Name/label
input?type="text"?name="last_name"?/
br?/
labeldepartment/label
input?type="text"?name="department"?/
br?/
labelEmail/label
input?type="text"?name="email"?/
br?/
input?type="submit"?value="Add?students"
/form
/body
/html
------------------------------
?php
//拷貝命名為write2db.php,數(shù)據(jù)庫(kù)用mysqli?面向過(guò)程調(diào)用方法
//print_r($_POST);
//?create?a?variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//調(diào)試用
echo?"Your?input:?";
echo?$first_name;
echo?'br?/';
echo?$last_name;
echo?'br?/';
echo?$department;
echo?'br?/';
echo?$email;
echo?'br?/';
$servername?=?"localhost";
//Your?database?username?and?password
//$username?=?"username";
//$password?=?"password";
$username?=?"tester";
$password?=?"testerPassword";
//your?database?name
$dbname?=?"test";
$tablename?="student";
//?Create?connection
$connect?=?mysqli_connect($servername,?$username,?$password,?$dbname);
if?(!$connect)?{
die("Connection?failed:?"?.?mysqli_connect_error());
}
//Execute?the?query
$sql="INSERT?INTO?$tablename?(first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')";
if?(mysqli_query($connect,?$sql))?{
echo?"Hooray!?New?record?is?inserted?to?database?successfully.?Please?check?database.";
}?else?{
echo?"Error:?"?.?$sql?.?"br?/"?.?mysqli_error($connect);
}
mysqli_close($connect);
?
?php
//拷貝命名為write2db_sqlio.php,數(shù)據(jù)庫(kù)用mysqli?面向?qū)ο笳{(diào)用方法
//print_r($_POST);
//?create?a?variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//調(diào)試用
echo?"Your?input:?";
echo?$first_name;
echo?'br?/';
echo?$last_name;
echo?'br?/';
echo?$department;
echo?'br?/';
echo?$email;
echo?'br?/';
$servername?=?"localhost";
//Your?database?username?and?password
//$username?=?"username";
//$password?=?"password";
$username?=?"tester";
$password?=?"testerPassword";
//database?name
$dbname?=?"test";
$tablename?="student";
//?Create?connection
$conn?=?new?mysqli($servername,?$username,?$password,?$dbname);
//?Check?connection
if?($conn-connect_error)?{
die("Connection?failed:?"?.?$conn-connect_error);
}?
$sql="INSERT?INTO?$tablename?(first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')";
if?($conn-query($sql)?===?TRUE)?{
echo?"New?record?created?successfully";
}?else?{
echo?"Error:?"?.?$sql?.?"br"?.?$conn-error;
}
$conn-close();
?
?php
//拷貝為文件write2db_pdo.php,數(shù)據(jù)庫(kù)用PDO調(diào)用方法
//print_r($_POST);
a?variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//調(diào)試用
echo?"Your?input:?";
echo?$first_name;
echo?'br?/';
echo?$last_name;
echo?'br?/';
echo?$department;
echo?'br?/';
echo?$email;
echo?'br?/';
$servername?=?"localhost";
//Your?database?username?and?password
//$username?=?"username";
//$password?=?"password";
$username?=?"tester";
$password?=?"testerPassword";
//your?database?name
$dbname?=?"test";
$tablename?="student";
//?Create?connection
try?{
$conn?=?new?PDO("mysql:host=$servername;dbname=$dbname",?$username,?$password);
//?set?the?PDO?error?mode?to?exception
$conn-setAttribute(PDO::ATTR_ERRMODE,?PDO::ERRMODE_EXCEPTION);
$sql="INSERT?INTO?$tablename?(first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')";
//?use?exec()?
$conn-exec($sql);
echo?"New?record?created?successfully";
}
catch(PDOException?$e)
{
echo?$sql?.?"br"?.?$e-getMessage();
}
$conn?=?null;
?
--創(chuàng)建數(shù)據(jù)庫(kù)test,?將此文件存為test.sql?導(dǎo)入數(shù)據(jù)庫(kù),或者手動(dòng)創(chuàng)建表結(jié)構(gòu)
--?phpMyAdmin?SQL?Dump
--?version?4.7.4
--?
--
--?Host:?127.0.0.1:3306
--?Generation?Time:?Mar?12,?2018?at?04:04?AM
--?Server?version:?5.7.19
--?PHP?Version:?7.1.9
SET?SQL_MODE?=?"NO_AUTO_VALUE_ON_ZERO";
SET?AUTOCOMMIT?=?0;
START?TRANSACTION;
SET?time_zone?=?"+00:00";
/*!40101?SET?@OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT?*/;
/*!40101?SET?@OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS?*/;
/*!40101?SET?@OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION?*/;
/*!40101?SET?NAMES?utf8mb4?*/;
--
--?Database:?`test`
--
--?--------------------------------------------------------
--
--?Table?structure?for?table?`student`
--
DROP?TABLE?IF?EXISTS?`student`;
CREATE?TABLE?IF?NOT?EXISTS?`student`?(
`id`?tinyint(3)?UNSIGNED?NOT?NULL?AUTO_INCREMENT,
`first_name`?varchar(20)?NOT?NULL,
`last_name`?varchar(20)?NOT?NULL,
`department`?varchar(50)?NOT?NULL,
`email`?varchar(50)?NOT?NULL,
PRIMARY?KEY?(`id`)
)?ENGINE=MyISAM?AUTO_INCREMENT=2?DEFAULT?CHARSET=utf8;
--
--?Dumping?data?for?table?`student`
--
INSERT?INTO?`student`?(`id`,?`first_name`,?`last_name`,?`department`,?`email`)?VALUES
(1,?'first1',?'last1',?'cs',?'1985@qq.com');
COMMIT;
/*!40101?SET?CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT?*/;
/*!40101?SET?CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS?*/;
/*!40101?SET?COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION?*/;
?php
$link=mysql_connect('localhost','mysql_user','mysql_password');//mysql的服務(wù)器地址,數(shù)據(jù)庫(kù)賬號(hào),數(shù)據(jù)庫(kù)密碼 該方法用于鏈接數(shù)據(jù)庫(kù)
if(!$link){
die('Could not connect: ' . mysql_error()); //鏈接不上返回錯(cuò)誤
}
mysql_select_db('XK',$link) or die ('Can\'t use foo : ' . mysql_error()); //鏈接數(shù)據(jù)庫(kù)中其中的一個(gè)庫(kù)‘XK’,連接不上報(bào)錯(cuò)。
$sql = mysql_query(”select name,xuehao from girltable where sex=‘girl’ “); //發(fā)送一條 MySQL 查詢
$result = 'table'; //將查詢結(jié)果做成表格
while($row=mysql_fetch_assoc($result)){ //從結(jié)果中取得一行作為關(guān)聯(lián)數(shù)組
$result .= 'tr';
$result .= 'td'$row['name']'/td';
$result .= 'td'$row['xuehao']'/td';
$result .= '/tr';
}
$result .= '/table';
echo $result; //打印表格
?
$sql="select?*?from?table?where?(sex="'".$_POST['sex']."'")?and?(aihao="'".$_POST['aihao']."'")?and?(dizhi="'".$_POST['dizhi']."')?order?by?addtime?desc"
建一張test表
set autocommit=0;//設(shè)置mysql不自動(dòng)提交就是不自動(dòng)執(zhí)行sql語(yǔ)句
begin; //開(kāi)啟一個(gè)事務(wù)
insert into test value(test);
insert into test value(test2);
commit;//提交 ,會(huì)執(zhí)行上面2句sql語(yǔ)句 此時(shí)test 表里面有 test 和 test2 2條數(shù)據(jù)
begin; //開(kāi)啟一個(gè)事務(wù)
insert into test values(test3);
rollback; //回滾 , test表里面并沒(méi)有test3 這條數(shù)據(jù)
通常 在php里面會(huì)判斷 if(!mysql_query($sql)){mysql_query('rollback');} 就是如果沒(méi)有成功執(zhí)行sql就進(jìn)行回滾,比如轉(zhuǎn)賬我給你10塊錢 先從我賬號(hào)里面減去10元 再?gòu)哪阗~戶加10元 這就是2條sql 要保證都能執(zhí)行完成
不管怎樣,在這里我總結(jié)了常用的PHP連接MySQL數(shù)據(jù)庫(kù)以及讀取寫入數(shù)據(jù)庫(kù)的方法,希望能夠幫到你,當(dāng)然也是作為我自己的一個(gè)回顧總結(jié)。
1.為了更好地設(shè)置數(shù)據(jù)連接,一般會(huì)將數(shù)據(jù)連接所涉及的值定義成變量.
?
1
2
3
4
5
6
7
$mysql_server_name='localhost'; //改成自己的mysql數(shù)據(jù)庫(kù)服務(wù)器
$mysql_username='root'; //改成自己的mysql數(shù)據(jù)庫(kù)用戶名
$mysql_password='123456'; //改成自己的mysql數(shù)據(jù)庫(kù)密碼
$mysql_database='Mydb'; //改成自己的mysql數(shù)據(jù)庫(kù)名
也可把以上變量放在一個(gè)文件里,可以隨時(shí)讓其他文件調(diào)用.
例如: 將以上內(nèi)容放在:db_config.php 那么在其他需要用到數(shù)據(jù)庫(kù)的頁(yè)面直接調(diào)用.
調(diào)用代碼:require("db_config.php");
2.連接數(shù)據(jù)庫(kù)
?
1
2
3
4
5
6
7
8
9
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password) or die("error connecting") ; //連接數(shù)據(jù)庫(kù)
mysql_query("set names 'utf8'"); //數(shù)據(jù)庫(kù)輸出編碼 應(yīng)該與你的數(shù)據(jù)庫(kù)編碼保持一致.南昌網(wǎng)站建設(shè)公司百恒網(wǎng)絡(luò)PHP工程師建議用UTF-8 國(guó)際標(biāo)準(zhǔn)編碼.
mysql_select_db($mysql_database); //打開(kāi)數(shù)據(jù)庫(kù)
$sql ="select * from news "; //SQL語(yǔ)句
$result = mysql_query($sql,$conn); //查詢
3.讀取表中的內(nèi)容,這里我們用while,可以根據(jù)具體情況,用for 或其他的.
?
1
2
3
4
5
6
7
8
9
10
11
while($row = mysql_fetch_array($result))
{
echo "div style=\"height:24px; line-height:24px; font-weight:bold;\""; //排版代碼
echo $row['Topic'] . "br/";
echo "/div"; //排版代碼
}
4.php寫入數(shù)據(jù)庫(kù),Mysql數(shù)據(jù)的寫入
本文標(biāo)題:php用mysql怎么寫,phpmysql是什么
當(dāng)前路徑:http://jinyejixie.com/article26/hojscg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、移動(dòng)網(wǎng)站建設(shè)、虛擬主機(jī)、網(wǎng)站改版、自適應(yīng)網(wǎng)站、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)