Jump to content


Photo

Can someone compare two functions for me --> see bottom entry


  • Please log in to reply
9 replies to this topic

#1 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 14 August 2006 - 10:32 AM

Hi all,

Having a problem with this function :

<?php

        function sess_read($key) {
                global $DEBUG, $SESS_LIFE;

                $result = mssql_query("SELECT value FROM sessions WHERE sesskey = '$key' AND expiry > " . time());

                if ($DEBUG) echo "sess_read: $statement <br>result: $result<br>";
                $row = $result->fetchRow();
                if ($row) {
                   return $row->value;
                }

                return false;
        }
?>

It returns this Error :

PHP Fatal error: Call to a member function fetchRow() on a non-object in C:\Inetpub\wwwroot\database\classes\lib.session_handler.php on line 33

Now the fetchRow() function it is on about lies in the PEAR DB Class which is shown below :

<?php

    /**
     * Fetch and return a row of data (it uses driver->fetchInto for that)
     * @param int $fetchmode  format of fetched row
     * @param int $rownum     the row number to fetch
     *
     * @return  array a row of data, NULL on no more rows or PEAR_Error on error
     */
    function fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null)
    {
        if ($fetchmode === DB_FETCHMODE_DEFAULT) {
            $fetchmode = $this->dbh->fetchmode;
        }
        if ($fetchmode === DB_FETCHMODE_OBJECT) {
            $fetchmode = DB_FETCHMODE_ASSOC;
            $object_class = $this->dbh->fetchmode_object_class;
        }
        if ($this->dbh->limit_from !== null) {
            if ($this->row_counter === null) {
                $this->row_counter = $this->dbh->limit_from;
                // For Interbase
                if ($this->dbh->features['limit'] == false) {
                    $i = 0;
                    while ($i++ < $this->dbh->limit_from) {
                        $this->dbh->fetchInto($this->result, $arr, $fetchmode);
                    }
                }
            }
            if ($this->row_counter >= (
                    $this->dbh->limit_from + $this->dbh->limit_count))
            {
                return null;
            }
            if ($this->dbh->features['limit'] == 'emulate') {
                $rownum = $this->row_counter;
            }

            $this->row_counter++;
        }
        $res = $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum);
        if ($res !== DB_OK) {
            return $res;
        }
        if (isset($object_class)) {
            // default mode specified in DB_common::fetchmode_object_class property
            if ($object_class == 'stdClass') {
                $ret = (object) $arr;
            } else {
                $ret =& new $object_class($arr);
            }
            return $ret;
        }
        return $arr;
    }

    /**
     * Fetch a row of data into an existing variable.
     *
     * @param  mixed $arr        reference to data containing the row
     * @param  int   $fetchmode  format of fetched row
     * @param  int   $rownum     the row number to fetch
     *
     * @return  mixed  DB_OK on success, NULL on no more rows or
     *                 a DB_Error object on error
     */
    function fetchInto(&$arr, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null)
    {
        if ($fetchmode === DB_FETCHMODE_DEFAULT) {
            $fetchmode = $this->dbh->fetchmode;
        }
        if ($fetchmode === DB_FETCHMODE_OBJECT) {
            $fetchmode = DB_FETCHMODE_ASSOC;
            $object_class = $this->dbh->fetchmode_object_class;
        }
        if ($this->dbh->limit_from !== null) {
            if ($this->row_counter === null) {
                $this->row_counter = $this->dbh->limit_from;
                // For Interbase
                if ($this->dbh->features['limit'] == false) {
                    $i = 0;
                    while ($i++ < $this->dbh->limit_from) {
                        $this->dbh->fetchInto($this->result, $arr, $fetchmode);
                    }
                }
            }
            if ($this->row_counter >= (
                    $this->dbh->limit_from + $this->dbh->limit_count))
            {
                return null;
            }
            if ($this->dbh->features['limit'] == 'emulate') {
                $rownum = $this->row_counter;
            }

            $this->row_counter++;
        }
        $res = $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum);
        if (($res === DB_OK) && isset($object_class)) {
            // default mode specified in DB_common::fetchmode_object_class property
            if ($object_class == 'stdClass') {
                $arr = (object) $arr;
            } else {
                $arr = new $object_class($arr);
            }
        }
        return $res;
    }

?>

So two things I ask, is that I don't really understand the error message.  And I'd also like to cut out the middle-man (PEAR), so how could I create the function to just use MsSql.

Thanks in advance

#2 trq

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

Posted 15 August 2006 - 09:19 AM

Your not using the PEAR DB class. Look at your code. You use mssql_query() which returns a result resource, not a PEAR DB object.

#3 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 15 August 2006 - 10:08 AM

Yeah, I changed that bit already, but then it sticks the result into fetchRow which is integrated into PEAR's DB.php, which is why I posted the functions.  See these Entries :

DB_FETCHMODE_DEFAULT
DB_FETCHMODE_OBJECT
DB_FETCHMODE_ASSOC

What are they all about?  And I want rid of that error, but don't really understand it.  Thanks

#4 trq

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

Posted 15 August 2006 - 01:34 PM

Yeah, I changed that bit already,


Post the relevent code then.

#5 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 15 August 2006 - 03:47 PM

I mean that within my lib.session_handler.php it had code within function sess_read() listed above that used PEAR, however I swapped it like so above to not use PEAR, now I'm left with that error and the extra stuff I don't know what it does.  Please help am fairly new to php and this is doing my head in.  Thanks

#6 trq

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

Posted 16 August 2006 - 01:29 PM

Have a look in the manual on how to use mssql_query. It is NOT an object, so $result->fetchRow() does not exist.

At its simplest, your code should be something like...

function sess_read($key) {
    global $DEBUG, $SESS_LIFE;

    $result = mssql_query("SELECT value FROM sessions WHERE sesskey = '$key' AND expiry > " . time());

    if ($DEBUG) echo "sess_read: $statement <br>result: $result<br>";
    if ($result) {
        $row = mssql_fetch_assoc($result);
        return $row['value'];
    }
    return false;
}


#7 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 16 August 2006 - 04:31 PM

Thanks Thorpe for your time, but I think I have been going about it wrong then, this was the original sess_read function that I changed to the one in the first msg :

<?php
function sess_read($key) {
                global $dbi, $DEBUG, $SESS_LIFE;

                $statement = "SELECT value FROM sessions WHERE " .
                       "sesskey = '$key' AND expiry > " . time();

                $result = $dbi->query($statement);

                if ($DEBUG) echo "sess_read: $statement <br>result: $result<br>";
                $row = $result->fetchRow();
                if ($row) {
                   return $row->value;
                }

                return false;
        }
?>

Which passes the $statement to function query() in a file called class.DBI.php and works like so :

<?php
      function query($statement)
      {

         $result =  $this->dbh->query($statement);

         if (DB::isError($result))
         {

            $this->setError($result->getMessage());

            return null;

         } else {

            return $result;
         }

      }
?>

So does that mean that result was an object and what I had edited just brought back a true of false?

I thaught that this file was just being scanned to see if there was a session already open before moving on to the login page, could you see from what I have posted if it is doing more than that.

Thanks in Advance

#8 czambran

czambran
  • Staff Alumni
  • Advanced Member
  • 52 posts
  • LocationFairfield,IA

Posted 16 August 2006 - 04:46 PM

make sure the following line is returning an object:
$result = $dbi->query($statement);
Chris
Using undefined variables in PHP is not an error, just sloppy coding practice. PHP5 Power Programming BOOK
--------------------------------------------------------------------------
PHP F.A.Q.
Linux User #394154

#9 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 17 August 2006 - 09:12 AM

OK Beginning to understand that objects are a bit like arrays.  I thaught that I was getting an  array with the value from my sessions table where that particular user was logged in.  Then passing it to the query function, (which I thaught did the DB Abstraction bit) and returned the result back to sess_read, which then went off to get the row that value is found in and return that value or return FALSE.

Can anybody confirm if that is correct from the bits of code posted and tell me if Thorpe's version is the same as the code I posted directly below his, but without the abstraction bit?

Many Thanks, I think I'm beggining to get somewhere on this problem.

#10 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 18 August 2006 - 01:17 PM

Right I have worked out that what was entered was wrong as the mssql_query returns a TRUE or FALSE value, and have now swapped it for mssql_result and have edited it as follows :

<?php

        function sess_read($key) {
                global $DEBUG, $SESS_LIFE;

				$statement = "SELECT value FROM sessions WHERE sesskey = '$key' AND expiry > " . time();
                $result = mssql_result("SELECT value FROM sessions WHERE sesskey = '$key' AND expiry > " . time());

                if ($DEBUG) echo "sess_read: $statement <br>result: $result<br>";
                
				if ($result) {
					$row = mssql_fetch_assoc($result);
					return $row['value'];
				}
				return false;
        }
?>

Now will this match the code supplied in the two boxes above and miss out the abstraction layer?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users