Jump to content

$_SESSION and sql links


kww

Recommended Posts

I'm trying to make a link to an SQL database survive though page reloads, and it's not working. Connecting, I use this:

 

function link_to_converted_database()

{

$link = mysqli_init();

if (!$link)

  {

  die('mysqli_init failed');

  }

if (!mysqli_options($link, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 1'))

  {

  die('Setting MYSQLI_INIT_COMMAND failed');

  }

if (!mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 50))

  {

  die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');

  }

if (!mysqli_real_connect($link, 'redacted', 'redacted', 'redacted'))

  {

  die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());

  }

comment( ' Success... ' . mysqli_get_host_info($link) );

 

if (mysqli_query($link,'USE conversion_database'))

  {

  comment("now using conversion_database.");

  }

else

  {

  die('Conversion database unusable (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());

  }

$_SESSION['mysql_link']=$link;

echo "<!--\n";

var_dump($_SESSION['mysql_link']);

echo "\n-->\n";

return(true);

}

 

Subsequent SQL commands work fine using $_SESSION['mysql_link'], and the var_dump returns:

 

object(mysqli)#2 (17) {

  ["affected_rows"]=>

  int(0)

  ["client_info"]=>

  string(48) "mysqlnd 5.0.7-dev - 091210 - $Revision: 294543 $"

  ["client_version"]=>

  int(50007)

  ["connect_errno"]=>

  int(0)

  ["connect_error"]=>

  NULL

  ["errno"]=>

  int(0)

  ["error"]=>

  string(0) ""

  ["field_count"]=>

  int(0)

  ["host_info"]=>

  string(24) "192.168.1.101 via TCP/IP"

  ["info"]=>

  NULL

  ["insert_id"]=>

  int(0)

  ["server_info"]=>

  string(16) "5.1.48-community"

  ["server_version"]=>

  int(50148)

  ["sqlstate"]=>

  string(5) "00000"

  ["protocol_version"]=>

  int(10)

  ["thread_id"]=>

  int(194)

  ["warning_count"]=>

  int(0)

}

 

After a page changes, all my $_SESSION vars work fine except for  $_SESSION['mysql_link']. It's still there, but SQL queries bomb, and var_dump returns:

 

Warning: var_dump(): Property access is not allowed yet in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\voicereader\serverstate.php on line 595

 

Warning: var_dump(): Property access is not allowed yet in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\voicereader\serverstate.php on line 595

 

Warning: var_dump(): Couldn't fetch mysqli in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\voicereader\serverstate.php on line 595

 

Warning: var_dump(): Couldn't fetch mysqli in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\voicereader\serverstate.php on line 595

 

Warning: var_dump(): Couldn't fetch mysqli in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\voicereader\serverstate.php on line 595

 

Warning: var_dump(): Couldn't fetch mysqli in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\voicereader\serverstate.php on line 595

 

Warning: var_dump(): Couldn't fetch mysqli in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\voicereader\serverstate.php on line 595

 

Warning: var_dump(): Couldn't fetch mysqli in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\voicereader\serverstate.php on line 595

 

Warning: var_dump(): Couldn't fetch mysqli in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\voicereader\serverstate.php on line 595

 

Warning: var_dump(): Couldn't fetch mysqli in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\voicereader\serverstate.php on line 595

 

Warning: var_dump(): Couldn't fetch mysqli in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\voicereader\serverstate.php on line 595

 

Warning: var_dump(): Couldn't fetch mysqli in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\voicereader\serverstate.php on line 595

 

Warning: var_dump(): Couldn't fetch mysqli in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\voicereader\serverstate.php on line 595

 

Warning: var_dump(): Couldn't fetch mysqli in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\voicereader\serverstate.php on line 595

object(mysqli)#1 (17) {

  ["affected_rows"]=>

  NULL

  ["client_info"]=>

  NULL

  ["client_version"]=>

  int(50007)

  ["connect_errno"]=>

  int(0)

  ["connect_error"]=>

  NULL

  ["errno"]=>

  NULL

  ["error"]=>

  NULL

  ["field_count"]=>

  NULL

  ["host_info"]=>

  NULL

  ["info"]=>

  NULL

  ["insert_id"]=>

  NULL

  ["server_info"]=>

  NULL

  ["server_version"]=>

  NULL

  ["sqlstate"]=>

  NULL

  ["protocol_version"]=>

  NULL

  ["thread_id"]=>

  NULL

  ["warning_count"]=>

  NULL

}

 

 

There's got to be a way to save my SQL linkages so that I don't have to reconnect to the SQL database every time I want to see if new data has arrived, but this doesn't seem to be it. My PHP version is PHP 5.3.2.0.

 

Thanks for any help.

Link to comment
https://forums.phpfreaks.com/topic/211331-_session-and-sql-links/
Share on other sites

You cannot do this, for two reasons -

 

1) The link is a resource and all resources are destroyed when your script on any page ends.

2) Because the session data is serialized when it is stored when your script ends, and resources cannot be serialized, you cannot save a resource as session data between page requests.

 

You have simply got to execute the code to create a database connection on each page request. Even if you happen to be on a server configuration where persistent database connections are supported, you must STILL execute the code to get an existing or create a new database connection on each page request.

"you must STILL execute the code to get an existing or create a new database connection on each page request."

 

I figured I was just doing one of those evil forbidden things. I understand how to "create a new database connection". Any hints on how to "get an existing ..." connection?

Archived

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

×
×
  • 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.