Jump to content

Hold a global with database connect


tvtg

Recommended Posts

Hi,

 

I want to keep "mysql_connect" result in a global that can be used by few scripts

I have a file DBActivities.php which contains many functions that access my sql and

run some queries

 

so the file looks like that:

<?php

$dbhost = 'localhost';

$dbuser = 'root';

$dbpass = '';

$conn = '';

 

connect()

{

    global $conn = mysql_connect($dbhost, $dbuser, $dbpass) or ......

}

 

f1()

{

  run some query using conn as global

}

 

f2()

{

  run some query using conn as global

}

?>

 

when my main page is loaded I call connect with success meaning conn is now contains the DB connect result

 

now I have a second php file Stuff.php

<?php

      include DBActivities.php;

      f1();

      f2();

 

?>

 

I assumed that conn is global and was initated then when calling to f1 and f2 conn keeps the information

 

How do I maintain conn without keep connecting to the DB for every query

 

Thanks in advance

 

T.

Link to comment
https://forums.phpfreaks.com/topic/194448-hold-a-global-with-database-connect/
Share on other sites

More specifically, none global variables are retained between different executions of different scripts. Add call to connect() to the end of your dbactivities.php file, and make sure it's included in every file that needs MySQL 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.