Jump to content


Photo

Database Class


  • Please log in to reply
2 replies to this topic

#1 yanaho

yanaho
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 09 October 2006 - 10:55 PM

Hi all! this is my first post so please go easy.

What I'm doing is simple.  But for the life of me I can't do it.  I hope someone can help.
I have a class called database, in this class is a function which i want to return a 2 dimensional array, filled with rows from my database table.  I want to call this function from another page and have the html table populated with the information. 
Why is this so hard you ask?  well I've spent hours looking on the net and nobody seems to call such a function.  All examples are coded on the page where the html table is being displayed, which i find very messy.

I'm guessing the function would look something like:

function getShows($date){
      $q = "SELECT * FROM ".TBL_SHOWS." WHERE date = '$date'";
      $result = mysql_query($q, $this->connection);
      /* Error occurred */
      if(!$result || (mysql_numrows($result) < 1)){
        return NULL;
      }
      /* Return result array */
      $dbassoc = mysql_fetch_assoc($result);
      return $dbassoc;
  }


and I'm guessing the code to display would look something like

$req_shows = $database->getShows(date);
foreach($req_shows AS $id=>$info)
{
  echo '<tr><td>'.$info['type'].'</td>
    <td>'.$info['time'].'</td>
    <td>'.$info['artistid'].'</td>
    <td>'.$info['genre'].'</td>
    <td>'.$info['location'].'</td></tr>';
}

but this doesnt work along with a thousand other things i have tried as my knowledge of php is kinda weak.  Please if you can find the time I'm sure you must have all written something like this at some time.  I guess the biggest problem is I dont know what the function above is actually returning. 1 row or all rows, if all rows then I guess step one is complete, if only 1 row how can i make the function return multiple, or am I wasting my time having it return anything and just write the script on the page displaying the info(which is easy enough as there is code everywhere showing how) Once I have this figured I'm all set. Thanks in advance

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 09 October 2006 - 11:15 PM

To have your function return multiple (all) rows, you need to use a while loop. eg;

function getShows($date){
      $q = "SELECT * FROM ".TBL_SHOWS." WHERE date = '$date'";
      $result = mysql_query($q, $this->connection);
      /* Error occurred */
      if(!$result || (mysql_num_rows($result) < 1)){
         return false;
      }
      /* Return result array */
      while ($dbassoc = mysql_fetch_assoc($result)) {
          $row[] = $dbassoc;
      }
      return $row;
   }

I also fixed a spelling error at mysql_num_rows, and made your function return false on failure. Now you could use it like...

if ($req_shows = $database->getShows(date)) {
  foreach($req_shows AS $info)
  {
    echo '<tr><td>'.$info['type'].'</td>
      <td>'.$info['time'].'</td>
      <td>'.$info['artistid'].'</td>
      <td>'.$info['genre'].'</td>
      <td>'.$info['location'].'</td></tr>';
  }
}


#3 yanaho

yanaho
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 09 October 2006 - 11:31 PM

I love you guys,thanks thorpe may your deeds not go unrewarded.  =)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users