Problems with mysql_fetch_array($IsResult,MYSQL_NUM)

Problems with mysql_fetch_array($IsResult,MYSQL_NUM)

am 28.05.2007 09:52:45 von patrick

Hi

I am trying to write a simple database class to encapsulate all the
database functions in one. Howerver I am having problems with
while($row = mysql_fetch_array($IsResult,MYSQL_NUM)) line it never
executes the loop. $IsResult is reeves no value.

What am I doing wrong

Thanks

pat


class CDatabase
{
// Database
var $misOpen = false;
var $mResult;

function getResults()
{
return $this->mResult;
}

function isOpen()
{
return $this->misOpen;
}

// constructer
function CDatabase($username,$password,$host,$databasename)
{
$db_connection = mysql_connect($host,$username,$password);
$this->misOpen = false;

if($db_connection)
{
$this->misOpen = true;
mysql_select_db($databasename);
}

echo mysql_error();
}

function __destruct()
{
$this->disconnect();
}

function disconnect()
{
if ($this->isOpen())
{
mysql_close();
}
}


function sqlQuery($query)
{
$numberofResults = 0;
$IsResult = mysql_query($query);

echo "IsResult= '".$IsResult."'
\n";

while($row = mysql_fetch_array($IsResult,MYSQL_NUM))
{
$this->mResult[] = $row;
$numberofResults++;
echo $numberofResults++;
echo "loop";
}

return $numberofResults;
}
};

?>

Re: Problems with mysql_fetch_array($IsResult,MYSQL_NUM)

am 28.05.2007 19:31:15 von Shion

Patrick wrote:
> Hi
>
> I am trying to write a simple database class to encapsulate all the
> database functions in one. Howerver I am having problems with
> while($row = mysql_fetch_array($IsResult,MYSQL_NUM)) line it never
> executes the loop. $IsResult is reeves no value.

Then the mysql_query() returned a false, you should check for that before you
start to loop, if it's false you should fetch the mysql_error().


> function sqlQuery($query)
> {
> $numberofResults = 0;
> $IsResult = mysql_query($query);
>
> echo "IsResult= '".$IsResult."'
\n";
>
> while($row = mysql_fetch_array($IsResult,MYSQL_NUM))

Why not use MYSQL_BOTH (this is the default, so you don't need to assign it to
mysql_fetch_array())? This way you can seach your mResult array with both the
column name and with the column number.

> {
> $this->mResult[] = $row;
> $numberofResults++;
> echo $numberofResults++;
> echo "loop";
> }
>
> return $numberofResults;

You don't have to count, as you have mysql_num_rows() which will give you the
amount of rows directly after you done the mysql_query()



--

//Aho