Jump to content

Archived

This topic is now archived and is closed to further replies.

scottybwoy

Still can't get this query to work *Solved*

Recommended Posts

I am still having trouble with this query below : (Please Help!!!)

[code]
<?php

require_once('constants.php');

$DEBUG = 0;

$SESS_LIFE = get_cfg_var("session.gc_maxlifetime");

        function sess_open($save_path, $session_name) {
          return true;
        }

        function sess_close() {
          return true;
        }

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

$statement = "SELECT * FROM sessions WHERE sesskey = '$key' AND expiry > " . time();
                $result = mssql_query($statement) or die ('Query failed.');  // This is Line 21

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

Also posted everything above it to show how lib.session_handler.php is being run.
The Error I am getting is this one here :

Query failed.PHP Warning: mssql_query() [function.mssql-query]: message: Invalid object name 'sessions'. (severity 16) in C:\Inetpub\wwwroot\database\classes\lib.session_handler.php on line 21 PHP Warning: mssql_query() [function.mssql-query]: Query failed in C:\Inetpub\wwwroot\database\classes\lib.session_handler.php on line 21

I have tried a number of things and you can find more details by [url=http://www.phpfreaks.com/forums/index.php/topic,105051.0.html]going here[/url]
I have been on this problem for over a week now and it is doing my head in  ???

As you can find from the previous post that it is now using one connection as shown here :

[code]
<?php

function connect()
      {

        // connect to the database

mssql_connect($INTRANET_DB_URL);
$this->connected == TRUE or die;
  $this->connected == FALSE;
mssql_select_db($AUTH_DB_TBL);
$this->connected == TRUE or die;
$this->connected == FALSE;
  }

?>
[/code]

As found in file class.PHPApplication.php
These constants are defined here in config.inc.php :

[code]
<?php

    $AUTH_DB_USERNAME     = 'user';
    $AUTH_DB_PASSWD       = 'pass';
    $AUTH_DB_HOST = 'localhost';
    $AUTH_DB_NAME = 'mri_sql';
    $AUTH_DB_TBL                  = 'users';

$INTRANET_DB_URL = '"' . $AUTH_DB_HOST . '"' . ',' . '"' . $AUTH_DB_USERNAME . '"' . ',' . '"' . $AUTH_DB_PASSWD . '"';
$AUTH_DB_URL        = $INTRANET_DB_URL;
$USER_DB_URL        = $INTRANET_DB_URL;
$APP_DB_URL = $INTRANET_DB_URL;

// some other code

//  require_once 'DB.php';
 
  require_once 'constants.php';
  require_once $APPLICATION_CLASS;
  require_once $ERROR_HANDLER_CLASS;
  require_once $AUTHENTICATION_CLASS;
//  require_once $DBI_CLASS;
//  require_once $THEME_CLASS;
//  require_once $THEME_TEMPLATE_CLASS;
  require_once $USER_CLASS;
  require_once $TEMPLATE_CLASS;

?>
[/code]

This config file is called by the Application config files.  In this case login.conf.php.  Which is called by login.php, the Application class that extends class.PHPApplication.php, the backbone of my project which connects the database as seen above.

I hope there is enough info here and on the [url=http://www.phpfreaks.com/forums/index.php/topic,105051.0.html]other page[/url] linked to help you help me solve this, I can't move on untill this is sorted. Thanks

Share this post


Link to post
Share on other sites
try this

[code]$statement = "SELECT * FROM sessions WHERE sesskey = '$key' AND expiry > '"time()"'";[/code]

Share this post


Link to post
Share on other sites
Yeah I did try that already it makes no difference, however I did :
[code]
$statement = "SELECT * FROM sessions WHERE sesskey = '$key' AND expiry > '" . time() . "'";
[/code]

Which is what I think you meant

Share this post


Link to post
Share on other sites
you need to create the connection before using mysql_query();

change your open function to:[code]<?php
function sess_open($save_path, $session_name) {
    global $SESS_LINK;
    if ($SESS_LINK = mysql_connect('host', 'user', 'pass')) {
        return mysql_select_db('databasethatcontainsthesessiontable');
    } else {
        return false;
    }
}
[/code]

You'll also need to update you other functions to specify the global $SESS_LINK resource in all mysql_query() functions.

Share this post


Link to post
Share on other sites
Ok,  Now this is my lib.session_handler.php
[code]
<?php

require_once('constants.php');

$DEBUG = 0;

$SESS_LIFE = get_cfg_var("session.gc_maxlifetime");

        function sess_open($save_path, $session_name) {
          global $SESS_LINK;
  if ($SESS_LINK = mssql_connect('localhost', 'user', 'pass')) {
  return mssql_select_db('mri_sql');
  } else {
  return false;
  }
        }

        function sess_close() {
          return true;
        }

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

$statement = "SELECT * FROM sessions WHERE sesskey = '$key' AND expiry > '" . time() . "'";
                $result = mssql_query($statement);
echo "<br />" . msql_error() . "<br />\n";

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

        function sess_write($key, $val) {
                global $SESS_LIFE, $SESS_LINK;

                $expiry = time() + $SESS_LIFE;
                $value = addslashes($val);

                $statement = "INSERT INTO sessions VALUES ('$key', $expiry, '$value')";
                mssql_query($statement) or die ('Query failed.'); // this is line 45

                if ($DEBUG) echo "sess_write: $statement <br>result: $result<br>";

                if (! $result) {
                        $statement = "UPDATE sessions SET expiry = $expiry, value = '$value' " .
                              "WHERE sesskey = '$key' AND expiry > " . time();
                        mssql_query($statement) or die ('Query failed.');
                }

                return $result;
        }

        function sess_destroy($key) {

global $SESS_LINK;
                $statement = "DELETE FROM sessions WHERE sesskey = '$key'";
                $result = mssql_query($statement);
                if ($DEBUG) echo "sess_destroy: $statement <br>result: $result<br>";

                return $result;
        }

        function sess_gc($maxlifetime) {

                global $SESS_LINK;
$statement = "DELETE FROM sessions WHERE expiry < " . time();
                $qid = mssql_query($statement);
                if ($DEBUG) echo "sess_gc: $statement <br>result: $result<br>";

                return 1;
        }

        session_set_save_handler(
                "sess_open",
                "sess_close",
                "sess_read",
                "sess_write",
                "sess_destroy",
                "sess_gc");
?>
[/code]

Now it gets past sess_read and displays the same error for line 45 sess_write function :

Query failed.PHP Warning: mssql_query() [function.mssql-query]: message: Invalid object name 'sessions'. (severity 16) in C:\Inetpub\wwwroot\database\classes\lib.session_handler.php on line 45 PHP Warning: mssql_query() [function.mssql-query]: Query failed in C:\Inetpub\wwwroot\database\classes\lib.session_handler.php on line 45

Why would this be if it worked on the one above?  Thanks for your help

Share this post


Link to post
Share on other sites
sorry, only just spotted you are using MS SQL, not MySQL. Change it to mssql_connect() (which you have done,) and use the $SESS_LINK in all your mssql_query() functions like so:
[code]<?php

$result = mssql_query($sql, $SESS_LINK);

?>[/code]

Share this post


Link to post
Share on other sites
Cheers Jenk thats great, I think it's sorted now.  Will I ba able to specify $SESS_LINK for other queries to connect to that database, outside of this file?

Share this post


Link to post
Share on other sites
aslong as it's within the same namespace, yes. But it would probably be a better idea to keep the $SESS_LINK connection for session data only, and not mix it in with the other connections.

Share this post


Link to post
Share on other sites
Jenk I only have one connection, but what do you mean by namespace?  My directory structure is like so :

|
+-Database
|      +-Classes
|      |      | // lib.session_handler.php is in here
|      |      | // All my classes
|      +-httpd
|      |    +-images
|      |    +-index.php
|      |    +-styles.css
|      +-library
|      |    +-PEAR
|      |    +-php
|      +-scripts
|      |    +-login
|      |    +-home
|      |    +-customers
|      |    +- // You get the idea
|      +-Templates
|      |    | // All my templates

Share this post


Link to post
Share on other sites
Ignore the namespace part for now - but the reason why I suggest keeping a separate resource for session data from application data is because if you use a different DB for sessions from Application you may have issues with incorrect table names etc.

Share this post


Link to post
Share on other sites

×

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.