本文目录一览:
- 1、怎么用PHP获取SQL表数据记录分页显示
- 2、php封装一个class类实现mysql数据库的增删该查
- 3、php 单例模式
- 4、vb怎么连接sql?
- 5、private function是什么意思
- 6、如何在VB中模拟硬件冲突的声音?
怎么用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