Jump to content

Database Class


yanaho

Recommended Posts

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
Link to comment
Share on other sites

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

[code=php:0]
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;
  }
[/code]

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

[code=php:0]
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>';
  }
}
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.