Code: Select all
andCode: 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 68Code: 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>thx
feyd | Please use
Code: Select all
andCode: 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]