PHP讀取MySQL的class快速用法

這是自己寫的基本PHP的mysql寫法。要安全的mysqli寫法有空再加上來。
很好用喔!不用再一個字一個字key重複的文字了。

*select傳統用法:
$Q = "select 欄位A,欄位B from 資料表 其他條件";
$Q = mysql_query($Q);
//若輸出多筆 
while( list($A,$B) = mysql_fetch_row($Q) )     
    {
    echo $A;   
     echo $B; 
    }
 //若輸出一筆
list($A,$B) = mysql_fetch_row($Q) ;
echo $A;
echo $B;
——

*我的用法:

 $j = new sql;

 //多筆查詢(用在動態顯示,0~n筆) 
$list = $j->select("id , title" , "table" , "");
if($list != 0) //若資料庫沒有資料會回傳0
    {
    foreach( $list as $row )
{
echo $row['id'];
echo $row['title'];
//陣列row裡面的key可以直接對應欄位名稱。不用再另外取名。
}
    }
//確定只要取一筆資料請用這個selectone 若用select會有陣列問題存在。
$row = $j->selectone("id , title" , "table" , "where id = 1"); 
 if( $row != 0 )
    echo $row['id'];

 
//若不小心打錯而產生錯誤,可以快速檢察SQL指令。在後面多打1,例如
  $j->select("欄位名稱" , "資料表" , "其他條件" , 1 );  //輸入1會終止後續輸出,這是方便用來檢視SQL程式碼錯誤。

//其他用法,請參考class裡面的註解說明囉!
$j->insert(); //新增 
$j->update(); //修改
$j->delete(); //刪除
$j->truncate(); //清空
$j->num_rows() //計算有多少列 

//若要計算有多少列 ,也可以用這種方法
$list = $j->select("id , title" , "table" , ""); 
echo count( $list );

 

 

/*================使用資料庫Mysql的類別開始==================*/
class sql
    {
    //執行
    function query($select,$from,$else,$returnSQLstring=0)
{
$Q="select $select from $from $else";
if($returnSQLstring==0)
$Q=mysql_query($Q) or die("SQL查詢敘述錯誤!");
else
die($Q);
return $Q;
}

    //查詢(欄位,表格,from之後的其他敘述或是限制),使用loop適合
    function select($select,$from,$else,$returnSQLstring=0)
{
$this->recruit = $this->query($select,$from,$else,$returnSQLstring);
$num = $this->num_rows($this->recruit); //計算數量

//不同筆數會回傳不同的形式
if($num==0) //沒有資料
{
return 0;
}
elseif($num==1) //如果只有一筆 需使用limit限定避免筆數時多時一,不建議使用!!!
{
//第一種
//return mysql_fetch_row($this->recruit);
//第二種
$datalist=array();
while($row=mysql_fetch_array($this->recruit))
{
array_push($datalist,$row);
}
return $datalist;
}
elseif($num>=2)
{
$datalist=array();
while($row=mysql_fetch_array($this->recruit))
{
array_push($datalist,$row);
}
return $datalist;
}
}
    
    //取出單列資料,不是使用loop時(已事先確定只取出一筆)
    function selectone($select,$from,$else,$returnSQLstring=0)
{
$this->recruit = $this->query($select,$from,$else,$returnSQLstring);
$num = $this->num_rows($this->recruit); //計算數量
if($num==0) //沒有資料
return 0;
if($num>1)
return die("限定只能取出一筆資料");
else
{
$datalist=array();
while($row=mysql_fetch_array($this->recruit))
{
array_push($datalist,$row);
}
$datalist = $datalist[0];
return $datalist;
}
}


    //新增
    function insert($inserttable,$insertfile,$val,$returnSQLstring=0)
{
$Q="insert into {$inserttable}({$insertfile})
values({$val})";
if($returnSQLstring==0)
{$Q = mysql_query($Q) or die("SQL新增敘述錯誤!");}
else
{die("$Q");}
return $Q;
}

    //修改
    function update($updatetable,$setfile,$else,$returnSQLstring=0)
{
$Q="update $updatetable
set $setfile
$else";
if($returnSQLstring==0)
$Q = mysql_query($Q) or die("SQL修改敘述錯誤!");
else
die($Q);
return $Q;
}
    //刪除
    //為了避免忘記設定條件而導致刪除所有資料,在此限制一定要使用where以卻保安全
    //若要一次刪除所有資料,請改用清空。
    function delete($deletetable,$where,$returnSQLstring=0)
{
$Q="delete from {$deletetable}
where {$where}";
if($returnSQLstring==0)
$Q = mysql_query($Q) or die("SQL刪除敘述錯誤!");
else
die($Q);
return $Q;
}
    
    //清空
    function truncate($table,$returnSQLstring=0)
{
$Q="truncate {$table}";
if($returnSQLstring==0)
$Q = mysql_query($Q) or die("SQL清空敘述錯誤!");
else
die($Q);
}

    //計算有多少筆資料
    function num_rows($query_recruit)
{
$num = mysql_num_rows($query_recruit);
return $num;
}
    }
/*================取出資料庫Mysql的類別結束==================*/

發表迴響