?

成都網(wǎng)絡(luò)公司-成都網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站十余年經(jīng)驗(yàn)成就非凡,專業(yè)從事做網(wǎng)站、網(wǎng)站制作,成都網(wǎng)頁設(shè)計(jì),成都網(wǎng)頁制作,軟文推廣,廣告投放等。十余年來已成功提供全面的成都網(wǎng)站建設(shè)方案,打造行業(yè)特色的成都網(wǎng)站建設(shè)案例,建站熱線:13518219792,我們期待您的來電!
$db_host = "localhost";//鏈接的數(shù)據(jù)庫(kù)地址,也就是主機(jī)名字
$db_user = "db";//數(shù)據(jù)庫(kù)名字
$db_pass = "數(shù)據(jù)庫(kù)密碼";
$db_name = "msg";//表名
$connec = mysql_connect($db_host,$db_user,$db_pass) or die("不能連接數(shù)據(jù)庫(kù)服務(wù)器: ".mysql_error());
mysql_select_db($db_name,$connec) or die ("不能選擇數(shù)據(jù)庫(kù): ".mysql_error());
$user=$_POST['user']; //$_post不用大寫的就沒用得
$sms=$_POST['sms'];
$ID=$_POST['id'];
$db_query='INSERT INTO msg(表名) VALUES $user,$sms,$ID';//插入
mysql db query($db_query);//運(yùn)行sql語句
?
上面的程序改改就可以用了,或許有問題,我在網(wǎng)吧,沒調(diào)試的!
我也是學(xué)PHP的,現(xiàn)在還很菜,有時(shí)間的話咱交流交流!
舉例如下:
創(chuàng)建userinfo_update.php頁面用于查詢用戶信息,先顯示信息,在修改:
先通過GET獲取用戶編號(hào)查詢用戶信息:
$sql = "select * from user_info where user_id='".$_GET['userId']."'";
$result = mysql_query($sql,$con);
if($row = mysql_fetch_array($result)){
}
頁面效果:
創(chuàng)建update.php文件,用于修改用戶信息:
使用到了mysql_affected_rows() 函數(shù)返回前一次 MySQL 操作所影響的記錄行數(shù)。
//通過post獲取頁面提交數(shù)據(jù)信息
$userId = $_POST[userId];
$userName = $_POST[userName];
$userAge = $_POST[userAge];
$sql = "update user_info set user_name='".$userName."',user_age=".$userAge." where user_id='".$userId."'";
mysql_query($sql,$conn);//執(zhí)行SQL
$mark? = mysql_affected_rows();//返回影響行數(shù)
$url = "userinf_select.php";
運(yùn)行結(jié)果
創(chuàng)建delete.php文件,完成刪除用戶信息功能:
$userId = $_GET['userId'];
include 'connection.php';
$sql = "delete from user_info where user_id='".$userId."'";
mysql_query($sql,$con);
$mark? = mysql_affected_rows();//返回影響行數(shù)
if($mark0){
echo "刪除成功";
}else{
echo? "刪除失敗";
}
mysql_close($con);
運(yùn)行結(jié)果:
?php
class MySQL{
private $host; //服務(wù)器地址
private $name; //登錄賬號(hào)
private $pwd; //登錄密碼
private $dBase; //數(shù)據(jù)庫(kù)名稱
private $conn; //數(shù)據(jù)庫(kù)鏈接資源
private $result; //結(jié)果集
private $msg; //返回結(jié)果
private $fields; //返回字段
private $fieldsNum; //返回字段數(shù)
private $rowsNum; //返回結(jié)果數(shù)
private $rowsRst; //返回單條記錄的字段數(shù)組
private $filesArray = array(); //返回字段數(shù)組
private $rowsArray = array(); //返回結(jié)果數(shù)組
private $charset='utf8'; //設(shè)置操作的字符集
private $query_count=0; //查詢結(jié)果次數(shù)
static private $_instance; //存儲(chǔ)對(duì)象
//初始化類
private function __construct($host='',$name='',$pwd='',$dBase=''){
if($host != '') $this-host = $host;
if($name != '') $this-name = $name;
if($pwd != '') $this-pwd = $pwd;
if($dBase != '') $this-dBase = $dBase;
$this-init_conn();
}
//防止被克隆
private function __clone(){}
public static function getInstance($host='',$name='',$pwd='',$dBase=''){
if(FALSE == (self::$_instance instanceof self)){
self::$_instance = new self($host,$name,$pwd,$dBase);
}
return self::$_instance;
}
public function __set($name,$value){
$this-$name=$value;
}
public function __get($name){
return $this-$name;
}
//鏈接數(shù)據(jù)庫(kù)
function init_conn(){
$this-conn=@mysql_connect($this-host,$this-name,$this-pwd) or die('connect db fail !');
@mysql_select_db($this-dBase,$this-conn) or die('select db fail !');
mysql_query("set names ".$this-charset);
}
//查詢結(jié)果
function mysql_query_rst($sql){
if($this-conn == '') $this-init_conn();
$this-result = @mysql_query($sql,$this-conn);
$this-query_count++;
}
//取得字段數(shù)
function getFieldsNum($sql){
$this-mysql_query_rst($sql);
$this-fieldsNum = @mysql_num_fields($this-result);
}
//取得查詢結(jié)果數(shù)
function getRowsNum($sql){
$this-mysql_query_rst($sql);
if(mysql_errno() == 0){
return @mysql_num_rows($this-result);
}else{
return '';
}
}
//取得記錄數(shù)組(單條記錄)
function getRowsRst($sql,$type=MYSQL_BOTH){
$this-mysql_query_rst($sql);
if(empty($this-result)) return '';
if(mysql_error() == 0){
$this-rowsRst = mysql_fetch_array($this-result,$type);
return $this-rowsRst;
}else{
return '';
}
}
//取得記錄數(shù)組(多條記錄)
function getRowsArray($sql,$type=MYSQL_BOTH){
!empty($this-rowsArray) ? $this-rowsArray=array() : '';
$this-mysql_query_rst($sql);
if(mysql_errno() == 0){
while($row = mysql_fetch_array($this-result,$type)) {
$this-rowsArray[] = $row;
}
return $this-rowsArray;
}else{
return '';
}
}
//更新、刪除、添加記錄數(shù)
function uidRst($sql){
if($this-conn == ''){
$this-init_conn();
}
@mysql_query($sql);
$this-rowsNum = @mysql_affected_rows();
if(mysql_errno() == 0){
return $this-rowsNum;
}else{
return '';
}
}
//返回最近插入的一條數(shù)據(jù)庫(kù)的id值
function returnRstId($sql){
if($this-conn == ''){
$this-init_conn();
}
@mysql_query($sql);
if(mysql_errno() == 0){
return mysql_insert_id();
}else{
return '';
}
}
//獲取對(duì)應(yīng)的字段值
function getFields($sql,$fields){
$this-mysql_query_rst($sql);
if(mysql_errno() == 0){
if(mysql_num_rows($this-result) 0){
$tmpfld = @mysql_fetch_row($this-result);
$this-fields = $tmpfld[$fields];
}
return $this-fields;
}else{
return '';
}
}
//錯(cuò)誤信息
function msg_error(){
if(mysql_errno() != 0) {
$this-msg = mysql_error();
}
return $this-msg;
}
//釋放結(jié)果集
function close_rst(){
mysql_free_result($this-result);
$this-msg = '';
$this-fieldsNum = 0;
$this-rowsNum = 0;
$this-filesArray = '';
$this-rowsArray = '';
}
//關(guān)閉數(shù)據(jù)庫(kù)
function close_conn(){
$this-close_rst();
mysql_close($this-conn);
$this-conn = '';
}
//取得數(shù)據(jù)庫(kù)版本
function db_version() {
return mysql_get_server_info();
}
}
php操作mysql步驟:
1.$connect=mysql_connect('localhost','root','123456')
or
die('數(shù)據(jù)庫(kù)連接失敗。'mysql_error());鏈接mysql。
2.mysql_select_db('database',$connect)選擇鏈接的數(shù)據(jù)庫(kù)。
3.mysql_query('Set
names
gb2312');$sql
=
"select
*
from
blog_article";準(zhǔn)備要查詢的數(shù)據(jù)。
4.$datas
=
mysql_query($sql);執(zhí)行sql查詢。
5.$data
=
mysql_fetch_assoc($datas)得到查詢到的緩存在內(nèi)存中的一條數(shù)據(jù)。
6.print_r($data);
相同點(diǎn):三個(gè)函數(shù)都是返回?cái)?shù)據(jù)庫(kù)中查詢到的一行數(shù)據(jù)(說的再清楚點(diǎn)就是一條數(shù)據(jù))。
不同點(diǎn):mysql_fetch_assoc()用的是數(shù)據(jù)庫(kù)中相應(yīng)的字段名作為的key值(也就是數(shù)組下標(biāo))
如:filed['id']=1;
mysql_fetch_row()用的是自動(dòng)生成的數(shù)字(從0開始依次生成)作為的key值(也就是數(shù)組下標(biāo))
如:filed[0]=1;
mysql_fetch_array()用的是自動(dòng)生成的數(shù)字(從0開始依次生成)作為的key值(也就是數(shù)組下標(biāo)),而且它還同時(shí)生成數(shù)據(jù)庫(kù)中相應(yīng)的字段名作為的key值(也就是數(shù)組下標(biāo))
如:
filed[0]=1,filed['id']=1;也就是說,mysql_fetch_array()將mysql_fetch_assoc()和mysql_fetch_row()查詢到的結(jié)果合為了一體了。
mysql_fetch_object()與mysql_fetch_assoc()差不多。只是mysql_fetch_assoc()返回的是數(shù)組。mysql_fetch_object()返回的是object對(duì)象。
mysql_insert_id() 取得上一步
INSERT
操作產(chǎn)生的
ID。
mysql_result()
函數(shù)返回結(jié)果集中一個(gè)字段的值。
mysql_num_fields()
函數(shù)返回結(jié)果集中字段的數(shù)目。
mysql_affected_rows();返回前一次
MySQL
操作所影響的記錄行數(shù)。
mysql_num_rows(mysql_query($sql))獲得結(jié)果集中行的數(shù)目。
mysql_pconnect()
函數(shù)打開一個(gè)到
MySQL
服務(wù)器的持久連接。
mysql_pconnect()
和
mysql_connect()
非常相似,但有兩個(gè)主要區(qū)別:
1.
當(dāng)連接的時(shí)候本函數(shù)將先嘗試尋找一個(gè)在同一個(gè)主機(jī)上用同樣的用戶名和密碼已經(jīng)打開的(持久)連接,如果找到,則返回此連接標(biāo)識(shí)而不打開新連接。
2.
其次,當(dāng)腳本執(zhí)行完畢后到
SQL
服務(wù)器的連接不會(huì)被關(guān)閉,此連接將保持打開以備以后使用(mysql_close()
不會(huì)關(guān)閉由
mysql_pconnect()
建立的連接)。
mysql_data_seek(mysql_query($sql),8);獲得結(jié)果集中的第8條數(shù)據(jù)。(mysql_num_rows(mysql_query($sql))和mysql_data_seek(mysql_query($sql),8)在mysql_unbuffered_query($sql)不可以使用。)
mysql_unbuffered_query($sql)和mysql_query($sql)效果差不多,但是
mysql_unbuffered_query($sql)不緩存。mysql_query($sql)會(huì)緩存查詢的結(jié)果。
mysql_close();關(guān)閉mysql的最近的鏈接。
mysql_field_flags(mysql_query($sql),6)返回第六個(gè)字段的表屬性輸出如:not_null
primary_key
auto_increment
。
mysql_fetch_lengths(mysql_query($sql))返回該條數(shù)據(jù)的所有字段的每個(gè)字段的長(zhǎng)度。返回的是一個(gè)數(shù)字組成的數(shù)組。
mysql_field_name(mysql_query($sql),3)返回第三個(gè)字段的字段名。
mysql_field_table(mysql_query($sql),0)返回指定字段所在的表名。
mysql_free_result(mysql_query($sql))
函數(shù)釋放結(jié)果內(nèi)存。
mysql_get_client_info()
函數(shù)返回
MySQL
客戶端信息。
mysql_get_host_info()
取得
MySQL
主機(jī)信息。
當(dāng)前題目:php操作數(shù)據(jù)庫(kù)code,php操作數(shù)據(jù)庫(kù)代碼
分享URL:http://chinadenli.net/article38/hcjhsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、網(wǎng)站維護(hù)、網(wǎng)站導(dǎo)航、標(biāo)簽優(yōu)化、品牌網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)公司
聲明:本網(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í)需注明來源: 創(chuàng)新互聯(lián)