开始弄PHP,发现和ASP超像,OO还处于初级阶段,发现PHP自带的类库太强了,只要熟悉了类库,基本不用自己写什么了,先搞掂一个数据库操作类。

Code
1
<?php
2
//------------------------------------------------------------------------------------------
3
// ※MysqlDB() 构造函数,数据库初始参数
4
// ※MysqlDB2() 构造函数,数据库初始参数 (多态方法)
5
// ※Select() 查询
6
// ※GetRows() 返回查询的记录总数
7
// ※Insert() 插入记录
8
// ※Update() 更新
9
// ※Delete() 删除
10
// ※Halt() 中断并显示错误信息*/
11
//------------------------------------------------------------------------------------------
12
13
define("SERVER","localhost"); //Host name or IP address of the database server
14
define("DATABASE","gd"); //要连接的数据库名
15
define("USER","root"); //用于连接数据库的用户名
16
define("PASSWORD","hicc"); //用于连接数据库的密码
17
18
19
/**
20
* 这是个PHP对MySQL数据库操作类
21
* @author <br>
22
* @version 1.0*
23
* */
24
25
class MysqlDB
26
{
27
var $dbLink; //连接句柄
28
var $result; //查询句柄
29
var $insId; //Insert()成功返回AUTO_INCREMENT列的值
30
var $rows; //返回数据数组
31
var $numRows; //返回数据数目
32
var $dbHost, $dbUser, $userPassword, $database;
33
var $msgFlag = "yes" ; //yes:show the Mysql message ; no: die by show "Halted."
34
function MysqlDB()
35
{
36
$this->dbLink=@mysql_pconnect(SERVER,USER,PASSWORD);// or die("Can't Connect to Remote Host!");
37
@mysql_select_db(DATABASE,$this->dbLink);// or die ("Can't Connect to Remote Host!");
38
return true;
39
}
40
function MysqlDB2($dbHost=SERVER,$dbUser=USER,$userPassword=PASSWORD,$database=DATABASE)
41
{
42
43
$this->dbLink=@mysql_pconnect($dbHost,$dbUser,$userPassword);// or die("Can't Connect to Remote Host!");
44
@mysql_select_db($database,$this->dbLink);// or die ("Can't Connect to Remote Host!");
45
return true;
46
}
47
/*SQL:Select() 返回为false无结果*/
48
function Select($table,$columns,$condition=1)
49
{
50
$sql="select $columns from $table where $condition ";
51
echo $sql."<br>";
52
$this->result=@mysql_query($sql,$this->dbLink);
53
unset($this->rows);
54
if($this->result)
55
{
56
$i=0;
57
if(!($this->rows=array("$i"=>@mysql_fetch_array($this->result))))
58
return false;
59
if(($this->numRows=@mysql_num_rows($this->result))==0)
60
return false;
61
while($tempRows=@mysql_fetch_array($this->result))
62
{
63
array_push($this->rows,$tempRows);
64
}
65
}
66
else
67
{
68
$this->Halt($sql);
69
return false;
70
}
71
return true;
72
}
73
74
/*SQL:GetRows() 返回查询的记录总数*/
75
function GetRows($table,$condition=1)
76
{
77
$sql="select count(1) as count from $table where $condition";
78
//echo $sql."<br>";
79
$this->result=@mysql_query($sql,$this->dbLink);
80
if($this->result)
81
{
82
$temp=@mysql_fetch_array($this->result);
83
$this->numRows=$temp[count];
84
}
85
else
86
{
87
$this->Halt($sql);
88
return false;
89
}
90
return $this->numRows;
91
}
92
93
/*SQL:Insert()*/
94
95
function Insert($table,$columns,$values)
96
{
97
$sql="insert into $table ($columns) values ($values)";
98
//echo $sql;
99
$this->result=@mysql_query($sql,$this->dbLink);
100
if ($this->result)
101
$this->insId=@mysql_insert_id($this->dbLink);
102
else
103
{
104
$this->Halt($sql);
105
return false;
106
}
107
return true;
108
}
109
110
/*SQL:Update()*/
111
112
function Update($table,$setings,$condition)
113
{
114
$sql="update $table set $setings where $condition";
115
//echo $sql;
116
$this->result=@mysql_query($sql,$this->dbLink);
117
if ($this->result)
118
$this->numRows=@mysql_affected_rows($this->result);
119
else
120
{
121
$this->Halt($sql);
122
return false;
123
}
124
return true;
125
}
126
127
/*SQL:Delete*/
128
129
function Delete($table,$condition)
130
{
131
$sql="delete from $table where $condition";
132
$this->result=@mysql_query($sql,$this->dbLink);
133
if ($this->result)
134
$this->numRows=@mysql_affected_rows($this->result);
135
else
136
{
137
$this->Halt($sql);
138
return false;
139
}
140
141
return true;
142
}
143
144
/*Halt():error message */
145
146
function Halt($msg)
147
{
148
if($this->msgFlag=="yes")
149
{
150
printf("<b>Database Query Error:</b> %s<br>\n", $msg);
151
printf("<b>MySql Error:</b> %s<br>\n",mysql_error());
152
}
153
else
154
echo "<META HTTP-EQUIV=REFRESH CONTENT='0;URL=error.htm'>";//自定义一个出错提示文件地址
155
return false;
156
}
157
}
158
switch($db->dbType)
159
{
160
case 1:
161
@mysql_close();
162
break;
163
case 2:
164
165
break;
166
}
167
$db = new MysqlDB();
168
169
?>
170
使用方法有两种,只是初始化时不同(不知道为什么,好像构造函数不支持多态,于是只能这样了):
法一:
$db->Select("message","*");

foreach ($db->rows as &$rowss)
{printf ("ID: %s Name: %s <br>", $rowss[0], $rowss[1]);}
注:message是表名。
法二:
$db->MysqlDB2("localhost","root","hicc","gb");
$db->Select("message","*");

foreach ($db->rows as &$rowss)
{printf ("ID: %s Name: %s <br>", $rowss[0], $rowss[1]);}
看了PHP的东西,才发现,JAVA果然是很体现OO的语言。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!