problems with mysql_fetch_array($IsResult,MYSQL_NUM)

This is a discussion on problems with mysql_fetch_array($IsResult,MYSQL_NUM) within the PHP Language forums, part of the PHP Programming Forums category; Hi I am trying to write a simple database class to encapsulate all the database functions in one. Howerver I ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-28-2007
Patrick
 
Posts: n/a
Default problems with mysql_fetch_array($IsResult,MYSQL_NUM)

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

<?php

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."' <br/>\n";

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

return $numberofResults;
}

};

?>

Reply With Quote
  #2 (permalink)  
Old 05-28-2007
purcaholic
 
Posts: n/a
Default Re: problems with mysql_fetch_array($IsResult,MYSQL_NUM)

On 28 Mai, 09:55, Patrick <hornep...@gmail.com> 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.
>
> What am I doing wrong
>
> Thanks
>
> pat
>
> <?php
>
> 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."' <br/>\n";
>
> while($row = mysql_fetch_array($IsResult,MYSQL_NUM))
> {
> $this->mResult[] = $row;
> $numberofResults++;
> echo $numberofResults++;
> echo "loop";
> }
>
> return $numberofResults;
> }
>
> };
>
> ?>



Your class seems to work (but i haven't tested it).

I suppose the passed query is invalid. And an try{}catch{} block to
your sqlQuery function and throw an error, if mysql_query fails.

Tip:
You increment the $numberofResults counter twice ("$numberofResults++"
and "echo $numberofResults++"), once is enough.
Use __construct() instead of CDatabase().

purcaholic

Reply With Quote
  #3 (permalink)  
Old 05-28-2007
Ravi
 
Posts: n/a
Default Re: problems with mysql_fetch_array($IsResult,MYSQL_NUM)

Your class is working fine. Please check weather the query is
executing or not. Check it using
mysql_query($qry) or die(mysql_error()); this code

Reply With Quote
  #4 (permalink)  
Old 05-28-2007
Schraalhans Keukenmeester
 
Posts: n/a
Default Re: problems with mysql_fetch_array($IsResult,MYSQL_NUM)

At Mon, 28 May 2007 00:55:35 -0700, Patrick let h(is|er) monkeys type:

> 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
>
> <?php
>
> 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."' <br/>\n";
>
> while($row = mysql_fetch_array($IsResult,MYSQL_NUM))
> {
> $this->mResult[] = $row;
> $numberofResults++;
> echo $numberofResults++;
> echo "loop";
> }
>
> return $numberofResults;
> }
>
> };
>
> ?>

Contrary to other comments' suggestion your class does not work fine yet.
But you knew that, or you hadn't come here ;-)
There's a few things worth paying further attention to:

1. The reason why your query does not execute.
You open a connection and store the result (resource or FALSE) in a local
variable. As soon as the constructor exits it goes out of scope and the
resource is gone.

Fix: change $db_connection to $this->db_connection and declare it as a
class variable.

2. Error checking consistence:
You check for a succesful mysql_connect() but don't check the result of
the mysql_select_db()

Fix: do a second result check after the mysql_select_db and postpone the
$misOpen = true assignment till after the second check.
You could combine the testslike so:

function CDatabase ($h, $u, $p, $db) {
$this->misOpen =
(($this->db_connection = mysql_connect($h,$u,$p)) &&
mysql_select_db($db))
// mysql_select_db only executes if mysql_connect returns true
// to suppress mysql warnings/errors, prepend the functions with '@'
}

Looking at the use of keyword 'var' in your script I assume you use PHP4.x
If so, ignore purcaholic's remark about using __construct().
If not (and you _are_ using PHP5) you ought to declare your vars without
the var keyword and since they should be hidden from the class user,
declare them private.

In the sqlQuery() function, you collect the result and print it's value (I
assume this is debugging extra code) but don't test the value before
you start fetching rows from the db. Besides, you don't know if there are
any rows to be fetched.

You might rewrite:
if ($result = mysql_query($query))
{
if (($numrows = mysql_numrows($result)) > 0)
{
while ($this->mresult[] = mysql_fetch_array ($result));
// if you want to display the counter and 'loop', remove the ; from
// previous line, and write your code here between { and }
}
return $numrows;
}

The try/catch block suggestion is nice, but I'd leave that for a later
stage when you feel a little more comfortable around PHP, imho. Or, if
you've become curious about it, study it on a separate trail and only
implement it in practical code when you have a good grasp of how, when and
why to use it.

And you know (of course?!) there are ready-to-use db classes and
extensions available ? PDO comes standard with PHP I believe, and PEAR has
a good universal db extension.

Lastly, I probably made some typos here and there, hope you manage to use
the suggested snippets nonetheless.

Have a nice day, and good luck programming!

--
Schraalhans Keukenmeester - schraalhans@the.spamtrapexample.nl
[Remove the lowercase part of Spamtrap to send me a message]

"strcmp('apples','oranges') is -1"

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:37 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0