From outside get Var value inside class

Hi all,
I’m practicing (hands on :slight_smile: class in PHP and I have problem with this code:


Class DBSQL
{  
	function showadmin($sql="")
	   {
	   $conn = $this->CONN;
	   $result = mysql_query($sql,$conn); 
	   return $result;
	   }
}

$db = new DBSQL($DBName);
$showadminuser = "select adminid, username from
newsadmin";
$db->showadmin($showadminuser);

How can I do to get $result to continuous writing these line:


while ($row = mysql_fetch_array($result)){
	$adminid=$row['adminid']; 
	$username=$row['username']; 	print "$username 
";
	}

Please show me, thanks men.

The same way you perform the internal function…


$db->result

sorry, It still … doesnt work
I think you missed one thing, is it $ ?
I write


$result = $db ->result;
print $sesult;

but it didn’t display anny thing. even I wrote $result = $db ->result; :-((

I’m sorry I was looking at your code wrong… since the internal function is returning the variable you should just write:


$db->showadmin();

like you wrote, It can’t get query to xecute ($showadminuser = “select adminid, username from newsadmin”:wink:
i’m not sure that i’m right :smiley:
but I want to use class to connect, get data from SQL. If you don’t mind, please make me a new one ?

I happen to be writing a class for a lightweight blog.

here is the code I’m using:


<?php 
class  MainBlog {
	var $_mysqlConn;
	function  MainBlog($uName, $pass, $db) {
		$this->_mysqlConn = mysql_pconnect("localhost", $uName, $pass);
		mysql_select_db($db, $this->_mysqlConn);
	}
	/**
	* @return array
	* @desc Get the top ten entries.
	*/
	function getEntries(){
		$rows = array();
		$query = mysql_query("SELECT * FROM entries LIMIT 10", $this->_mysqlConn);
		if(!query) print mysql_error($this->_mysqlConn);
		while ($row = mysql_fetch_array($query)){
			array_push($rows, $row);
		}
		return $rows;
	}
	/**
	* @return array
	* @param number $entryID
	* @desc Get an entry. Returns an array of fields of the entry.
	*/
	function getEntry($entryID){
		$id = (int) $id;
		$query = mysql_query("SELECT * FROM entries WHERE 'id' = '$id' LIMIT 1", $this->_mysqlConn);
		return mysql_fetch_array($query);
	}
	function customQuery($query) {
		$return = array();
		$execQuery = mysql_query($query, $this->_mysqlConn);
		while($row = mysql_fetch_array($execQuery)){
			array_push($return, $row);
		}
		return $return;
		
	}
			
}
?>

in PHP 5, you’ll be able to use mysql_connect, as PHP5 lets you run a function when the class is destroyed.