How to Use this Class?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adamjblakey
    New Member
    • Jan 2008
    • 133

    How to Use this Class?

    Hi,

    I have a class that i am using and want to know how to echo a result from this.

    This is a class
    [PHP]

    class database {


    function selects($select ) {
    $this->result = mysql_query($se lect,$this->connection);
    $this->row = mysql_fetch_ass oc($select);
    }

    function getRes() {
    while ($res = mysql_fetch_arr ay($this->result)) {
    $this->reser[] = $res;
    }
    return $this->reser;
    }

    }
    [/PHP]

    Here is the code

    [PHP]$current = new database;
    $current->selects("SELEC T * FROM users WHERE id = '$_COOKIE[id]'");
    $cresults = $current->getRes();
    // assign your db results to the template
    $smarty->assign('cresul ts', $cresults);
    [/PHP]

    I though i would be able to do something like:

    print $cresults['username'];

    But this does not work.

    Any Ideas?

    Cheers,
    Adam
  • Amzul
    New Member
    • Oct 2007
    • 130

    #2
    Originally posted by adamjblakey
    Hi,

    I have a class that i am using and want to know how to echo a result from this.

    This is a class
    [PHP]

    class database {


    function selects($select ) {
    $this->result = mysql_query($se lect,$this->connection);
    $this->row = mysql_fetch_ass oc($select);
    }

    function getRes() {
    while ($res = mysql_fetch_arr ay($this->result)) {
    $this->reser[] = $res;
    }
    return $this->reser;
    }

    }
    [/PHP]

    Here is the code

    [PHP]$current = new database;
    $current->selects("SELEC T * FROM users WHERE id = '$_COOKIE[id]'");
    $cresults = $current->getRes();
    // assign your db results to the template
    $smarty->assign('cresul ts', $cresults);
    [/PHP]

    I though i would be able to do something like:

    print $cresults['username'];

    But this does not work.

    Any Ideas?

    Cheers,
    Adam
    try to put echo in your function

    [CODE=php]function getRes() {
    $i=-1;
    while ($res = mysql_fetch_arr ay($this->result)) {
    $i++;
    $this->reser[] = $res;
    echo $this->reser[$i];
    }
    return $this->reser;
    }[/CODE]

    not tested but i think it will work

    Comment

    • adamjblakey
      New Member
      • Jan 2008
      • 133

      #3
      I have just added this but does not seem to work.

      Any ideas?

      Comment

      • adamjblakey
        New Member
        • Jan 2008
        • 133

        #4
        Anyone else have something i can try for this?

        Comment

        • nathj
          Recognized Expert Contributor
          • May 2007
          • 937

          #5
          Originally posted by adamjblakey
          Anyone else have something i can try for this?
          Hi,

          I have a data class also and the function that executes the query returns an associative array:
          [php]
          function queryGetData($p lSetCount, $pcQuery)
          {
          // ensure there is a connection
          $this->ensureConnecti on();
          // run the supplied query
          $result = mysql_query($pc Query, $this->database_lin k) or die("Error: ". mysql_error());
          $laReturnArray = array();
          $i=0;
          while ($row = mysql_fetch_arr ay($result, MYSQL_BOTH))
          {
          if ($row)
          {
          $laReturnArray[$i++]=$row;
          }
          }
          if($plSetCount)
          {
          $this->nRecentCount = mysql_num_rows( $result);
          }
          mysql_free_resu lt($result);
          return $laReturnArray;
          }
          [/php]
          Don't worry about the ensure connection stuff the important part for you is that it takes a select statement as the second parameter (the first para is a logical that indicates if you want a count on the object set), executes this select and transforms the results into an associative array. This array is returned to the calling code enabling the following:
          [php]
          // assume dataobject exists
          $lcQuery = "select * from table" ;
          $laResults = $loDatObject->queryGetData(f alse, $lcQuery) ;

          if($laResults)
          {
          foreach($laResu lts as $laRow)
          {
          echo $laRow['fieldname'];
          }
          // alternatlively you could replace the foreach with print_r($laResu lts) to get the array printed to screen.
          }
          else
          {
          // no results
          echo '<p> no results returned</p>';
          }
          [/php]

          I do this quite a lot and I got the basis for my existing code from here

          I hope this helps
          nathj

          Comment

          Working...