Jump to content

Archived

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

scottybwoy

Query causing error

Recommended Posts

Since trying to get rid of Database abstraction from a tutorial project, that I have used as my basis.  I get this error :

PHP Warning: Wrong parameter count for mssql_result() in C:\Inetpub\wwwroot\database\classes\lib.session_handler.php on line 31 PHP Warning: Wrong parameter count for mssql_result() in C:\Inetpub\wwwroot\database\classes\lib.session_handler.php on line 49 PHP Warning: Wrong parameter count for mssql_result() in C:\Inetpub\wwwroot\database\classes\lib.session_handler.php on line 56

Heres the first block of code, as it's the same error for each function I won't post the others.

[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()); //this is line 31

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

?>
[/code]

It looks like a simple error but I don't know what a perameter count is and it looks fine to me.
Thanks in advance

Share this post


Link to post
Share on other sites
I'll tell you what 'wrong parameter count' means: It means you're trying to pass to little or too many arguments to functions.

[quote=THE MANUAL]
string mssql_result ( resource result, int row, mixed field )

mssql_result() returns the contents of one cell from a [b]MS SQL[/b] result set. The field argument can be the field's offset, the field's name or the field's table dot field's name (tablename.fieldname). If the column name has been aliased ('select foo as bar from...'), it uses the alias instead of the column name. [/quote]

None of the arguments are in square brackets, meaning all of them are required!

Plus, you don't even use mssql_result() to pass a query to MS SQL, use mssql_query()!!!

Read up on [url=http://nl3.php.net/manual/en/ref.mssql.php]MS SQL functions in THE MANUAL[/url].

Share this post


Link to post
Share on other sites
Yes your right, I had just worked it out, I had read the MS SQL functions in THE MANUAL but get a little confused as to what some words mean, I'm new to programming and do it out of funtion rather than fashion, so please forgive my ignorance.  I worked it out just by playing around.

arguments - what are they in accordance to my code above please?

I thaught that mssql_result() queried the database and then returned the single value requested and that mssql_query returned either TRUE or FALSE, which is why I hadn't used it.  Then I realised I was wrong, mssql_query() only returns FALSE if it fails.

How does mssql_query() present itself to $result in my example, would it be an associative array? i.e $result[value] = whatever_value ?

In the example above there would only be one value as $key is a unique session key so would not break "return $row['value'];"

Is this correct understanding?

But now I get these Errors:

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

from this :

[code]
<?php

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

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

                $statement = "INSERT INTO sessTbl VALUES ('$key', $expiry, '$value')";
                $result = mssql_query($statement);  // this is line 49

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

                if (! $result) {
                        $statement = "UPDATE sessTbl SET expiry = $expiry, value = '$value' " .
                              "WHERE sesskey = '$key' AND expiry > " . time();
                        $result = mssql_query($statement); // this is line 56
                }

                return $result;
        }

?>
[/code]

Assuming that the first posted code has been amended in the same fashion.  I thaught it read the table first to see if it needed to write a session, if that is the case it would know that the object sessTbl does exist.  So why would it fail the query?

Share this post


Link to post
Share on other sites
'Object' in this case, refers to a table or resultset.
Why are you using 'sessTbl', when before you referred to the table as 'sessions'?

From [b]THE MANUAL[/b]  :P:
[quote]mixed mssql_query ( string query [, resource link_identifier [, int batch_size]] )

[b]Returns: A MS SQL result resource on success, TRUE if no rows were returned, or FALSE on error. [/b][/quote]

From this result resource you fetch associative arrays, with mssql_fetch_assoc().

Share this post


Link to post
Share on other sites
I changed sessions to sessTbl as the database and table were both called sessions, and I thaught it may be selecting the database instaed of the table so I changed the table name and ammended the code to clarify that that was not where the problem laid.  I done this as I gathered that is what it mean by Object.

do I need any of these : [, resource link_identifier [, int batch_size]] , for my query to work?
If so what do they mean, how do I find out what goes in there, again excuse my ignorance, I apreciate your help.

Share this post


Link to post
Share on other sites
[quote author=scottybwoy link=topic=105051.msg420659#msg420659 date=1156341435]
I changed sessions to sessTbl as the database and table were both called sessions, and I thaught it may be selecting the database instaed of the table so I changed the table name and ammended the code to clarify that that was not where the problem laid.  I done this as I gathered that is what it mean by Object.
[/quote]
You already selected a db, otherwise you would get a 'no database selected' error.. You can't select or manipulate entries in a db, but only in tables, since a db is nothing more than a collection of tables. So what is the table named: 'sessions' or 'sessTbl'??

[quote author=scottybwoy link=topic=105051.msg420659#msg420659 date=1156341435]
do I need any of these : [, resource link_identifier [, int batch_size]] , for my query to work?
If so what do they mean, how do I find out what goes in there, again excuse my ignorance, I apreciate your help.
[/quote]

No, you don't need them right now.
Like I said before: anything without square brackets is required, meaning WITH is optional.

Share this post


Link to post
Share on other sites
[quote author=scottybwoy link=topic=105051.msg420659#msg420659 date=1156341435]
I changed sessions to sessTbl
[/quote]

Thanks for clearing up the [] definition.  But why am I recieving this error.  I have two databases one that has all the gubbins for my project and one called sessions with just one table called sessTbl with 3 columns sessKey, expiry and value.  At the top of lib.session_handler.php I have the database connection like so :

[code]
<?php

require_once('constants.php');
//require_once('class.DBI.php');

$DEBUG = 0;

//$DB_URL = "mssql://user:pass@localhost:/sessions";

mssql_connect("localhost", "user", "pass") or
die("Could not connect to database");
mssql_select_db("sessions") or
die("Could not select database");

//$dbi =  new DBI($DB_URL);

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

?>
[/code]

Then below are my functions.  Will this overide any original connections i.e my other database, which is used for login/authentication.  If not would it be viable to just have sessTbl in my other database and not have two?  As if it is searching for that tbl in the wrong database that could be why, it returns an invalid object.

Share this post


Link to post
Share on other sites
Ah, it's starting to make sense.

When you connect, you get a link resource id:

[quote=manual]resource mssql_connect ( [string servername [, string username [, string password [, bool new_link]]]] )

Returns: A MS SQL link identifier on success, or FALSE on error. [/quote]

If you use multiple db's, you either have to switch (mssql_select_db()), or use a new connection. If you choose the latter option (like you did), you have to specify which connection you want to use when sending a query.

For example:
[code]<?php
$link1 = mssql_connect("localhost", "user", "pass") or
die('Could not connect to database');
mssql_select_db('sessions') or die('Could not select database, MSSQL returned: '.mssql_error($link));
$link2 = mssql_connect("localhost", "user", "pass") or
die('Could not connect to database');
mssql_select_db('usermanagement') or die('Could not select database, MSSQL returned: '.mssql_error($link2));
mssql_query($query,$link1);
mssql_query($query,$link2);
?>[/code]

But since you only have one table in 'sessions' I'd suggest you use a single db.

Share this post


Link to post
Share on other sites
OK, "it didn't work" is a little meager. You need to add more debugging code.

[code]<?php
mssql_query($statement) or die ('Query failed. MS SQL returned: '.mssql_error());
?>[/code]

Share this post


Link to post
Share on other sites
By I mean it didn't work, it just gives the same Error, which suggests it's not a connection problem but maybe something to do with the query.  Please note I have tried using both the $statement variable and the actual $statement string, both recieve the same error and it appears that I don't have this built in function of mssql_error() as it now returns this error :

PHP Warning: mssql_query() [function.mssql-query]: message: Invalid object name 'sessTbl'. (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 PHP Fatal error: Call to undefined function mssql_error() in C:\Inetpub\wwwroot\database\classes\lib.session_handler.php on line 21

By the way am using Windows 2k Server with IIS 5 and php 5.1.4

Share this post


Link to post
Share on other sites
Can you verify 'sessTbl' is in the right capitalization?

If it is, I think you've got me beat for now.

Btw, are you using a framework package?

LOL, there isn't even a mssql_error() function.. ::) My bad!

Share this post


Link to post
Share on other sites
sessTbl is definately the right capitalisation, thats how I usually name things.  Sort of using a Framework package, what I am doing is using a tutorial book which has a similar project that I am trying to do, although it has more features than I need, so I'm trying to slim it down.  It used PEAR, however I don't need the database abstraction layer so I'm trying to take it out.  I'll post both versions so you can see the differences :

[code]
<?php

// my edited version

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 value FROM sessTbl WHERE sesskey = '$key' AND expiry > " . time();
                $result = mssql_query($statement) or die ('Query failed.');

                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;

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

                $statement = "INSERT INTO sessTbl VALUES ('$key', $expiry, '$value')";
                mssql_query($statement) or die ('Query failed.');

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

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

                return $result;
        }

        function sess_destroy($key) {

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

                return $result;
        }

        function sess_gc($maxlifetime) {

                $statement = "DELETE FROM sessTbl 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]

The original version

[code]
<?php
require_once('constants.php');
require_once('class.DBI.php');
require_once 'DB.php';

$DEBUG = 0;

$DB_URL = "mssql://user:pass@localhost:/sessions";

$dbi =  new DBI($DB_URL);

$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 $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;
        }

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

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

                $statement = "INSERT INTO sessions VALUES ('$key', $expiry, '$value')";
                $result = $dbi->query($statement);

                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();
                        $result = $dbi->query($statement);
                }

                return $result;
        }

        function sess_destroy($key) {
                global $dbi;

                $statement = "DELETE FROM sessions WHERE sesskey = '$key'";
                $result = $dbi->query($statement);
                if ($DEBUG) echo "sess_destroy: $statement <br>result: $result<br>";

                return $result;
        }

        function sess_gc($maxlifetime) {
                global $dbi;

                $statement = "DELETE FROM sessions WHERE expiry < " . time();
                $qid = $dbi->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]

There is one file called class.PHPApplication.php which is like the central backbone to all the other areas of the application, near the beginning it has a require_once for lib.sessions_handler.php -- the file I'm having trouble with.  class.PHPApplication.php also connects to the database via this function :
[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]

Thanks again for your time

Share this post


Link to post
Share on other sites
Ok I tottaly changed my query to "SELECT * FROM users" and it brings up the same Error Msg, that users is an Invalid Object.

But it must have connected to that table before for authentication otherwise it would not execute this far.
Where could this problem lie.

Would it tell me if there was not a connection to this Database if it droped it before executing this script?

Share this post


Link to post
Share on other sites
Where do you define $AUTH_DB_TBL? As used in your connect function? I have a feeling (since its in caps) that it is meant to be a constant. Maybe it is defined in constants... can we see?

If it is a constant, try changing your code within connct() to...

[code=php:0]
mssql_select_db(AUTH_DB_TBL);
[/code]

Share this post


Link to post
Share on other sites
Yep it is a constant, here's the code :
[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;

// Which also calls these at the bottom

//  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]

Found in config.inc.php which is loaded from the conf files of my classes that extend the class.PHPApplication.php file.  In this case what is happening is :

index.php is executed which just redirects to home.php ->
home.php loads home.conf.php and extends class.PHPApplication.php ->

If user does not have a session home.php relies on class.PHPApplication.php to redirect to login.php

login.php loads login.conf.php and extends class.PHPApplication.php ->

Here the username is validated and those credetials are used to create a session by lib.session_handler.php -- this is where I run into problems.

I tried what you said and took out the quotes but did not make a difference.  I hope there is enough info here for you to help me, thanks.

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.