PDA

View Full Version : from outside get Var value inside class



OKtrust
December 23rd, 2003, 06:02 AM
Hi all,
I'm practicing (hands on :-) 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 \n";
}


Please show me, thanks men.

Jubba
December 23rd, 2003, 11:42 AM
The same way you perform the internal function...



$db->result

OKtrust
December 23rd, 2003, 11:58 AM
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; :-((

Jubba
December 23rd, 2003, 12:16 PM
I'm sorry I was looking at your code wrong... since the internal function is returning the variable you should just write:



$db->showadmin();

OKtrust
December 23rd, 2003, 12:29 PM
like you wrote, It can't get query to xecute ($showadminuser = "select adminid, username from newsadmin";)
i'm not sure that i'm right :D
but I want to use class to connect, get data from SQL. If you don't mind, please make me a new one ?

λ
December 23rd, 2003, 02:02 PM
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.