『壹』 誰能提供一個可用的,註解完全的php連接mysql資料庫的類
QQ184031066
加我幫你解答
『貳』 PHP訪問MYSQL資料庫封裝類(附函數說明)
復制代碼
代碼如下:
<?php
/*
MYSQL
資料庫訪問封裝類
MYSQL
數據訪問方式,php4支持以mysql_開頭的過程訪問方式,php5開始支持以mysqli_開頭的過程和mysqli面向對象
訪問方式,本封裝類以mysql_封裝
數據訪問的一般流程:
1,連接資料庫
mysql_connect
or
mysql_pconnect
2,選擇資料庫
mysql_select_db
3,執行SQL查詢
mysql_query
4,處理返回的數據
mysql_fetch_array
mysql_num_rows
mysql_fetch_assoc
mysql_fetch_row
etc
*/
class
db_mysql
{
var
$querynum
=
0
;
//當前頁面進程查詢資料庫的次數
var
$dblink
;
//資料庫連接資源
//鏈接資料庫
function
connect($dbhost,$dbuser,$dbpw,$dbname='',$dbcharset='utf-8',$pconnect=0
,
$halt=true)
{
$func
=
empty($pconnect)
?
'mysql_connect'
:
'mysql_pconnect'
;
$this->dblink
=
@$func($dbhost,$dbuser,$dbpw)
;
if
($halt
&&
!$this->dblink)
{
$this->halt("無法鏈接資料庫!");
}
//設置查詢字元集
mysql_query("SET
character_set_connection={$dbcharset},character_set_results={$dbcharset},character_set_client=binary",$this->dblink)
;
//選擇資料庫
$dbname
&&
@mysql_select_db($dbname,$this->dblink)
;
}
//選擇資料庫
function
select_db($dbname)
{
return
mysql_select_db($dbname,$this->dblink);
}
//執行SQL查詢
function
query($sql)
{
$this->querynum++
;
return
mysql_query($sql,$this->dblink)
;
}
//返回最近一次與連接句柄關聯的INSERT,UPDATE
或DELETE
查詢所影響的記錄行數
function
affected_rows()
{
return
mysql_affected_rows($this->dblink)
;
}
//取得結果集中行的數目,只對select查詢的結果集有效
function
num_rows($result)
{
return
mysql_num_rows($result)
;
}
//獲得單格的查詢結果
function
result($result,$row=0)
{
return
mysql_result($result,$row)
;
}
//取得上一步
INSERT
操作產生的
ID,只對表有AUTO_INCREMENT
ID的操作有效
function
insert_id()
{
return
($id
=
mysql_insert_id($this->dblink))
>=
0
?
$id
:
$this->result($this->query("SELECT
last_insert_id()"),
0);
}
//從結果集提取當前行,以數字為key表示的關聯數組形式返回
function
fetch_row($result)
{
return
mysql_fetch_row($result)
;
}
//從結果集提取當前行,以欄位名為key表示的關聯數組形式返回
function
fetch_assoc($result)
{
return
mysql_fetch_assoc($result);
}
//從結果集提取當前行,以欄位名和數字為key表示的關聯數組形式返回
function
fetch_array($result)
{
return
mysql_fetch_array($result);
}
//關閉鏈接
function
close()
{
return
mysql_close($this->dblink)
;
}
//輸出簡單的錯誤html提示信息並終止程序
function
halt($msg)
{
$message
=
"<html>\n<head>\n"
;
$message
.=
"<meta
content='text/html;charset=gb2312'>\n"
;
$message
.=
"</head>\n"
;
$message
.=
"<body>\n"
;
$message
.=
"資料庫出錯:".htmlspecialchars($msg)."\n"
;
$message
.=
"</body>\n"
;
$message
.=
"</html>"
;
echo
$message
;
exit
;
}
}
?>
『叄』 php連接mysql資料庫的幾種方式
mysql_connect("127.0.0.1","root","root")
mysql_select_db("庫名")
『肆』 php在類中怎麼連接mysql資料庫
classdbmysqli{
private$error='';
private$errno=0;
private$port;
private$host;
private$username;
private$password;
private$dbname;
private$charset;
public $mysqli;
/**
*構造函數
*@authoraaron
*@returnvoid
*/
function__construct(){
$this->port=3306;
$this->host='127.0.0.1';
$this->username='usert';
$this->password="******";
$this->dbname='testdb';
$this->charset='UTF8';
$db=newmysqli($this->host,$this->username,$this->password,$this->dbname,$this->port);
if(mysqli_connect_error()){
$this->error=mysqli_connect_error();
$this->errno=mysqli_connect_errno();
returnFALSE;
}
$db->query("SETNAMES".$this->charset);
$this->mysqli=$db;
}
}
『伍』 一個關於PHP連接mysql資料庫類的問題
如果你的程序只連接一個MYSQL,那麼mysql_connect的返回值是多餘的,系統會自動記錄,連接成功以後直接mysql_query($sql)就可以了。
『陸』 PHP連接MySQL資料庫的幾種方式
MySQLi :MySQLi 只針對 MySQL 資料庫,MySQLi 還提供了 API 介面。
PDO (PHP Data Objects):PDO 應用在 12 種不同資料庫中。
『柒』 幫忙寫一個PHP,連接mysql資料庫的一個類,實現連接,執行sql語句就好
class mysql{
private $name;
private $host;
private $pw;
private $table_name;
private $bianma;
function __construct($h,$n,$p,$b){
$this->name=$n;
$this->host=$h;
$this->pw=$p;
$this->conn();
$this->bianma=$b;
$this->bianma();
}
function conn(){
return mysql_connect("$this->host","$this->name","$this->pw");
}
function db($table){
mysql_select_db("$table");
}
function query($sql=""){
return mysql_query("$sql");
}
function bianma(){
mysql_query("set names '$this->bianma'");
}
}
$mysql=new mysql("localhost","root","","GBK");
$mysql->db("mynews");
『捌』 PHP連接MYSQL資料庫
<?PHP
$conn = @ mysql_connect("127.0.0.1", "用戶名", "密碼") or die("資料庫連接失敗");
mysql_select_db("bbs", $conn);
mysql_query("set names 'GBK'");
?>
『玖』 php連接mysql資料庫
您好,非常榮幸能在此回答您的問題。以下是我對此問題的部分見解,若有錯誤,歡迎指出。<?
function conn(){
$conn01 = mysql_connect("localhost",'root','123456');//root是帳號,123456是密碼
$mycon=mysql_select_db('testdatabase',$conn01); //testdatabase是mysql資料庫名
if($mycon){
echo("資料庫連接成功");
}else{
echo("資料庫連接失敗");
}
}
conn();
?>非常感謝您的耐心觀看,如有幫助請採納,祝生活愉快!謝謝!
『拾』 如何php連接mysql資料庫
@mysql_connect("localhost","root","123456") or die("資料庫連接失敗");
@mysql_select_db("user") or die("資料庫不存在或不可用");
$query=@mysql_query("select * from adminInfo") or die("SQL 語句執行失敗");
while($row=mysql_fetch_array($query))
{
echo $row["name"];
}
mysql_close();