數(shù)據(jù)庫(kù)有很多種類:mysql,oracle,mssql,db2等等。PHP操作數(shù)據(jù)庫(kù)的時(shí)候,要保證該類型數(shù)據(jù)庫(kù)的擴(kuò)展已開啟。這里連接的數(shù)據(jù)庫(kù)以mysql為例:?php
十多年的丹棱網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)整合營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整丹棱建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“丹棱網(wǎng)站設(shè)計(jì)”,“丹棱網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
//數(shù)據(jù)庫(kù)服務(wù)器地址
$host="localhost";
//連接數(shù)據(jù)庫(kù)用戶名
$uname="root";
//連接數(shù)據(jù)庫(kù)密碼
$upass="";
//連接數(shù)據(jù)庫(kù)
$conn=mysql_connect($host, $uname,$upass);
//判斷連接
if(!$conn){
die("連接數(shù)據(jù)庫(kù)失敗!").mysql_errno();
}
//連接成功,其他操作省略
?
PHP連接數(shù)據(jù)庫(kù)之PHP連接MYSQL數(shù)據(jù)庫(kù)代碼
?php???
$mysql_server_name='localhost';?
//改成自己的mysql數(shù)據(jù)庫(kù)服務(wù)器??
$mysql_username='root';?
//改成自己的mysql數(shù)據(jù)庫(kù)用戶名??
$mysql_password='12345678';?
//改成自己的mysql數(shù)據(jù)庫(kù)密碼??
$mysql_database='mycounter';
//改成自己的mysql數(shù)據(jù)庫(kù)名??
$conn=mysql_connect($mysql_server_name,
$mysql_username,$mysql_password,
$mysql_database);???
$sql='CREATE?DATABASE?mycounter?
DEFAULT?CHARACTER?SET?gbk?COLLATE?gbk_chinese_ci;???
';???
mysql_query($sql);???
$sql='CREATE?TABLE?`counter`?
(`id`?INT(255)?UNSIGNED?NOT?NULL?
AUTO_INCREMENT?,`count`?INT(255)?
UNSIGNED?NOT?NULL?DEFAULT?0,PRIMARY?KEY?
(?`id`?)?)?TYPE?=?innodb;';???
mysql_select_db($mysql_database,$conn);???
$result=mysql_query($sql);???
//echo?$sql;???
mysql_close($conn);???
echo?"Hello!數(shù)據(jù)庫(kù)mycounter已經(jīng)成功建立!";???
??
PHP連接數(shù)據(jù)庫(kù)之PHP連接ACCESS數(shù)據(jù)庫(kù)代碼方法
???
$conn?=?new?com("ADODB.Connection");???
$connstr?=?"DRIVER={Microsoft
Access?Driver?(*.mdb)};?
DBQ=".?realpath("data/db.mdb");???
$conn-Open($connstr);???
$rs?=?new?com("ADODB.RecordSet");???
$rs-Open("select?*
from?szd_t",$conn,1,1);???
while(!?$rs-eof)?{???
$f?=?$rs-Fields(1);???
echo?$f-value;???
$rs-MoveNext();???
}???
?
?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();
}
}
呵呵,這個(gè)我會(huì)代碼我寫下!加注釋...
$link = mysql_connect("localhost", "user", "123");//連接數(shù)據(jù)庫(kù)
mysql_select_db("db");//選擇數(shù)據(jù)庫(kù)
$kkk=_GET('k');
$sql="select * from ttt where kkk='$kkk'";//查詢kkk對(duì)應(yīng)content語(yǔ)句
$set=mysqluery($sql);//執(zhí)行sql語(yǔ)句!
$rs=mysql_fetch_arry($set);
$content=$rs['content'];//取出kkk對(duì)應(yīng)的content
if($content==''){
$content=fff($kkk);//將fff($kkk)賦值給content
//---這里面說加入相應(yīng)字段是不是指插入數(shù)據(jù)庫(kù)里面!如果是
//----那樣的話就再加兩句:$sa="update ttt set content='$content' where kkk='$kkk'";
//-----$set_a=mysql_query($sa);
}else{
echo $content;//輸出$content
}
完成....
你后面說的問題:如果$kkk有重復(fù)那我剛才的語(yǔ)句就把所有kkk=$kkk的全部更新了!
你只想選擇出第一條記錄的話:把查詢語(yǔ)句改成這樣$sql="select top 1 * from ttt where kkk='$kkk'";
還有就是數(shù)據(jù)入庫(kù)的時(shí)候要替換什么東西,讀取出來(lái)的時(shí)候還需要還原。這句話不明白!
呵呵,別忘了給我加分哦!