Page 1 of 1

php, class and database

Posted: Tue Oct 18, 2005 6:11 am
by h4mm3r
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi, 
i'm newbie here and I really don't know if this is the right section where post my problem.

Actually I have to connect to a mysql database to retrieve some contents. I've created to classes, DbManager and ResultSet. Everything seems to work well (i can connect to db and retrieve data without using classes) but when I try to use my DbManager object I get this error:

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in C:\Programmi\Apache Group\Apache2\htdocs\CMSserport\sql\ResultSet.php on line 68

Code: Select all

<?

/*
 * DbManager.php
 * Class DbManager
 * 
 * @version	18 Ottobre 2005
 * @author	Francesco Biacca
 * 
 * 
 */

include_once('dbConstants.php');
include_once('ResultSet.php');

class DbManager
{
	var $_db = null;
	var $_host = null;
	var $_dbuser = null;
	var $_dbpassword = null;
	var $_link = null;
	var $_result = null;
	var $qtype = null;
	var $numrows;
	
	function DbManager()
	{
		$this->_host = CMShost;
		$this->_dbuser = CMSdbuser;
		$this->_dbpassword = CMSdbpass;
		$this->_db = CMSdatabase;
	}
	
	function getDB()
	{
		return $this->_db;
	}
	
	function getUser()
	{
		return $this->_dbuser;	
	}
	
	function getPassword()
	{
		return $this->_dbpassword;
	}	
	
	function getHost()
	{
		return $this->_host;	
	}
	
	
	function getResultSet()
	{
		if($this->_result){
			return new ResultSet($this->_result);	
		}		
		else
			return null;
	} 
		
	function connect()
	{
		$this->_link = mysql_connect($this->_host, $this->_dbuser, $this->_dbpassword);
		
		if(!$this->_link)
		{
			die('Non riesco a collegarmi: ' . mysql_error());	
		}
		
		mysql_select_db($this->_db) or die ('Database non selezionabile');
	}
	
	function disconnect()
	{
		mysql_close($this->_link);
		mysql_free_result($this->_result);
	}

	function query($string)
	{	
		if( eregi("^delete", $string) || eregi("^insert", $sql) || eregi("^update", $sql) )
		{
			$qtype = 'm'; 
		}
		elseif( eregi("^select", $string) )
		{
			$qtype = 's';	
		}
		
		 $this->_result = mysql_query($string) or die('query errata: ' .mysql_error());
		
		if($qtype == 's')
		{
			if(mysql_affected_rows($this->_link) <= 0)
			{
				die('errore: ' . mysql_error());
				return false;
			}
			else
				return true;
		} 
		elseif($qtype  == 'm')
		{
			$this->numrows = mysql_num_rows($this->_result);
			if( $this->_numrows == 0 )
			{
				die('Query non valida: ' . mysql_error());
				return false;
			}
			else
				return true;
		}
		return true;
	}
	
	function selectQuery($strSQL)
	{
		$ok = $this->query($strSQL);
		
		if($ok)
		{
			$result = $this->getResultSet();
			$resultArray = $result->toArray();
			$result->clean();
			
			return $resultArray; 
		}
		else
		{
			echo 'some errors' . '<br/>';
			return false;
		}
	}	
}//class
?>

Code: Select all

<?
/*
 * ResultSet.php
 * class ResultSet
 * 
 * @version	18/10/2005
 * @author	Francesco Biacca
 * 
 */

class ResultSet
{
	var $_rslt;

	function ResultSet($resultId)
	{
		$this->_rslt = $resultID;
	}
	
	function numRows()
	{
		return (@mysql_num_rows($this->_rslt));	
	}
	
	function fetchObject()
	{
		return (@mysql_fetch_object($this->_rslt, MYSQL_ASSOC));	
	}
		
	function clean()
	{
		return (@mysql_free_result($this->_rslt));
	}
	
	function toArray()
	{
		while($row = mysql_fetch_object($this->_rslt))
		{
			$result[] = $row;	
		}
		
		return $result;
	}
}//class
?>

Code: Select all

<?
/*
 * testdb.php
 * 
 * @version	18/10/2005
 * @author	Francesco Biacca
 * 
 */
 
 include_once('sql/DbManager.php');
?>

<html>
<head>
	<title>Test::DB connection</title>
</head>

<body>
	<?	
		$db = new DbManager();
		$db->connect();
		
		$strSQL = "select * from linguaggio";
		$temp = $db->selectQuery($strSQL);
		
		echo '<h1>' .$result. '</h1>';
	?>
</body>
</html>
Hope to get help!
thx


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Tue Oct 18, 2005 6:33 am
by feyd
h4mm3r wrote:

Code: Select all

function ResultSet($resultId)
    {
        $this->_rslt = $resultID;
    }
is at fault.. I'll let you look at it for a bit and figure out why.. ;)

Posted: Tue Oct 18, 2005 6:37 am
by h4mm3r
I had understood that problem was there ... but I can't still figure out why ... :(
I mean: when I call new object ResultSet, I pass to it $this->_result, but inside ResultSet it seems that this value got lost

Posted: Tue Oct 18, 2005 6:41 am
by feyd
Observe the variable name in the argument list of the function. Now observe the variable name you assign to $this->_rslt.

If you're still having trouble, change the directive "error_reporting" to E_ALL (only) in your php.ini and restart your web server service.

Posted: Tue Oct 18, 2005 6:50 am
by h4mm3r
I can't believe!!! thx very much
(I mean, coding in the early morning is some times not so brilliant idea)

Posted: Tue Oct 18, 2005 6:52 am
by Charles256
and for future reference coding while drunk doesn't work either..left off $ and couldn't figure out why it wouldn't assign my variables properly...