本文目錄一覽:
- 1、誰給個php操作mysql類並有詳細使用說明或例子
- 2、php 求一個mysqli的db類注釋儘可能的多,初學小白
- 3、幫忙寫一個PHP,連接mysql數據庫的一個類,實現連接,執行sql語句就好
- 4、PHP+MySQL 如何把針對數據庫的添加,查詢,修改,刪除等操作做成一個PHP寫的類?
- 5、PHP mysql操作類的問題
- 6、php實現mysql封裝類示例
誰給個php操作mysql類並有詳細使用說明或例子
下面這個,是針對php5的一個簡單數據庫封裝類,適合學習,其他的如刪除、更新等操作,你可以自己加上:
?php
class Mysql{ //首先定義一個類,首寫字母大寫
public $host;//服務器名,訪問修飾符PUBLIC證明$host是一個公共的屬情在類的內部外部都可訪問,可以被繼承
public $user;//用戶名,是公共的屬性
private $pass;//密碼,問修飾符private證明$pass是私有的.只能在類的內部使用且不能被繼承.
public $dbname;//數據庫名,也是公共的屬性.
//__construct聲名這是一個造函數,定義一些初始的信息.有三個參數
public function __construct($host,$user,$pass,$dbname){
$this-host = $host;
$this-user = $user;
$this-pass = $pass;
$this-dbname = $dbname;
$link = @mysql_connect($this-host,$this-user,$this-pass)
or die(“error”);
@mysql_select_db($this-dbname,$link)
or die(“error2”);
}
//定義數據庫的查尋和顯示函數
function myQuery($sql){
$result = mysql_query($sql);
if(!$result){
echo “error3”;
exit;
}
$num = mysql_num_rows($result);
if($num){
echo “NO”.$num;
}
while($row = mysql_fetch_assoc($result)){
echo ‘trtd bgcolor=”#fffddd”pre’.htmlspecialchars(stripslashes($row[‘body’])).”pre/td/tr”;
}
}
}
$rutt = new Mysql(‘localhost’,’root’,’ssss’,’calvin’);//實例化一個類…記住這裡的參數是和構造函數的參數一樣的…
$rutt-myQuery(‘select * from calvin_body’);//運行數據庫查尋並顯示的函數..
?
php 求一個mysqli的db類注釋儘可能的多,初學小白
mysqli一個最簡單的例子,要深入封裝的話可以自己再增加…
其實個人覺得mysqli已經沒什麼必要封裝了…..
?php
class db{ //類名
public $con; //定義句柄
public $result; //結果存取
public function __construct($Host,$User,$Pass,$DB){ //構建函數
$this-con = new mysqli($Host,$User,$Pass,$DB); //調用mysqli類
if($this-con-connect_error){ //判斷是否有錯誤,有錯誤則返回連接錯誤代號和錯誤內容
return array($this-con-connect_errno,$this-con-connect_error);
}
}
public function query($sql,$type=”){ //執行查詢,$sql為查詢語句,$type為result mode [MYSQLI_USE_RESULT ] OR [MYSQLI_STORE_RESULT ] 執行成功返回true,否則返回false
$this-result = empty($type) ? $this-con-query($sql) : $this-con-query($sql,$type);
return !$this-result ? false : true;
}
public function insertid(){ //必須先進行query才能獲得插入或更新的id
return $this-con-insert_id;
}
public function fetch($n,$t){//獲取結果集,$n必選[array][assoc][field_direct][field][fields][object][row][all],$t為$n對應的可選參數,成功返回結果集
$f = ‘fetch_’.$n;
return $this-result-$f($t);
}
public function __destruct(){ //銷毀函數
if($this-result)$this-result-close();
if($this-con)$this-con-close();
}
public function GetError(){ //獲取錯誤
return array($this-con-errno,$this-con-error);
}
}
$db = new db(‘127.0.0.1′,”,”,’test’);
if(!$db-query(“insert into tb (`time`,`amount`)values(‘1420085532′,’300’)”)){
var_dump($db-GetError());
die();
}
echo $db-insertid(),PHP_EOL;
$db-query(‘select * from tb’);
while($arr = $db-fetch(‘array’,MYSQLI_NUM)){
echo $arr[‘0′],’ ‘,$arr[‘1′],’ ‘,$arr[‘2′],’ ‘,PHP_EOL;
}
幫忙寫一個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寫的類?
類我就不寫了,簡單的說function吧
function selectMysql ($columns, $table, $conds=false, $extra=false) {
if (count($columns)) $col = join(“,”, $columns);
else return false;
$cond = “”;
if ($conds) $cond = “WHERE” . join(“,”, $conds);
$ex = “”;
if ($extra) $ex = $extra;
$result = array();
$q = “SELECT $col FROM $table $cond $ex”;
$s = mysql_query($q);
while ($r = mysql_fetch_assoc($r)) $result[] = $r;
if (count($result)) return $result;
return false;
}
就寫一個select吧 其他類似。
不過我感覺這樣寫意義不是很大呀~ sql操作最重要的column table conditions 等等你還是要從外面傳。
這個函數的column和condition接受數組(你改成str也行)
table和extra(主要是為了可以放點limit啊之類的)傳入str。
返回一個二維數組(如果有值的話),$result[]對應sql里的一行記錄。 $result[][]就是某行某列了。
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封裝類示例
php封裝mysql類
複製代碼
代碼如下:
?php
class
Mysql
{
private
$host;
private
$user;
private
$pwd;
private
$dbName;
private
$charset;
private
$conn
=
null;
public
function
__construct()
{
$this-host
=
‘localhost’;
$this-user
=
‘root’;
$this-pwd
=
‘root’;
$this-dbName
=
‘test’;
$this-connect($this-host,$this-user,$this-pwd);
$this-switchDb($this-dbName);
$this-setChar($this-charset);
}
//負責鏈接
private
function
connect($h,$u,$p)
{
$conn
=
mysql_connect($h,$u,$p);
$this-conn
=
$conn;
}
//負責切換數據庫
public
function
switchDb($db)
{
$sql
=
‘use’
.
$db;
$this-query($sql);
}
//負責設置字符集
public
function
setChar($char)
{
$sql
=
‘set
names’
.
$char;
$this-query($sql);
}
//負責發送sql查詢
public
function
query($sql)
{
return
mysql_query($sql,$this-conn);
}
//負責獲取多行多列的select結果
public
function
getAll($sql)
{
$list
=
array();
$rs
=
$this-query($sql);
if
(!$rs)
{
return
false;
}
while
($row
=
mysql_fetch_assoc($rs))
{
$list[]
=
$row;
}
return
$list;
}
public
function
getRow($sql)
{
$rs
=
$this-query($sql);
if(!$rs)
{
return
false;
}
return
mysql_fetch_assoc($rs);
}
public
function
getOne($sql)
{
$rs
=
$this-query($sql);
if
(!$rs)
{
return
false;
}
return
mysql_fetch_assoc($rs);
return
$row[0];
}
public
function
close()
{
mysql_close($this-conn);
}
}
echo
‘pre’;
$mysql
=
new
Mysql();
print_r($mysql);
$sql
=
“insert
into
stu
values
(4,’wangwu’,’99998′)”;
if($mysql-query($sql)){
echo
“query成功”;
}else
{
echo
“失敗”;
}
echo
“br
/”;
$sql
=
“select
*
from
stu”;
$arr
=
$mysql-getAll($sql);
print_r($arr);
?
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/247665.html