开发者

How declare a php variable to hold the return value of "mysql_connect()"?

开发者 https://www.devze.com 2023-03-12 16:48 出处:网络
I need to save the value of mysql_connect() in a global variable that\'s accessible in every php file in my project

I need to save the value of mysql_connect() in a global variable that's accessible in every php file in my project for example, $db_ServerVal = mysql_connect().

I need to be able to call mysql_connect only once, at the start of the program.

Then in every php file -- 开发者_开发问答that $db_ServerVal MUST be valid. Not the first time. EVERY time. Until I call mysql_close( $db_ServerVal);

Can I use $GLOBALS[], the pre-defined array in php, to store my $db_ServerVal database connection?

The other problem is -- I need have a 'close' event when the browser window closes, so that I will know that it's time to call mysql_close( $db_ServerVal);.

I can't call mysql_close() at the end of my index.php file since that will close the database prematurely, then every time I access the database I'd have to mysql_connect() again.

While I could call mysql_connect() and mysql_close() before and after each database call -- I'm not sure if that's the standard way things are done.


first, you do not need to store this variable, as ALL of the mysql_* function will use the last opened connection if you omit the db_connection argument. you would only need to store this if you were to open multiple db connections at the same time.

second, the mysql connection will automatically close at the end of your script. there is no need to close it in most cases.

third, you shouldn't use mysql_* anymore. either use mysqli_* or the recommended PDO class.


Don't close the database each time. You could even choose not to close the connection at all, since it will implicitly be closed at the end of script.

If you want to be able to connect at all times, you could write a function for that:

$_connection = false;
function getConnection()
{
  global $_connection;
  if (!$_connection)
    $_connection = mysql_connect( ... );
  return $_connection;
}

function closeConnection()
{
  global $_connection;
  if ($_connection)
    mysql_close($_connection);
  $_connection = false;
}

Of course, embedding the connection in an actual class is even better, but this will get you started to fix you problem. Baby steps. :)


This is from my db class, but I think this should help you. Just open the first time you need it and close at the end of your page. Or, like I do , have the connect in the contructor of your db class, and the close in the destructor.

//Constructor Code
$this->link_id = mysql_connect($this->db_host, $this->db_user, $this->db_pass);


//Destructor Code
@mysql_close($this->link_id);

Here is a sample of the 2 methods i use.

    //forward php4 to constructor
    function db_class() {

        return $this->__construct();
    }

    //constructor
    function __construct() {

        register_shutdown_function(array(&$this, "__destruct"));

        $this->link_id = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
        mysql_select_db($this->db_name, $this->link_id);

}
    //destructor
    function __destruct() {


        @mysql_close($this->link_id);

    }


I typically do my database connection call on a per page basis, not per query basis. So I include the file that does my database connection at the top of each file in which I need to connect:

require('db_connect.php');

And close it after I've done anything on the page that needed the connection. What you should do is based on your needs, however, so in that sense, it depends.


You can store the connection object in a global variable and access it via $GLOBALS. Note that unless you are using persistent connections (and you should know what you are doing if you do), the database connection will automatically close when your PHP script ends anyway.

0

精彩评论

暂无评论...
验证码 换一张
取 消