php中一个类调用另一个类的函数出现错误.

luchongzhi 2009-08-03 02:46:11
这个是数据库连接类:dbconclass.php

<?php
define('MYSQL_TYPES_NUMERIC', 'int real ');
define('MYSQL_TYPES_DATE', 'datetime timestamp year date time ');
define('MYSQL_TYPES_STRING', 'string blob ');
class dbconclass {
private $last_error; //最后一次执行数据库错误.
private $last_query; //最后一次查询数据库操作.
private $row_count; //holds the last number of rows from a select

private $host; //Mysql 数据库连接主机
private $user; //Mysql 数据库连接用户
private $pwd; //Mysql 数据库连接密码
private $dbname; //Mysql 选择数据库名称

private $db_link; //Mysql 数据库连接
private $auto_slashes; //the class will add/strip slashes when it can
function __construct() {

$this->host="localhost";
$this->user="";
$this->pwd="";

$this->auto_slashes=TRUE;
}
function connect($host="",$user="",$pwd="",$dbname="",$persistant=true){
if (!empty($host)) $this->host=$host;
if (!empty($user)) $this->user=$user;
if (!empty($pwd)) $this->pwd=$pwd;

if ($persistant){
$this->db_link=mysql_pconnect($this->host,$this->user,$this->pwd);
}else {
$this->db_link=mysql_connect($this->host,$this->user,$this->pwd);
}

//判断数据库是否连接正确.
if (!$this->db_link){
$this->last_error=mysql_error();
return FALSE;
}else { //connect success
if (!$this->select_db($dbname)){ // select db error.
return FALSE;
}else {
return $this->db_link; //success.
}
}
}
function select_db($dbname=""){
if (!empty($dbname)) $this->dbname=$dbname;

if (!mysql_select_db($this->dbname)){
$this->last_error=mysql_error();
return FALSE;
}else {
mysql_query("SET NAMES 'UTF-8'");
return TRUE; //选择数据库成功.
}
}
function querySQL($sql){
$this->last_query=$sql;

$result=mysql_query($sql);
if (!$result){
$this->last_error=mysql_error();
return FALSE;
}else {
$this->row_count=mysql_num_rows($result);
return $result;
}
}
function get_Array($result){
if (!$result){
$this->last_error="Invalid resource identifier passed to get_Array() function.";
return FALSE;
}else {
$row=mysql_fetch_array($result);
if ($row==FALSE){
$this->last_error=mysql_error();
return FALSE;
}else {
return $row;
}
}
}
function get_Num($result){
if (!$result) {
$this->last_error="Invalid resource identifier passed to get_Num() function.";
return FALSE;
}else {
$count=mysql_num_rows($result);
if ($count<0){
$this->last_error="The results count can not be less than zero.";
return FALSE;
}else {
return $count;
}
}
}
function get_Row($result,$type="MYSQL_BOTH"){
if (!$result){
$this->last_error="Invalid resource identifier passed to get_Row() function.";
return FALSE;
}

if ($type == 'MYSQL_ASSOC') $row = mysql_fetch_array($result, MYSQL_ASSOC);
if ($type == 'MYSQL_NUM') $row = mysql_fetch_array($result, MYSQL_NUM);
if ($type == 'MYSQL_BOTH') $row = mysql_fetch_array($result, MYSQL_BOTH);

if (!$row) {
return FALSE;
}else {
if ($this->auto_slashes) {
// strip all slashes out of row...
foreach ($row as $key => $value) {
$row[$key] = stripslashes($value);
}
}
return $row;
}
}
function get_One($sql){
$this->last_query=$sql;
$r=mysql_query($sql);
if (!$r){
$this->last_error=mysql_error();
return FALSE;
}
if (mysql_num_rows($r)>1){
$this->last_error="查询函数get_One()返回的结果记录数大于一.";
return FALSE;
}
if (mysql_num_rows($r)<1){
$this->last_error="查询函数get_One()没有结果记录返回.";
return FALSE;
}
$ret=mysql_result($r,0);
mysql_free_result($r);

if ($this->auto_slashes) {
return stripslashes($ret);
}else {
return $ret;
}
}
function insert_SQL($sql){
$this->last_query=$sql;

$result=mysql_query($sql);
if (!$result) {
$this->last_error=mysql_error();
return FALSE;
}else {
$id=mysql_insert_id();
if ($id==0){
return TRUE;
}else {
return $id;
}
}
}
function insert_Array($table,$data){
if (empty($data)) {
$this->last_error = "You must pass an array to the insert_array() function.";
return false;
}

$cols = '(';
$values = '(';
foreach ($data as $key=>$value) { // iterate values to input

$cols .= "$key,";

$col_type = $this->get_column_type($table, $key); // get column type
if (!$col_type) return false; // error!

// determine if we need to encase the value in single quotes
if (is_null($value)) {
$values .= "NULL,";
}
elseif (substr_count(MYSQL_TYPES_NUMERIC, "$col_type ")) {
$values .= "$value,";
}
elseif (substr_count(MYSQL_TYPES_DATE, "$col_type ")) {
$value = $this->sql_date_format($value, $col_type); // format date
$values .= "'$value',";
}
elseif (substr_count(MYSQL_TYPES_STRING, "$col_type ")) {
if ($this->auto_slashes) $value = addslashes($value);
$values .= "'$value',";
}
}
$cols = rtrim($cols, ',').')';
$values = rtrim($values, ',').')';
// insert values
$sql = "INSERT INTO $table $cols VALUES $values";
return $this->insert_SQL($sql);
}
function get_insertId(){
return @mysql_insert_id();
}
function update_SQL($sql) {

$this->last_query = $sql;

$r = mysql_query($sql);
if (!$r) {
$this->last_error = mysql_error();
return false;
}

$rows = mysql_affected_rows();
if ($rows == 0)
return true; // no rows were updated
else
return $rows;
}
function update_Array($table,$data,$condition){
if (empty($data)) {
$this->last_error = "You must pass an array to the update_array() function.";
return false;
}

$sql = "UPDATE $table SET";
foreach ($data as $key=>$value) { // iterate values to input

$sql .= " $key=";

$col_type = $this->get_column_type($table, $key); // get column type
if (!$col_type) return false; // error!

// determine if we need to encase the value in single quotes
if (is_null($value)) {
$sql .= "NULL,";
}
elseif (substr_count(MYSQL_TYPES_NUMERIC, "$col_type ")) {
$sql .= "$value,";
}
elseif (substr_count(MYSQL_TYPES_DATE, "$col_type ")) {
$value = $this->sql_date_format($value, $col_type); // format date
$sql .= "'$value',";
}
elseif (substr_count(MYSQL_TYPES_STRING, "$col_type ")) {
if ($this->auto_slashes) $value = addslashes($value);
$sql .= "'$value',";
}

}
$sql = rtrim($sql, ','); // strip off last "extra" comma
if (!empty($condition)) $sql .= " WHERE $condition";

// insert values
return $this->update_SQL($sql);
}
function get_column_type($table, $column) {
$r = mysql_query("SELECT $column FROM $table");
if (!$r) {
$this->last_error = mysql_error();
return false;
}
$ret = mysql_field_type($r, 0);
if (!$ret) {
$this->last_error = "Unable to get column information on $table.$column.";
mysql_free_result($r);
return false;
}
mysql_free_result($r);
return $ret;
}
function sql_date_format($value) {
if (gettype($value) == 'string') $value = strtotime($value);
return date('Y-m-d H:i:s', $value);
}
function print_last_error($show_query=true) {
?>
<div style="border: 1px solid red; font-size: 9pt; font-family: monospace; color: red; padding: .5em; margin: 8px; background-color: #FFE2E2">
<span style="font-weight: bold">db.class.php Error:</span><?= $this->last_error ?>
</div>
<?
if ($show_query && (!empty($this->last_query))) {
$this->print_last_query();
}
}
function print_last_query() {
?>
<div style="border: 1px solid blue; font-size: 9pt; font-family: monospace; color: blue; padding: .5em; margin: 8px; background-color: #E6E5FF">
<span style="font-weight: bold">Last SQL Query:</span><?= str_replace("\n", '<br>', $this->last_query) ?>
</div>
<?
}
}
?>

...全文
807 点赞 收藏 10
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
luchongzhi 2009-08-03
楼上的高手指点下我的类的那个调用是否合理呢?具体应该怎样调用?谢谢.(附:6楼调用方法)
回复
phpboy 2009-08-03
直接这样:

class userDAO extends dbconclass {

}


直接就可以用 $this->xxx 调用
回复
foolbirdflyfirst 2009-08-03
[Quote=引用 6 楼 luchongzhi 的回复:]
PHP codeprivate$user_db;function __construct() {if (require_once'../db/dbconclass.php'){$dbcon=new dbconclass();$user_db=$dbcon->connect(constant("HOST"),constant("USER"),constant("PWD"),constant("DB_USER"));if (!$user_db)$dbcon->print_last_error(TRUE);else$this->user_db=$dbcon;
}else {thrownewException("The File dbconclass.php not found.");
}
}function getUserInfoByName($username){$sql="select * from".constant("TB_USERINFO")." where username='{$username}'";$result=$this->user_db->querySQL($sql);if ($result){return$this->user_db->get_Array($result);
}else {return$this->user_db->print_last_error(TRUE);
}
}
我这样能够调用那个方法,也能取到所要的值..但是我不知道这样合不合理?
[/Quote]
类怎么设计,你就相应怎么使用,为什么会有不合理的担心呢?
回复
phpboy 2009-08-03
1、private $pwd = '';
2、


function select_db($dbname=""){
if (!empty($dbname)) $this->dbname=$dbname;

if (!mysql_select_db($this->dbname, $this->db_link)){ //$this->link 应该是 connect 返回的
$this->last_error=mysql_error();
return FALSE;
}else {
mysql_query("SET NAMES 'UTF-8'");
return TRUE; //选择数据库成功.
}
}


回复
luchongzhi 2009-08-03

private $user_db;
function __construct() {
if (require_once '../db/dbconclass.php'){
$dbcon=new dbconclass();
$user_db=$dbcon->connect(constant("HOST"),constant("USER"),constant("PWD"),constant("DB_USER"));
if (!$user_db) $dbcon->print_last_error(TRUE);
else $this->user_db=$dbcon;
}else {
throw new Exception("The File dbconclass.php not found.");
}
}
function getUserInfoByName($username){
$sql="select * from ".constant("TB_USERINFO")." where username='{$username}'";

$result=$this->user_db->querySQL($sql);
if ($result){
return $this->user_db->get_Array($result);
}else {
return $this->user_db->print_last_error(TRUE);
}
}

我这样能够调用那个方法,也能取到所要的值..但是我不知道这样合不合理?
回复
blueforyou 2009-08-03
dbconclass 这个类应该先实例化再使用。
回复
阿_布 2009-08-03
$result=dbconclass::querySQL($sql);
你的querySQL()方法不是静态方法,怎么可以这样调用?
回复
foolbirdflyfirst 2009-08-03
LZ弄明白了什么是'类',什么是'对象'吗?
你执行了一个类的方法(dbconclass::connect),方法内部却使用$this关键字,$this指向的的是当前类的实例化对象.
你应该new一个dbconclass的对象,然后用 $对象->方法() 的方式调用。
回复
luchongzhi 2009-08-03
[client 192.168.102.236] PHP Notice: Undefined property: userDAO::$pwd in /var/www/html/goodyitem/purchase/db/dbconclass.php on line 61
[client 192.168.102.236] PHP Fatal error: Call to undefined method userDAO::select_db() in /var/www/html/goodyitem/purchase/db/dbconclass.php on line 71
提示的是这个错误.
回复
luchongzhi 2009-08-03
在另一个类中调用数据库连接类的函数:

<?php
session_start();
require_once '../db/dbconclass.php';

class userDAO {
function __construct() {
$user_db=dbconclass::connect("localhost","root","","brilliant_purchase");
if (!$user_db) dbconclass::print_last_error(TRUE);
}

function getUserByName($username){
$sql="select * from tb_supplierdetailinfo where supplierno='{$username}'";

$result=dbconclass::querySQL($sql);
if ($result){
print_r(dbconclass::get_Array($result));
}else {
dbconclass::print_last_error(TRUE);
}
}
}
$user=new userDAO();
echo $user->getUserByName("HP");
?>

这样调用总是出错.
回复
发动态
发帖子
基础编程
创建于2007-09-28

2.1w+

社区成员

从PHP安装配置,PHP入门,PHP基础到PHP应用
申请成为版主
社区公告
暂无公告