<?php
class opmysql{
private $host = 'localhost'; //服务器地址
private $name = 'root'; //登录账号
private $pwd = 'ptiand'; //登录密码
private $dBase = 'db_navy'; //数据库名称
private $conn = ''; //数据库链接资源
private $result = ''; //结果集
private $msg = ''; //返回结果
private $fields; //返回字段
private $fieldsNum = 0; //返回字段数
private $rowsNum = 0; //返回结果数
private $filesArray = array(); //返回字段数组
private $rowsArray = array(); //返回结果数组
//初始化类
function __construct($host='',$name='',$pwd='',$dBase=''){
if($host != '')
$this->host = $host;
if($name != '')
$this->name = $name;
if($pwd != '')
$this->pwd = $pwd;
if($dBase != '')
$this->dBase = $dBase;
$this->init_conn();
}
//链接数据库
function init_conn(){
$this->conn=@mysql_connect($this->host,$this->name,$this->pwd);
@mysql_select_db($this->dBase,$this->conn);
mysql_query("set names gb2312");
}
//查询结果
function mysql_query_rst($sql){
if($this->conn == ''){
$this->init_conn();
}
$this->result = @mysql_query($sql,$this->conn);
}
//取得字段数
function getFieldsNum($sql){
$this->mysql_query_rst($sql);
$this->fieldsNum = @mysql_num_fields($this->result);
}
//取得查询结果数
function getRowsNum($sql){
$this->mysql_query_rst($sql);
$this->rowsNum = @mysql_num_rows($this->result);
return $this->rowsNum;
}
//取得记录数组(多条记录)
function getRowsArray($sql){
$this-> mysql_query_rst($sql);
while($row = @mysql_fetch_array($this->result,MYSQL_ASSOC)) {
$this->rowsArray[] = $row;
}
return $this->rowsArray;
}
//更新、删除、添加记录数
function uidRst($sql){
if($this->conn == ''){
$this->init_conn();
}
@mysql_query($sql);
$this->rowsNum = @mysql_affected_rows();
return $this->rowsNum;
}
//获取对应的字段值
function getFields($sql,$fields){
$this->mysql_query_rst($sql);
if(mysql_num_rows($this->result) > 0){
$tmpfld = mysql_fetch_row($this->result);
$this->fields = $tmpfld[$fields];
}
return $this->fields;
}
//错误信息
function msg_error(){
if(mysql_errno() != 0) {
$this->msg = mysql_error();
}
return $this->msg;
}
//释放结果集
function close_rst(){
//mysql_free_result($this->result);
$this->msg = '';
$this->fieldsNum = 0;
$this->rowsNum = 0;
$this->filesArray = '';
$this->rowsArray = '';
}
//关闭数据库
function close_conn(){
$this->close_rst();
mysql_close($this->conn);
$this->conn = '';
}
}
$conne = new opmysql();
?>我是自学PHP,能看懂的不多,望高手勿烦且赐教!
这是一个conn.php的连接数据库文件,但function里面的东西不知道是基于什么意图写出来的,每句的意思大概能懂,但是整个运行原理不太明白,希望能抽两三段一句一句解释一下,最主要是关联的运行原理!
class opmysql{
private $host = 'localhost'; //服务器地址
private $name = 'root'; //登录账号
private $pwd = 'ptiand'; //登录密码
private $dBase = 'db_navy'; //数据库名称
private $conn = ''; //数据库链接资源
private $result = ''; //结果集
private $msg = ''; //返回结果
private $fields; //返回字段
private $fieldsNum = 0; //返回字段数
private $rowsNum = 0; //返回结果数
private $filesArray = array(); //返回字段数组
private $rowsArray = array(); //返回结果数组
//初始化类
function __construct($host='',$name='',$pwd='',$dBase=''){
if($host != '')
$this->host = $host;
if($name != '')
$this->name = $name;
if($pwd != '')
$this->pwd = $pwd;
if($dBase != '')
$this->dBase = $dBase;
$this->init_conn();
}
//链接数据库
function init_conn(){
$this->conn=@mysql_connect($this->host,$this->name,$this->pwd);
@mysql_select_db($this->dBase,$this->conn);
mysql_query("set names gb2312");
}
//查询结果
function mysql_query_rst($sql){
if($this->conn == ''){
$this->init_conn();
}
$this->result = @mysql_query($sql,$this->conn);
}
//取得字段数
function getFieldsNum($sql){
$this->mysql_query_rst($sql);
$this->fieldsNum = @mysql_num_fields($this->result);
}
//取得查询结果数
function getRowsNum($sql){
$this->mysql_query_rst($sql);
$this->rowsNum = @mysql_num_rows($this->result);
return $this->rowsNum;
}
//取得记录数组(多条记录)
function getRowsArray($sql){
$this-> mysql_query_rst($sql);
while($row = @mysql_fetch_array($this->result,MYSQL_ASSOC)) {
$this->rowsArray[] = $row;
}
return $this->rowsArray;
}
//更新、删除、添加记录数
function uidRst($sql){
if($this->conn == ''){
$this->init_conn();
}
@mysql_query($sql);
$this->rowsNum = @mysql_affected_rows();
return $this->rowsNum;
}
//获取对应的字段值
function getFields($sql,$fields){
$this->mysql_query_rst($sql);
if(mysql_num_rows($this->result) > 0){
$tmpfld = mysql_fetch_row($this->result);
$this->fields = $tmpfld[$fields];
}
return $this->fields;
}
//错误信息
function msg_error(){
if(mysql_errno() != 0) {
$this->msg = mysql_error();
}
return $this->msg;
}
//释放结果集
function close_rst(){
//mysql_free_result($this->result);
$this->msg = '';
$this->fieldsNum = 0;
$this->rowsNum = 0;
$this->filesArray = '';
$this->rowsArray = '';
}
//关闭数据库
function close_conn(){
$this->close_rst();
mysql_close($this->conn);
$this->conn = '';
}
}
$conne = new opmysql();
?>我是自学PHP,能看懂的不多,望高手勿烦且赐教!
这是一个conn.php的连接数据库文件,但function里面的东西不知道是基于什么意图写出来的,每句的意思大概能懂,但是整个运行原理不太明白,希望能抽两三段一句一句解释一下,最主要是关联的运行原理!
解决方案 »
- 求助!求助!PHP求助!ignore_user_abort(),set_time_limit(0),定时、持续运行
- 缓存文件如何更新?
- 这样的报头用php这么接收里面的值
- 判断php语句是否执行过和两个php之间传递变量 -在线等
- 请教高手指点PHP 5 session 和cookie 生命周期问题?
- 如何使用安全的SQL搜索语句
- 关于php中gettext的用法? 高手help me!!!(大家看我写这贴子如此辛苦, 求解如此急切, 都来看看, 来者有分)(
- 急需帮助----为什么总报错!!!
- 单引号、双引号的用法?
- *****我觉得可结分的帖子*****
- Zend Studio7远程调试问题,还有一个很神奇的防火墙访问PHP问题
- smarty ie下部分css无法载入
<?php
class opmysql{ //定义数据库连接类
/************************************************************************
以下定义的变量是该类的属性,类实例化后可以通过“类对象名称->属性变量名”的形式来访问
************************************************************************/
private $host = 'localhost'; //服务器地址
private $name = 'root'; //登录账号
private $pwd = 'ptiand'; //登录密码
private $dBase = 'db_navy'; //数据库名称
private $conn = ''; //数据库链接资源
private $result = ''; //结果集
private $msg = ''; //返回结果
private $fields; //返回字段
private $fieldsNum = 0; //返回字段数
private $rowsNum = 0; //返回结果数
private $filesArray = array(); //返回字段数组
private $rowsArray = array(); //返回结果数组
/************************************************************************
以下定义类的构造函数__construct(),构造函数主要是对必要的属性进行赋值。构造函数会
在类实例化时自动运行,如果构造函数带有参数,则实例化该类时也要传递对应的参数,例如
var $mySQL=new opmysql('localhost','zhangsan','123456','mydb'); 在类定义
中还有许多以双下划线“__”为开头名称的函数,称为特殊函数,之所以特殊是因为在实例化类后,
执行某特定操作时该方法会自动执行的(故且称为“自动感应”吧),无需专门调用。
*************************************************************************/
function __construct($host='',$name='',$pwd='',$dBase=''){
if($host != '')
$this->host = $host;
/***************************************************************************
$this表示内部调用类本身,使用“->”操作符访问其属性或方法,相当于其它程序语言中的“.”
的作用(因为PHP中“.”是字符串连接操作符,所以它换成了这样的形式),注意引用属性时,属
性名称前不再用美元符号。此语句执行对属性host的赋值,下类同。实例化后的类对象也是这些的
形式访问其对象和方法,如$mySQL->host
****************************************************************************/
if($name != '')
$this->name = $name;
if($pwd != '')
$this->pwd = $pwd;
if($dBase != '')
$this->dBase = $dBase;
$this->init_conn(); //内部调用其init_conn()方法,该方法的定义见后面的init_conn()函数定义
}
/******************************************************************
以下开始定义相关方法(内部定义的函数就是其方法,有的术语叫“操作”),这些方
法中有的有返回值,有的没有返回值。多数方法中还调用了其它方法。
*******************************************************************/
//链接数据库方法
function init_conn(){
$this->conn=@mysql_connect($this->host,$this->name,$this->pwd);
@mysql_select_db($this->dBase,$this->conn);
mysql_query("set names gb2312");
}
//查询结果
function mysql_query_rst($sql){
if($this->conn == ''){
$this->init_conn();
}
$this->result = @mysql_query($sql,$this->conn);
}
//取得字段数
function getFieldsNum($sql){
$this->mysql_query_rst($sql);
$this->fieldsNum = @mysql_num_fields($this->result);
}
//取得查询结果数
function getRowsNum($sql){
$this->mysql_query_rst($sql);
$this->rowsNum = @mysql_num_rows($this->result);
return $this->rowsNum;
}
//取得记录数组(多条记录)
function getRowsArray($sql){
$this-> mysql_query_rst($sql);
while($row = @mysql_fetch_array($this->result,MYSQL_ASSOC)) {
$this->rowsArray[] = $row;
}
return $this->rowsArray;
}
//更新、删除、添加记录数
function uidRst($sql){
if($this->conn == ''){
$this->init_conn();
}
@mysql_query($sql);
$this->rowsNum = @mysql_affected_rows();
return $this->rowsNum;
}
//获取对应的字段值
function getFields($sql,$fields){
$this->mysql_query_rst($sql);
if(mysql_num_rows($this->result) > 0){
$tmpfld = mysql_fetch_row($this->result);
$this->fields = $tmpfld[$fields];
}
return $this->fields;
} //错误信息
function msg_error(){
if(mysql_errno() != 0) {
$this->msg = mysql_error();
}
return $this->msg;
}
//释放结果集
function close_rst(){
//mysql_free_result($this->result);
$this->msg = '';
$this->fieldsNum = 0;
$this->rowsNum = 0;
$this->filesArray = '';
$this->rowsArray = '';
}
//关闭数据库
function close_conn(){
$this->close_rst();
mysql_close($this->conn);
$this->conn = '';
}
}
$conne = new opmysql();
?>
这是php的类(class),php的类对初学是道坎,很多人因此止步
说来话长,查资料吧
if($host != '')
$this->host = $host;
if($name != '')
$this->name = $name;
if($pwd != '')
$this->pwd = $pwd;
if($dBase != '')
$this->dBase = $dBase;
$this->init_conn();
}
不太明白他的用处,像
//链接数据库方法
function init_conn(){
$this->conn=@mysql_connect($this->host,$this->name,$this->pwd);
@mysql_select_db($this->dBase,$this->conn);
mysql_query("set names gb2312");
} 为什么不能直接写
function init_conn(){
$this->conn=@mysql_connect($host,$name,$pwd);
@mysql_select_db($dBase,$conn);
mysql_query("set names gb2312");
}