Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 1 of 1
  1. #1
    Member
    Join Date
    Apr 2011
    Posts
    81
    Member #
    27344
    Liked
    3 times
    I am trying to start using a session database to store all my session data in it but I am having the following error:

    mysqli_close() expects parameter 1 to be mysqli, null given

    Can anyone indicate how I can solve this problem?
    Also, I am unable to log in.
    No data is being saved in my database table.

    Here is my php script:

    <?php

    // include configuration file to make use of the error handling function
    require_once('config.inc.php');

    // global variable used for the database connections in all session functions
    $sdbc=NULL;

    // define the open_session() function

    function open_session()

    {

    global $sdbc;

    // set the MySQL host, username, password and database name as constants

    define('DB_HOST','myhostname');
    define('DB_USER','myusername');
    define('DB_PASS','mypass');
    define('DB_NAME','mydatabase_name');

    // connect to MySQL
    $sdbc = @mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);

    // handle any errors if the database connection failed
    if(!$sdbc) {
    trigger_error('Could not connect to MySQL:' . mysqli_connect_error() ); }

    return true;

    } // end of open_session() function

    // define the close_session() function
    // this function takes also no argument
    // this function closes the database connection

    function close_session()

    {

    global $sdbc;

    return mysqli_close($sdbc);

    } // end of close_session() function

    // define the read_session() function
    // this function takes one argument: the session ID
    // this function retrieves the session data

    function read_session($session_id)

    {

    global $sdbc;

    // query the database

    $q=sprintf("SELECT session_data
    FROM sessions
    WHERE session_id='%s'",mysqli_real_escape_string($sdbc,$ session_id));

    $r=mysqli_query($sdbc,$q) or
    trigger_error("Query: $q\n<br />MySQL Error:" . mysqli_error($sdbc));

    // retrieve the results
    if(mysqli_num_rows($r) == 1)

    {

    list($session_data)=mysqli_fetch_array($r,MYSQLI_N UM);

    // return the data
    return $session_data;

    }

    else

    {

    // return an empty string
    return '';

    }

    // get user id from session
    $user_id=$_SESSION['user_id'];

    } // end of read_session() function

    // define the write_session() function
    // this function takes 3 arguments:
    // - the session ID
    // - the user ID
    // - the session data

    function write_session($session_id,$user_id,$session_data)

    {

    global $sdbc;

    // store data in session database
    $q=sprintf("REPLACE INTO sessions (session_id,user_id,session_data)
    VALUES ('%s','%d','%s')",
    mysqli_real_escape_string($sdbc,$session_id),
    mysqli_real_escape_string($sdbc,$user_id),
    mysqli_real_escape_string($sdbc,$session_data));

    $r=mysqli_query($sdbc,$q) or
    trigger_error("Query: $q\n<br />MySQL Error:" . mysqli_error($sdbc));

    } // end of write_session() function

    // define the destroy_session() function
    // this function takes one argument: the session ID

    function destroy_session()

    {

    global $sdbc;

    // delete from session database
    $q=sprintf("DELETE FROM sessions
    WHERE session_id='%s'",
    mysqli_real_escape_string($sdbc,$session_id));

    $r=mysqli_query($sdbc,$q) or
    trigger_error("Query: $q\n<br />MySQL Error:" . mysqli_error($sdbc));

    // clear the $_SESSION array:
    $_SESSION=array();

    return mysqli_affected_rows($sdbc);

    } // end of destroy_session() function

    // define the clean_session() function
    // this function takes one argument: a value in seconds

    function clean_session($expire)

    {

    global $sdbc;

    // delete old sessions:
    $q=sprintf("DELETE FROM sessions
    WHERE DATE_ADD(session_last_accessed, INTERVAL %d SECOND) < NOW()", (int)$expire);

    $r=mysqli_query($sdbc,$q) or
    trigger_error("Query: $q\n<br />MySQL Error:" . mysqli_error($sdbc));

    return mysqli_affected_rows($sdbc);

    } // end of clean_session() function

    // declare the functions to use
    // tell php to use the session handling functions:

    session_set_save_handler('open_session','close_ses sion','read_session','write_session','destroy_sess ion','clean_session');

    // start the session:
    session_start();

    // close php script

    ?>

    Here is my table:

    CREATE TABLE sessions(
    session_id CHAR(32) NOT NULL,
    user_id INT UNSIGNED NOT NULL,
    session_data TEXT,
    session_last_accessed DATETIME NOT NULL,
    PRIMARY KEY (session_id),
    INDEX (session_id),
    INDEX (user_id))
    ENGINE=MyISAM;

  2.  


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Search tags for this page

save session data to mysql

Click on a term to search for related topics.
All times are GMT -6. The time now is 02:54 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com