privatefunction_build_sql()的簡單介紹

本文目錄一覽:

怎麼用PHP獲取SQL表數據記錄分頁顯示

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

?php

class Page {

private $total; //數據表中總記錄數

private $listRows; //每頁顯示行數

private $limit;

private $uri;

private $pageNum; //頁數

private $config=array(‘header’=”個記錄”, “prev”=”上一頁”, “next”=”下一頁”, “first”=”首 頁”, “last”=”尾 頁”);

private $listNum=8;

/*

* $total

* $listRows

*/

public function __construct($total, $listRows=10, $pa=””){

$this-total=$total;

$this-listRows=$listRows;

$this-uri=$this-getUri($pa);

$this-page=!empty($_GET[“page”]) ? $_GET[“page”] : 1;

$this-pageNum=ceil($this-total/$this-listRows);

$this-limit=$this-setLimit();

}

private function setLimit(){

return “Limit “.($this-page-1)*$this-listRows.”, {$this-listRows}”;

}

private function getUri($pa){

$url=$_SERVER[“REQUEST_URI”].(strpos($_SERVER[“REQUEST_URI”], ‘?’)?”:”?”).$pa;

$parse=parse_url($url);

if(isset($parse[“query”])){

parse_str($parse[‘query’],$params);

unset($params[“page”]);

$url=$parse[‘path’].’?’.http_build_query($params);

}

return $url;

}

private function __get($args){

if($args==”limit”)

return $this-limit;

else

return null;

}

private function start(){

if($this-total==0)

return 0;

else

return ($this-page-1)*$this-listRows+1;

}

private function end(){

return min($this-page*$this-listRows,$this-total);

}

private function first(){

if($this-page==1)

$html.=”;

else

$html.=” a href=’javascript:setPage(\”{$this-uri}page=1\”)'{$this-config[“first”]}/a “;

return $html;

}

private function prev(){

if($this-page==1)

$html.=”;

else

$html.=” a href=’javascript:setPage(\”{$this-uri}page=”.($this-page-1).”\”)'{$this-config[“prev”]}/a “;

return $html;

}

private function pageList(){

$linkPage=””;

$inum=floor($this-listNum/2);

for($i=$inum; $i=1; $i–){

$page=$this-page-$i;

if($page1)

continue;

$linkPage.=” a href=’javascript:setPage(\”{$this-uri}page={$page}\”)'{$page}/a “;

}

$linkPage.=” {$this-page} “;

for($i=1; $i=$inum; $i++){

$page=$this-page+$i;

if($page=$this-pageNum)

$linkPage.=” a href=’javascript:setPage(\”{$this-uri}page={$page}\”)'{$page}/a “;

else

break;

}

return $linkPage;

}

private function next(){

if($this-page==$this-pageNum)

$html.=”;

else

$html.=” a href=’javascript:setPage(\”{$this-uri}page=”.($this-page+1).”\”)'{$this-config[“next”]}/a “;

return $html;

}

private function last(){

if($this-page==$this-pageNum)

$html.=”;

else

$html.=” a href=’javascript:setPage(\”{$this-uri}page=”.($this-pageNum).”\”)'{$this-config[“last”]}/a “;

return $html;

}

private function goPage(){

return ‘ input type=”text” onkeydown=”javascript:if(event.keyCode==13){var page=(this.value’.$this-pageNum.’)?’.$this-pageNum.’:this.value;setPage(\”.$this-uri.’page=\’+page+\’\’)}” value=”‘.$this-page.'” style=”width:25px”input type=”button” value=”GO” onclick=”javascript:var page=(this.previousSibling.value’.$this-pageNum.’)?’.$this-pageNum.’:this.previousSibling.value;setPage(\”.$this-uri.’page=\’+page+\’\’)” ‘;

}

function fpage($display=array(0,1,2,3,4,5,6,7,8)){

$html[0]=” 共有b{$this-total}/b{$this-config[“header”]} “;

$html[1]=” 每頁顯示b”.($this-end()-$this-start()+1).”/b條,本頁b{$this-start()}-{$this-end()}/b條 “;

$html[2]=” b{$this-page}/{$this-pageNum}/b頁 “;

$html[3]=$this-first();

$html[4]=$this-prev();

$html[5]=$this-pageList();

$html[6]=$this-next();

$html[7]=$this-last();

$html[8]=$this-goPage();

$fpage=”;

foreach($display as $index){

$fpage.=$html[$index];

}

return $fpage;

}

}

php封裝一個class類實現mysql資料庫的增刪該查

?php

class db{

private $db;

const MYSQL_OPT_READ_TIMEOUT = 11;

const MYSQL_OPT_WRITE_TIMEOUT = 12;

private $tbl_name;

private $where;

private $sort;

private $fields;

private $limit;

public static $_instance = null;

function __construct(){

$cfg = loadConfig(‘db’);

$db = mysqli_init();

$db-options(self::MYSQL_OPT_READ_TIMEOUT, 3);

$db-options(self::MYSQL_OPT_WRITE_TIMEOUT, 1);

@$db-real_connect($cfg[‘host’],$cfg[‘user’],$cfg[‘pwd’],$cfg[‘db’]);

if ($db-connect_error) {

$this-crash($db-errno,$db-error);

}

$db-set_charset(“utf8”);

$this-db = $db;

//echo $this-db-stat;

}

public static function getInstance(){

if(!(self::$_instance instanceof self)){

self::$_instance = new self();

}

return self::$_instance;

}

private function __clone() {} //覆蓋__clone()方法,禁止克隆

public function find($conditions = null){

if($conditions) $this-where($conditions);

return $this-getArray($this-buildSql(),1);

}

public function findAll($conditions = null){

if($conditions) $this-where($conditions);

return $this-getArray($this-buildSql());

}

//表

public function t($table){ $this-tbl_name = $table; return $this;}

//條件

public function where($conditions){

$where = ”;

if(is_array($conditions)){

$join = array();

foreach( $conditions as $key = $condition ){

$condition = $this-db-real_escape_string($condition);

$join[] = “`{$key}` = ‘{$condition}'”;

}

$where = “WHERE “.join(” AND “,$join);

}else{

if(null != $conditions) $where = “WHERE “.$conditions;

}

$this-where = $where;

return $this;

}

//排序

public function sort($sort){

if(null != $sort) $sort = “ORDER BY {$sort}”;

$this-sort = $sort;

return $this;

}

//欄位

public function fields($fields){ $this-fields = $fields; return $this; }

public function limit($limit){$this-limit = $limit; return $this;}

private function buildSql(){

$this-fields = empty($this-fields) ? “*” : $this-fields;

$sql = “SELECT {$this-fields} FROM {$this-tbl_name} {$this-where} {$this-sort}”;

accessLog(‘db_access’,$sql);

if(null != $this-limit)$sql .= ” limit {$this-limit}”;

return $sql;

}

/**

* 返回查詢數據

* @param $sql

* @param bool $hasOne

* @return array|bool|mixed

*/

private function getArray($sql,$hasOne = false){

if($this-db-real_query($sql) ){

if ($result = $this-db-use_result()) {

$row = array();

if($hasOne){

$row = $result-fetch_assoc();

}else{

while($d = $result-fetch_assoc()) $row[] = $d;

}

$result-close();

$this-fields = “*”;

return $row;

}else{

return false;

}

}else{

if($this-db-error){

$this-crash($this-db-errno,$this-db-error,$sql);

}

}

}

public function findSql($sql,$hasOne = false){

accessLog(‘db_access’,$sql);

if($this-db-real_query($sql) ){

if ($result = $this-db-use_result()) {

$row = array();

if($hasOne){

$row = $result-fetch_assoc();

}else{

while($d = $result-fetch_assoc()) $row[] = $d;

}

$result-close();

$this-fields = “*”;

return $row;

}else{

return false;

}

}else{

if($this-db-error){

$this-crash($this-db-errno,$this-db-error,$sql);

}

}

}

public function create($row){

if(!is_array($row))return FALSE;

$row = $this-prepera_format($row);

if(empty($row))return FALSE;

foreach($row as $key = $value){

$cols[] = ‘`’.$key.’`’;

$vals[] = “‘”.$this-db-real_escape_string($value).”‘”;

}

$col = implode(‘,’, $cols);

$val = implode(‘,’, $vals);

$sql = “INSERT INTO `{$this-tbl_name}` ({$col}) VALUES ({$val})”;

accessLog(‘db_access’,$sql);

if( FALSE != $this-db-query($sql) ){ // 獲取當前新增的ID

if($this-db-insert_id){

return $this-db-insert_id;

}

if($this-db-affected_rows){

return true;

}

}

return FALSE;

}

//直接執行sql

public function runSql($sql){

accessLog(‘db_access’,$sql);

if( FALSE != $this-db-query($sql) ){ // 獲取當前新增的ID

return true;

}else{

return false;

}

}

public function update($row){

$where = “”;

$row = $this-prepera_format($row);

if(empty($row))return FALSE;

foreach($row as $key = $value){

$value = $this-db-real_escape_string($value);

$vals[] = “`{$key}` = ‘{$value}'”;

}

$values = join(“, “,$vals);

$sql = “UPDATE {$this-tbl_name} SET {$values} {$this-where}”;

accessLog(‘db_access’,$sql);

if( FALSE != $this-db-query($sql) ){ // 獲取當前新增的ID

if( $this-db-affected_rows){

return true;

}

}

return false;

}

function delete(){

$sql = “DELETE FROM {$this-tbl_name} {$this-where}”;

if( FALSE != $this-db-query($sql) ){ // 獲取當前新增的ID

if( $this-db-affected_rows){

return true;

}

}

return FALSE;

}

private function prepera_format($rows){

$columns = $this-getArray(“DESCRIBE {$this-tbl_name}”);

$newcol = array();

foreach( $columns as $col ){

$newcol[$col[‘Field’]] = $col[‘Field’];

}

return array_intersect_key($rows,$newcol);

}

//崩潰信息

private function crash($number,$message,$sql=”){

$msg = ‘Db Error ‘.$number.’:’.$message ;

if(empty($sql)){

echo t(‘db_crash’);

}else{

$msg .= ” SQL:”.$sql;

echo t(‘db_query_err’);

}

accessLog(‘db_error’,$msg);

exit;

}

}

php 單例模式

單例模式是一種常用的軟體設計模式,可以保證系統中一個類只有一個實例,從而達到節約系統資源提升特殊類使用效率的目的

php實現單例模式的方法

class A {

    //靜態屬性

    private static $_instance;

    

    //空的克隆方法,防止被克隆

    private function __clone() {}

    

    //獲取實例

    public static function getInstance() {

        if(!(self::$_instance instanceof self)) {

          self::$_instance = new A();

        }

        return self::$_instance;

    }

}

//調用

$obj = A::getInstance();

vb怎麼連接sql?

一、配置ODBC數據源

1、在控制面板中,雙擊管理工具,然後打開ODBC數據源管理器。

2、在「系統DSN」選項卡中,單擊「添加」按鈕,打開「創建新數據源」對話框,在「名稱」列表框中選擇「SQL Server」。選好單擊完成

3、在打開「建立新的數據源到SQL Server」對話框,在「名稱」文本框輸入新數據源的名稱,描述數據源按理解的方式來寫(隨意)。「伺服器」就選擇要連接到的伺服器。

4、選擇使用用戶輸入登錄的ID和密碼的SQL 驗證

。選連接SQL默認設置

5、再下一步下一步,完成。測試數據源看連接是否成功就行了。成功後按確定。

二、VB中設置連接

1、添加部件Mircrosoft ADO Data Control 6.0(OLEDB),把部件拖到窗體。

2、對ADO部件點右鍵選屬性,選擇使用連接字元串,按生成。

3、選擇Mircosoft OLE DB Providar for SQL Server

按下一步

1、輸入伺服器名稱

2、使用指定的伺服器信息

3、在伺服器上選擇資料庫

這時就選擇在SQL Server建好的資料庫就行了

5、測試連接可以看到連接是否成功。

private function是什麼意思

private function

私有函數;私有函式;私用函數

例句篩選

1.

Property procedure to use the private function.

屬性過程中的代碼以使用私有函數。

2.

Class, you will create a public method that returns a full name, and a privatefunction to calculate the age.

類中,您將創建一個返回全名的公共方法和一個計算年的私有函數。

如何在VB中模擬硬體衝突的聲音?

BEEP是發出一個提示音,如果要模擬的話,不如自己做一個聲音文件,然後播放它

播放代碼請看這個

Private

Declare

Function

sndPlaySound

Lib

“winmm.dll”

Alias

“sndPlaySoundA”

(lpszSoundName

As

Any,

ByVal

uFlags

As

Long)

As

Long

Private

Declare

Function

sndStopSound

Lib

“winmm.dll”

Alias

“sndPlaySoundA”

(ByVal

lpszNull

As

Long,

ByVal

uFlags

As

Long)

As

Long

Private

Const

SND_MEMORY

=

H4

Private

Const

SND_SYNC

=

H0

Private

Const

SND_ASYNC

=

H1

Private

Const

SND_NODEFAULT

=

H2

Private

Const

SND_LOOP

=

H8

Private

Const

SND_NOSTOP

=

H10

Private

arrBound()

As

Byte

Private

arrFired()

As

Byte

Private

arrNoBullet()

As

Byte

Private

Function

PlaySound(ind

As

Integer)

As

Boolean

Dim

r

As

Long

Dim

uFlags

As

Long

uFlags

=

SND_ASYNC

Or

SND_NODEFAULT

Or

SND_MEMORY

r

=

sndStopSound(0,

SND_ASYNC)

Select

Case

ind

Case

‘Fire

r

=

sndPlaySound(arrFired(0),

uFlags)

Case

1

‘Shotted

r

=

sndPlaySound(arrBound(0),

uFlags)

Case

2

‘esNoBullet

r

=

sndPlaySound(arrNoBullet(0),

uFlags)

End

Select

End

Function

Private

Function

LoadSound()

As

Boolean

arrBound

=

LoadResData(“Bound”,

“WAVE”)

arrFired

=

LoadResData(“Fire”,

“WAVE”)

arrNoBullet

=

LoadResData(“NoBullet”,

“WAVE”)

End

Function

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/230346.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-10 18:17
下一篇 2024-12-10 18:17

相關推薦

  • Python簡單數學計算

    本文將從多個方面介紹Python的簡單數學計算,包括基礎運算符、函數、庫以及實際應用場景。 一、基礎運算符 Python提供了基礎的算術運算符,包括加(+)、減(-)、乘(*)、除…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • Python海龜代碼簡單畫圖

    本文將介紹如何使用Python的海龜庫進行簡單畫圖,並提供相關示例代碼。 一、基礎用法 使用Python的海龜庫,我們可以控制一個小海龜在窗口中移動,並利用它的「畫筆」在窗口中繪製…

    編程 2025-04-29
  • Python櫻花樹代碼簡單

    本文將對Python櫻花樹代碼進行詳細的闡述和講解,幫助讀者更好地理解該代碼的實現方法。 一、簡介 櫻花樹是一種圖形效果,它的實現方法比較簡單。Python中可以通過turtle這…

    編程 2025-04-28
  • Python大神作品:讓編程變得更加簡單

    Python作為一種高級的解釋性編程語言,一直被廣泛地運用於各個領域,從Web開發、遊戲開發到人工智慧,Python都扮演著重要的角色。Python的代碼簡潔明了,易於閱讀和維護,…

    編程 2025-04-28
  • 用Python實現簡單爬蟲程序

    在當今時代,互聯網上的信息量是爆炸式增長的,其中很多信息可以被利用。對於數據分析、數據挖掘或者其他一些需要大量數據的任務,我們可以使用爬蟲技術從各個網站獲取需要的信息。而Pytho…

    編程 2025-04-28
  • 如何製作一個簡單的換裝遊戲

    本文將從以下幾個方面,為大家介紹如何製作一個簡單的換裝遊戲: 1. 遊戲需求和界面設計 2. 使用HTML、CSS和JavaScript開發遊戲 3. 實現遊戲的基本功能:拖拽交互…

    編程 2025-04-27
  • Guava Limiter——限流器的簡單易用

    本文將從多個維度對Guava Limiter進行詳細闡述,介紹其定義、使用方法、工作原理和案例應用等方面,並給出完整的代碼示例,希望能夠幫助讀者更好地了解和使用該庫。 一、定義 G…

    編程 2025-04-27
  • 製作一個簡單的管理系統的成本及實現

    想要製作一個簡單的管理系統,需要進行技術選型、開發、測試等過程,那麼這個過程會花費多少錢呢?我們將從多個方面來闡述製作一個簡單的管理系統的成本及實現。 一、技術選型 當我們開始思考…

    編程 2025-04-27
  • 2的32次方-1:一個看似簡單卻又複雜的數字

    對於計算機領域的人來說,2的32次方-1(也就是十進位下的4294967295)這個數字並不陌生。它經常被用來表示IPv4地址或者無符號32位整數的最大值。但實際上,這個數字卻包含…

    編程 2025-04-27

發表回復

登錄後才能評論