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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

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