?
成都網絡公司-成都網站建設公司創(chuàng)新互聯建站十余年經驗成就非凡,專業(yè)從事做網站、網站制作,成都網頁設計,成都網頁制作,軟文推廣,廣告投放等。十余年來已成功提供全面的成都網站建設方案,打造行業(yè)特色的成都網站建設案例,建站熱線:13518219792,我們期待您的來電!
$db_host = "localhost";//鏈接的數據庫地址,也就是主機名字
$db_user = "db";//數據庫名字
$db_pass = "數據庫密碼";
$db_name = "msg";//表名
$connec = mysql_connect($db_host,$db_user,$db_pass) or die("不能連接數據庫服務器: ".mysql_error());
mysql_select_db($db_name,$connec) or die ("不能選擇數據庫: ".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);//運行sql語句
?
上面的程序改改就可以用了,或許有問題,我在網吧,沒調試的!
我也是學PHP的,現在還很菜,有時間的話咱交流交流!
舉例如下:
創(chuàng)建userinfo_update.php頁面用于查詢用戶信息,先顯示信息,在修改:
先通過GET獲取用戶編號查詢用戶信息:
$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() 函數返回前一次 MySQL 操作所影響的記錄行數。
//通過post獲取頁面提交數據信息
$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();//返回影響行數
$url = "userinf_select.php";
運行結果
創(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();//返回影響行數
if($mark0){
echo "刪除成功";
}else{
echo? "刪除失敗";
}
mysql_close($con);
運行結果:
?php
class MySQL{
private $host; //服務器地址
private $name; //登錄賬號
private $pwd; //登錄密碼
private $dBase; //數據庫名稱
private $conn; //數據庫鏈接資源
private $result; //結果集
private $msg; //返回結果
private $fields; //返回字段
private $fieldsNum; //返回字段數
private $rowsNum; //返回結果數
private $rowsRst; //返回單條記錄的字段數組
private $filesArray = array(); //返回字段數組
private $rowsArray = array(); //返回結果數組
private $charset='utf8'; //設置操作的字符集
private $query_count=0; //查詢結果次數
static private $_instance; //存儲對象
//初始化類
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;
}
//鏈接數據庫
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);
}
//查詢結果
function mysql_query_rst($sql){
if($this-conn == '') $this-init_conn();
$this-result = @mysql_query($sql,$this-conn);
$this-query_count++;
}
//取得字段數
function getFieldsNum($sql){
$this-mysql_query_rst($sql);
$this-fieldsNum = @mysql_num_fields($this-result);
}
//取得查詢結果數
function getRowsNum($sql){
$this-mysql_query_rst($sql);
if(mysql_errno() == 0){
return @mysql_num_rows($this-result);
}else{
return '';
}
}
//取得記錄數組(單條記錄)
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 '';
}
}
//取得記錄數組(多條記錄)
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 '';
}
}
//更新、刪除、添加記錄數
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 '';
}
}
//返回最近插入的一條數據庫的id值
function returnRstId($sql){
if($this-conn == ''){
$this-init_conn();
}
@mysql_query($sql);
if(mysql_errno() == 0){
return mysql_insert_id();
}else{
return '';
}
}
//獲取對應的字段值
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 '';
}
}
//錯誤信息
function msg_error(){
if(mysql_errno() != 0) {
$this-msg = mysql_error();
}
return $this-msg;
}
//釋放結果集
function close_rst(){
mysql_free_result($this-result);
$this-msg = '';
$this-fieldsNum = 0;
$this-rowsNum = 0;
$this-filesArray = '';
$this-rowsArray = '';
}
//關閉數據庫
function close_conn(){
$this-close_rst();
mysql_close($this-conn);
$this-conn = '';
}
//取得數據庫版本
function db_version() {
return mysql_get_server_info();
}
}
php操作mysql步驟:
1.$connect=mysql_connect('localhost','root','123456')
or
die('數據庫連接失敗。'mysql_error());鏈接mysql。
2.mysql_select_db('database',$connect)選擇鏈接的數據庫。
3.mysql_query('Set
names
gb2312');$sql
=
"select
*
from
blog_article";準備要查詢的數據。
4.$datas
=
mysql_query($sql);執(zhí)行sql查詢。
5.$data
=
mysql_fetch_assoc($datas)得到查詢到的緩存在內存中的一條數據。
6.print_r($data);
相同點:三個函數都是返回數據庫中查詢到的一行數據(說的再清楚點就是一條數據)。
不同點:mysql_fetch_assoc()用的是數據庫中相應的字段名作為的key值(也就是數組下標)
如:filed['id']=1;
mysql_fetch_row()用的是自動生成的數字(從0開始依次生成)作為的key值(也就是數組下標)
如:filed[0]=1;
mysql_fetch_array()用的是自動生成的數字(從0開始依次生成)作為的key值(也就是數組下標),而且它還同時生成數據庫中相應的字段名作為的key值(也就是數組下標)
如:
filed[0]=1,filed['id']=1;也就是說,mysql_fetch_array()將mysql_fetch_assoc()和mysql_fetch_row()查詢到的結果合為了一體了。
mysql_fetch_object()與mysql_fetch_assoc()差不多。只是mysql_fetch_assoc()返回的是數組。mysql_fetch_object()返回的是object對象。
mysql_insert_id() 取得上一步
INSERT
操作產生的
ID。
mysql_result()
函數返回結果集中一個字段的值。
mysql_num_fields()
函數返回結果集中字段的數目。
mysql_affected_rows();返回前一次
MySQL
操作所影響的記錄行數。
mysql_num_rows(mysql_query($sql))獲得結果集中行的數目。
mysql_pconnect()
函數打開一個到
MySQL
服務器的持久連接。
mysql_pconnect()
和
mysql_connect()
非常相似,但有兩個主要區(qū)別:
1.
當連接的時候本函數將先嘗試尋找一個在同一個主機上用同樣的用戶名和密碼已經打開的(持久)連接,如果找到,則返回此連接標識而不打開新連接。
2.
其次,當腳本執(zhí)行完畢后到
SQL
服務器的連接不會被關閉,此連接將保持打開以備以后使用(mysql_close()
不會關閉由
mysql_pconnect()
建立的連接)。
mysql_data_seek(mysql_query($sql),8);獲得結果集中的第8條數據。(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)會緩存查詢的結果。
mysql_close();關閉mysql的最近的鏈接。
mysql_field_flags(mysql_query($sql),6)返回第六個字段的表屬性輸出如:not_null
primary_key
auto_increment
。
mysql_fetch_lengths(mysql_query($sql))返回該條數據的所有字段的每個字段的長度。返回的是一個數字組成的數組。
mysql_field_name(mysql_query($sql),3)返回第三個字段的字段名。
mysql_field_table(mysql_query($sql),0)返回指定字段所在的表名。
mysql_free_result(mysql_query($sql))
函數釋放結果內存。
mysql_get_client_info()
函數返回
MySQL
客戶端信息。
mysql_get_host_info()
取得
MySQL
主機信息。
當前題目:php操作數據庫code,php操作數據庫代碼
分享URL:http://muchs.cn/article38/hcjhsp.html
成都網站建設公司_創(chuàng)新互聯,為您提供電子商務、網站維護、網站導航、標簽優(yōu)化、品牌網站建設、網頁設計公司
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯