Jump to content

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


scottybwoy

Recommended Posts

Hi all,

Having a problem with this function :

[code]
<?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;
        }
?>
[/code]

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 :

[code]
<?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;
    }

?>
[/code]

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

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

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

Have a look in the manual on how to use [url=http://php.net/mssql_query]mssql_query[/url]. It is NOT an object, so $result->fetchRow() does not exist.

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

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

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 :

[code]
<?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;
        }
?>
[/code]

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

[code]
<?php
      function query($statement)
      {

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

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

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

            return null;

        } else {

            return $result;
        }

      }
?>
[/code]

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

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

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 :

[code]
<?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;
        }
?>
[/code]

Now will this match the code supplied in the two boxes above and miss out the abstraction layer?
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.