Jump to content

Archived

This topic is now archived and is closed to further replies.

poirot

Problem with mysq_fetch_* + classes

Recommended Posts

OK, I here it goes. I have a class to handle the MySQL database, it's something like the class at the bottom. I thought it worked, until...
[code]$sql = new Database; // Initializing class
$sql->connect(); // Connects to the database.

$sql->result = $sql->query("SELECT * FROM `table`"); // Sample Query

while ($row = $sql->fetch()) {
   echo $row['field1'] . $row['field3']; // <<< The problem
}[/code]

But for some reason, it wll only echo the first result! Even when the query returns more than 1 row, it will echo/display the first one!
Any Ideas?

Class:
[code]class Database
{

   var $result // This is the result resource;

    function query($query)
    {
       $return = mysql_query($query) or $this->error_handle(mysql_error(), mysql_errno()); // basic error handling function
       return $return;
    }

    // Shorthand fetching; fetch_assoc as default
    function fetch($mode='assoc')
    {
       switch ($mode) {
          case 'array':
          $ret = mysql_fetch_array($this->result);
          break;
          
          case 'row':
          $ret = mysql_fetch_row($this->result);
          break;
          
          default:
          $ret = mysql_fetch_assoc($this->result);
       }

       return $ret;
    }
}[/code]

Thanks :)

Share this post


Link to post
Share on other sites
maybe that . in the middle of the rows is supposed to be something else.
or maybe something could be wrong with the database, meaning maybe the row in the database isn't connecting right.
just some guesses as I don't know databasing quite yet.
[a href=\"http://www.php-scripts.com/php_diary/070700.php3\" target=\"_blank\"]http://www.php-scripts.com/php_diary/070700.php3[/a]
maybe this can help some, you know more about php and mysql than me, I don't know what to make of it, but I looked at your problem and ran through google, maybe that will help some.

Share this post


Link to post
Share on other sites
Oh no, if I do it (same script, same query, same database) using the "classic" method:
[code]$result = mysql_query("SELECT * FROM `table`");
while ($row = mysql_fetch_assoc($result)) {
   echo $row['field1'] . $row['field3']; // <<< The problem
}[/code]
It just works fine.

Share this post


Link to post
Share on other sites
why don't you use the classic method this time, maybe one of the things you are trying to do with the other method you are not used to and maybe doing it wrong, is there a reason you don't want to go with your normal way.

Share this post


Link to post
Share on other sites
[!--quoteo(post=367399:date=Apr 21 2006, 07:02 PM:name=businessman332211)--][div class=\'quotetop\']QUOTE(businessman332211 @ Apr 21 2006, 07:02 PM) [snapback]367399[/snapback][/div][div class=\'quotemain\'][!--quotec--]
why don't you use the classic method this time, maybe one of the things you are trying to do with the other method you are not used to and maybe doing it wrong, is there a reason you don't want to go with your normal way.
[/quote]
I prefer the class because OOP makes things more organized.
Besides, I have some debug and error handling functions that I don't want to call every time I call a query.

Share this post


Link to post
Share on other sites
This might not help you much but if you are using php5 I know of a site that has the completely new system I read that the php object oriented programming entire system was redesigned here is a link, but I cna't help other than that, because I stopped studying tonight and haven't gotten into oop yet, I was starting that actually monday I just told my host to switch over my server to php5 it takes them 4 hours.
[a href=\"http://www.hudzilla.org/phpbook/read.php/6_0_0\" target=\"_blank\"]http://www.hudzilla.org/phpbook/read.php/6_0_0[/a]
see if you can find something there real quick, theres a section there on mysql databases too in case if that one I gave you doens't help.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.