Jump to content


Photo

Problem with mysq_fetch_* + classes


  • Please log in to reply
5 replies to this topic

#1 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 22 April 2006 - 01:41 AM

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...
$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
}

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:
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;
    }
}

Thanks :)

~ D Kuang

#2 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 22 April 2006 - 01:56 AM

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.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#3 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 22 April 2006 - 02:01 AM

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

#4 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 22 April 2006 - 02:02 AM

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.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#5 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 22 April 2006 - 02:16 AM

[!--quoteo(post=367399:date=Apr 21 2006, 07:02 PM:name=businessman332211)--][div class=\'quotetop\']QUOTE(businessman332211 @ Apr 21 2006, 07:02 PM) View Post[/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.
~ D Kuang

#6 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 22 April 2006 - 02:18 AM

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.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users