class database
defined( '_VALID_URL' ) or die( '直接访问这个地址是不允许的' );
class database {
/** @var string Internal variable to hold the query sql */
var $_sql='';
/** @var int Internal variable to hold the database error number */
var $_errorNum=0;
/** @var string Internal variable to hold the database error message */
var $_errorMsg='';
/** @var string Internal variable to hold the prefix used on all database tables */
var $_table_prefix='';
function database( $host='localhost', $user, $pass, $db, $table_prefix='') {
// perform a number of fatality checks, then die gracefully
function_exists( 'mysql_connect' )
or die( 'FATAL ERROR: MySQL support not available.  Please check your configuration.' );
$link = mysql_connect( $host, $user, $pass )
or die( 'FATAL ERROR: Connection to database server failed.' );
mysql_select_db($db)
or die( "FATAL ERROR: Database not found. Operation failed with error: ".mysql_error());
$this->_table_prefix = $table_prefix;
}
function setQuery( $sql, $prefix='#__' ) {
$this->_sql = str_replace( $prefix, $this->_table_prefix, $sql );
}
/**
* @return string The current value of the internal SQL vairable
*/
function getQuery() {
return "<pre>$this->_sql</pre>";
}
/**
* Execute the query
* @return mixed A database resource if successful, FALSE if not.
*/
function query() {
$this->_errorNum = 0;
$this->_errorMsg = '';
$cur = mysql_query( $this->_sql );
if( !$cur ) {
$this->_errorNum = mysql_errno();
$this->_errorMsg = mysql_error()." SQL=$this->_sql";
return false;
}
return $cur;
}/**
* @return int The number of rows returned from the most recent query.
*/
function getNumRows( $res ) {
return mysql_num_rows( $res );
}/**
* This method loads the first field of the first row returned by the query.
*
* @return The value returned in the query or null if the query failed.
*/
function loadResult() {
if (!($cur = $this->query())) {
return null;
}
$ret = null;
if ($row = mysql_fetch_row( $cur )) {
$ret = $row[0];
}
mysql_free_result( $cur );
return $ret;
}
/**
* Load an array of single field results into an array
*/
function loadResultArray($numinarray = 0) {
if (!($cur = $this->query())) {
return null;
}
$array = array();
while ($row = mysql_fetch_row( $cur )) {
$array[] = $row[$numinarray];
}
mysql_free_result( $cur );
return $array;
}
/**
* This global function loads the first row of a query into an object
*
* If an object is passed to this function, the returned row is bound to the existing elements of <var>object</var>.
* If <var>object</var> has a value of null, then all of the returned query fields returned in the object.
* @param string The SQL query
* @param object The address of variable
*/
function loadObject( &$object ) {
if ($object != null) {
if (!($cur = $this->query())) {
return false;
}
if ($array = mysql_fetch_assoc( $cur )) {
mysql_free_result( $cur );
return true;
} else {
return false;
}
} else {
if ($cur = $this->query()) {
if ($object = mysql_fetch_object( $cur )) {
mysql_free_result( $cur );
return true;
} else {
$object = null;
return false;
}
} else {
return false;
}
}
}
/**
* Load a list of database objects
* @param string The field name of a primary key
* @return array If <var>key</var> is empty as sequential list of returned records.
* If <var>key</var> is not empty then the returned array is indexed by the value
* the database key.  Returns <var>null</var> if the query fails.
*/
function loadObjectList( $key='' ) {
if (!($cur = $this->query())) {
return null;
}
$array = array();
while ($row = mysql_fetch_object( $cur )) {
if ($key) {
$array[$row->$key] = $row;
} else {
$array[] = $row;
}
}
mysql_free_result( $cur );
return $array;
}
}

解决方案 »

  1.   

    目录输出
    <table width="98%" border="0" align="center" cellpadding="1" cellspacing="4">
     <?
    define( '_VALID_URL' , 1);
    require_once('include/common.php');
    require_once('include/database.php');
    require_once('siteman/inc/db_info.inc');
    require_once('siteman/inc/common.php');

    $database=new database($DB_URL,$DB_USER,$DB_USER_PASSWORD,$DB_NAME);
    //使用临时数组存储产品目录
    $sql="select TypeID,TypeName,PID,path,level,nochild FROM `tgoodstype` WHERE 1";
    $database->setquery($sql);
    $datalist=$database->loadobjectlist();
    DIRSort($datalist,0);
    $count = 0;
    foreach($dirarray as $dir){
    $count++;
    for($i=1,$temp='';$i < $dir->level;$i++){
    $temp .= '&nbsp;';
    }
    if(1 == $dir->nochild){
    echo " <tr style=\"display:none\" id=\"t$dir->PID{$dir->TypeID}\"> 
              <td> <table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"1\">                        
                  <tr><td nowrap>&nbsp;<a href=\"goodslist.php?category=$dir->TypeID\">$temp $dir->TypeName</a></td>
                  </tr></table></td>
            </tr>";
            }
            elseif(0 == $dir->PID){
             echo "<tr colspan=2> 
              <td style=\"cursor:hand\" onMouseOver=\"mOver(this)\" onMouseOut=\"mOut(this)\" onClick=\"shmenu($dir->TypeID)\"> 
                $temp <font face=\"Webdings\">4</font>$dir->TypeName</td>
            </tr>";
            }
            else{
             echo "<tr style=\"display:none\" colspan=2 id=\"t$dir->PID{$dir->TypeID}\"> 
              <td style=\"cursor:hand\" onMouseOver=\"mOver(this)\" onMouseOut=\"mOut(this)\" onClick=\"shmenu($dir->TypeID)\"> 
                $temp <font face=\"Webdings\">4</font>$dir->TypeName</td>
            </tr>";
            }
    }

    ?>
    </table>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function mOver(obj){
    obj.className = "msover";
    }function mOut(obj){
    obj.className = "msout";
    }function shmenu(pid){
    tmp = <?= $count?>;
    var objlist = new Array(tmp);
    <?
    $i=0;
    foreach($dirarray as $dir){
    ?>
    i = <?=$i?>;
    objlist[i] = new Array(2);
    objlist[i][0] = "<?= $dir->PID?>";
    objlist[i][1] = "<?= $dir->PID?><?= $dir->TypeID?>";
    <?
    $i++;
    }
    ?>
    var i = 0;
    while(i< <?= $count?>){
    if(objlist[i][0] == pid){
    obj = eval("t" + objlist[i][1]);
    stat = obj.style.display;
    if (stat == "none")
    obj.style.display = "";
    else
    obj.style.display = "none";
    }
    i++;
    }
    }
    //-->
    </SCRIPT>
    function DIRSort($array,$PID){
    Global $dirarray;
    foreach($array as $data){
    if($data->PID == $PID){
    $dirarray[]=$data;
    if($data->nochild !=1){
    DIRSort($array,$data->TypeID);
    }

    }

    }其他
    Function GetParam( &$arr, $name, $def=null ) {
    return isset( $arr[$name] ) ? $arr[$name] : $def;
    }Function SetError($error = NULL){
    if(isset($error) or $error="发现未知的错误"){
    echo "$error <br>";
    }
    }