當前位置:首頁 » 編程語言 » php連接mysql的類

php連接mysql的類

發布時間: 2022-03-31 04:14:52

① 誰能提供一個可用的,註解完全的php連接mysql資料庫的類

<?php

/*這個基本上就是我使用的資料庫類*/
//屏蔽未定義錯誤
error_reporting(7);

class DB_MySQL {

var $servername="localhost";
var $dbname="DBASE";
var $dbusername = "ROOT";
var $dbpassword = "";
var $conn = 0;
var $technicalemail='java@cu165.com';

function geterrdesc() {
$this->error = @mysql_error($this->conn);
return $this->error;
}

function geterrno() {
$this->errno = @mysql_errno($this->conn);
return $this->errno;
}

function query($query_string) {

// $this->result = mysql_db_query($this->dbname,$query_string);
$this->result = mysql_query($query_string);
if (!$this->result) {
$this->halt("SQL 無效: ".$query_string);
}

return $this->result;
}

function num_rows($queryid) {

$this->rows = mysql_num_rows($queryid);

if (empty($queryid)){
$this->halt("Query id 無效:".$queryid);
}
return $this->rows;
}

function fetch_array($queryid) {

$this->record = mysql_fetch_array($queryid);
if (empty($queryid)){
$this->halt("Query id 無效:".$queryid);
}
return $this->record;
}

function conn(){
$this->conn = mysql_connect($this->servername, $this->dbusername, $this->dbpassword) or die(mysql_error("資料庫鏈接失敗"));
return $this->conn;
}

function selectdb(){
if(!mysql_select_db($this->dbname)){
$this->halt("資料庫鏈接失敗");
}
}

function my_close() {
// mysql_close($this->conn);
mysql_close();
}

function fetch_row($queryid) {

$this->record = mysql_fetch_row($queryid);
if (empty($queryid)){
$this->halt("queryid 無效:".$queryid);
}
return $this->record;
}

function fetch_one_num($query) {

$this->result = $this->query($query);
$this->record = $this->num_rows($this->result);
if (empty($query)){
$this->halt("Query id 無效:".$query);
}
return $this->record;

}
function fetch_one_array($query) {

$this->result = $this->query($query);
$this->record = $this->fetch_array($this->result);
if (empty($query)){
$this->halt("Query id 無效:".$query);
}
return $this->record;

}

function free_result($query){
if (!mysql_free_result($query)){
$this->halt("fail to mysql_free_result");
}
}

function insert_id(){
$this->insertid = mysql_insert_id();
if (!$this->insertid){
$this->halt("fail to get mysql_insert_id");
}
return $this->insertid;
}

/*========================================================================*/
// Create an array from a multidimensional array returning formatted
// strings ready to use in an Insert query, saves having to manually format
// the (Insert INTO table) ('field', 'field', 'field') VALUES ('val', 'val')
/*========================================================================*/

function compile_db_insert_string($data) {

$field_names = "";
$field_values = "";

foreach ($data as $k => $v)
{
$v = preg_replace( "/'/", "\\'", $v );
//$v = preg_replace( "/#/", "\\#", $v );
$field_names .= "$k,";
$field_values .= "'$v',";
}

$field_names = preg_replace( "/,$/" , "" , $field_names );
$field_values = preg_replace( "/,$/" , "" , $field_values );

return array( 'FIELD_NAMES' => $field_names,
'FIELD_VALUES' => $field_values,
);
}

/*========================================================================*/
// Create an array from a multidimensional array returning a formatted
// string ready to use in an Update query, saves having to manually format
// the FIELD='val', FIELD='val', FIELD='val'
/*========================================================================*/

function compile_db_update_string($data) {

$return_string = "";

foreach ($data as $k => $v)
{
$v = preg_replace( "/'/", "\\'", $v );
$return_string .= $k . "='".$v."',";
}

$return_string = preg_replace( "/,$/" , "" , $return_string );

return $return_string;
}

function halt($msg){

global $technicalemail,$debug;

$message = "<html>\n<head>\n";
$message .= "<meta content=\"text/html; charset=gb2312\" http-equiv=\"Content-Type\">\n";
$message .= "<STYLE TYPE=\"text/css\">\n";
$message .= "<!--\n";
$message .= "body,td,p,pre {\n";
$message .= "font-family : Verdana, Arial, Helvetica, sans-serif;font-size : 12px;\n";
$message .= "}\n";
$message .= "</STYLE>\n";
$message .= "</head>\n";
$message .= "<body bgcolor=\"#EEEEEE\" text=\"#000000\" link=\"#006699\" vlink=\"#5493B4\">\n";
$message .= "<font size=10><b>系統調試</b></font><font size=6><b>(by 大白菜芯 )</b></font>\n<hr NOSHADE SIZE=1>\n";

$content = "<p>資料庫出錯:</p><pre><b>".htmlspecialchars($msg)."</b></pre>\n";
$content .= "<b>Mysql error description</b>: ".$this->geterrdesc()."\n<br>";
$content .= "<b>Mysql error number</b>: ".$this->geterrno()."\n<br>";
$content .= "<b>Date</b>: ".date("Y-m-d @ H:i")."\n<br>";
$content .= "<b>Script</b>: http://".$_SERVER[HTTP_HOST].getenv("REQUEST_URI")."\n<br>";
$content .= "<b>Referer</b>: ".getenv("HTTP_REFERER")."\n<br><br>";

$message .= $content;

$message .= "<p>請嘗試刷新你的瀏覽器,如果仍然無法正常顯示,請聯系<a href=\"technicalemail."\'>mailto:".$this->technicalemail."\">管理員</a>.</p>";
$message .= "</body>\n</html>";
echo $message;

$headers = "From: nt.cn <$this->technicalemail>\r\n";

$content = strip_tags($content);
@mail($technicalemail,"資料庫出錯",$content,$headers);

exit;
}

}
?>

② PHP連接MYSQL資料庫

php連接資料庫類,可以使用;
或者自己寫個連接代碼:
自定義連接地址、用戶名、密碼、資料庫
用$connect= mysql_connect('localhost','user','pwd') or die('連接錯誤:'.mysql_error());
<?php
$servername='localhost' ;
$database_username='root';
$database_password='password';
$database_name='databasename';
$connect=mysql_connect($servername,$database_username,$database_password);
mysql_query("set names 'utf8'");
mysql_select_db($database_name);

?>

③ PHP mysql操作類的問題

你是想用填入一個數組然後自動解析出SQL語句么?
我給你個思路吧...

SELECT [select options] FROM [tables] [CONDITION]
首先是select options,一般有查詢COUNT(*)、*或者指定一些查詢值
所以可以把select options的選項定義在一個數組中的一個新的組
也就是
$array = array(
"SELECT" => array("a", "b")
);
你需要循環SELECT的值,然後解析成SQL
解析出來大概就是 SELECT a,b FROM ...
然後table,這個好說.. 直接給個固定值
最麻煩就是後面的CONDITION,也就是 SELECT **** WHERE a = 'a' 之類的東西
這個你可以作為常項
array(
「SELECT」 => array("a","b"),
"username" => "mutou"
);
你直接循環這個單一數組,把SELECT單列出來,後面的用else,然後進行key和value的提取,獲取值填入SQL
這段解析出來應該是 SELECT a,b FROM table WHERE username = "mutou"
其他SELECT的常用參數還有ORDER,LIMIT等,可以用同樣的辦法
最近寫了一個比較簡單的SELECT類.. 所以暫說這么多了

④ 幫忙寫一個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

classdemo
{
function__destruct()
{
$DB->close();//$DB哪裡來的?應該是$this->DB->close()吧
}
publicfunction__construct()
{
$DB=newDB_MySQL;//這屬於函數內部變數,函數執行完就消失了。所以應該用$this->DB=newDB_MySQL
$DB->connect(servername,dbusername,dbpassword,dbname,usepconnect);//同理,需要改成$this->DB,參數也有問題吧,還是你為避免泄露sql賬號密碼故意這么寫的?
}
functiontest()
{
$sql1="SELECT*FROMtablimit1";
$txt=$DB->fetch_one_array($sql1);//同理,需要改成$this->DB
return$txt['id'];
}
}

$person=newdemo;
echo$person->test();

⑥ 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資料庫封裝類(附函數說明)

復制代碼
代碼如下:
<?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
;
}
}
?>

⑧ MySQL與PHP的連接語句類型

PHP與MYSQL資料庫連接語句
mysql_connect(」localhost」,
「username」,
「password」)
資料庫選擇
mysql_select_db(」test」)

⑨ 一個關於PHP連接mysql資料庫類的問題

如果你的程序只連接一個MYSQL,那麼mysql_connect的返回值是多餘的,系統會自動記錄,連接成功以後直接mysql_query($sql)就可以了。

⑩ PHP Mysql操作類

<?php

class Mysql{

private $db_host;
private $db_user;
private $db_pass;
private $db_table;
private $ut;

function __construct($db_host,$db_user,$db_pass,$db_table,$ut){
$this->db_host=$db_host;
$this->db_user=$db_user;
$this->db_pass=$db_pass;
$this->db_table=$db_table;
$this->host=$ut;
$this->connect();

}

function connect(){
$conn= mysql_connect($this->db_host,$this->db_user,$this->db_pass) or die("鏈接錯誤:".$this->db_table);
mysql_select_db($this->db_table,$conn);
mysql_query("set names '$this->ut'");
}

//執行SQL語句的方法
public function query($sql){
$this->query=mysql_query($sql);

}
//查詢一條記錄返回一維數組
public function select_once($table,$where=1,$fields="*"){
$sql="select $fields from $table where $where limit 1";
$this->query($sql);
$result=$this->_array();
return $result;
}

//查詢多條記錄返回二維數組
public function select_all($table,$where=1,$fields="*"){
$sql="select $fields from $table where $where";
$this->query($sql);
$result="";
while($rs=$this->_array()){
$result[]=$rs;
}
return $result;
}

//返回查詢記錄數
public function getRowsNum($sql) {
$query = $this->query ( $sql );
return mysql_num_rows ( $query );
}

//mysql_fetch_array
private function _array(){
return $rs=mysql_fetch_array($this->query);
}

}

$db=new Mysql('localhost','root','','yugaoyf',"gb2312");

?>

使用實例 同樣是使用了 封裝
//查詢全部結果
public function select_all(){
return $this->db->select_all("message as a,reply as b","a.id = b.mid ");
}
//查詢一條記錄
public function select_once(){
return $this->db->select_once("message as a,reply as b","a.id = b.mid and a.id='".$_GET['id']."'");
}

熱點內容
幼兒園源碼php 發布:2025-01-17 02:41:45 瀏覽:401
win引導Linux 發布:2025-01-17 02:36:49 瀏覽:263
ftp是傳輸類協議嗎 發布:2025-01-17 02:36:47 瀏覽:311
查看電視配置下載什麼軟體 發布:2025-01-17 02:36:41 瀏覽:159
寶馬x330i比28i多哪些配置 發布:2025-01-17 02:35:59 瀏覽:573
伺服器運維安全雲幫手 發布:2025-01-17 02:35:48 瀏覽:72
c應用編程 發布:2025-01-17 02:35:16 瀏覽:941
ios清除app緩存數據免費 發布:2025-01-17 02:34:33 瀏覽:375
微信企業號上傳文件 發布:2025-01-17 02:10:28 瀏覽:64
孩子幾歲可以學習編程 發布:2025-01-17 02:09:55 瀏覽:602