Jump to content
Larry Ullman's Book Forums

Recommended Posts

I am running PHP under XAMPP on a Windows 7 machine. When running Script 3.1 (saving session data to database), I received an error on line 53. When I changed the constant from 'MYSQLI_NUM' TO 'MYSQL_NUM' this corrected the error. Is this a typo or is there some other reason for this? Thanks very much.

Link to post
Share on other sites

:blink: Thanks so very much for replying... I hope I've included the code appropriately. I believe I've used "mysqli" exclusively in all my calls. Question (since I'm a

newbie) - should I assume it to be MYSQLI_NUM for mysqli calls and MYSQL_NUM for the (older?) mysql calls?

 

 

 

<?php # Script 3.1 - db_sessions.inc.php

$sdbc = NULL;

 

function open_session() {

global $sdbc;

$sdbc = mysqli_connect ('localhost', 'ts_ajax', 'jANuary18', 'ts_ajax') or die('Cannot connect

to the database.');

return true;

}

 

function close_session() {

global $sdbc;

return mysqli_close($sdbc);

}

 

function read_session($sid) {

global $sdbc;

$q = sprintf('SELECT data FROM sessions WHERE id="%s"', mysqli_real_escape_string($sdbc,$sid));

$r = mysqli_query($sdbc,$q);

if(mysqli_num_rows($r)==1) {

list($data) = mysqli_fetch_array($r, MYSQL_NUM);

return($data);

} else {

return '';}

}

 

 

function write_session($sid, $data) {

global $sdbc;

$q = sprintf('REPLACE INTO sessions (id, data) VALUES ("%s", "%s")',

mysqli_real_escape_string($sdbc, $sid), mysqli_real_escape_string($sdbc, $data));

$r = mysqli_query($sdbc,$q);

return mysqli_affected_rows($sdbc);

}

 

function destroy_session($sid) {

global $sdbc;

$q = sprintf('DELETE FROM sessions WHERE id="%s"', mysqli_real_escape_string($sdbc,$sid));

$r = mysqli_query($sdbc,$q);

$_SESSION = array();

return mysqli_affected_rows($sdbc);

}

 

function clean_session($expire) {

global $sdbc;

$q = sprintf('DELETE FROM sessions WHERE DATE_ADD(last_accessed, INTERVAL %d SECOND) <

NOW()', (int) $expire);

$r = mysqli_query($sdbc,$q);

return mysqli_affected_rows($sdbc);

}

 

session_set_save_handler('open_session', 'close_session', 'read_session', 'write_session',

'destroy_session', 'clean_session');

 

session_start();

 

?>

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...