Jump to content

Recommended Posts

Hello.

 

If i have a page called index.php, and i include all my pages by $_GET. And it's always the same DB. Do i need to connect on the included pages as well? I know i need different queries though.

And also, in a function do i need to connect to the db again there?

 

And is it important to close the db?

Link to comment
https://forums.phpfreaks.com/topic/114557-connecting-to-db/
Share on other sites

I'm not sure where $_GET comes into it.  But here's a rough guide...

 

if you include a file one of these ways....

 

    include('myfile.php');
    include_once('myfile.php');
    require('myfile.php');
    require_once('myfile.php');

 

... then any variables or resources which have been set in these files, will be usable in your current script.  If say, you had one page with a link on which called a second page, as soon as the browser loads the second page - your resources do not carry across.

 

A function cannot natively see a database connection created outside the function.  Perhaps you could look into mySQL database classes.  They are fairly easy to use and tidy things up a bit.

 

I'd say it's fairly important to close databases, though it will happen by itself.  I expect that PHP closes DB connections when the script terminates anyway!

 

Link to comment
https://forums.phpfreaks.com/topic/114557-connecting-to-db/#findComment-589066
Share on other sites

The best way is to make a file, where you specify your database connection, like:

 

$db_server = "localhost";
$db_database = "";
$db_user = "";
$db_pass= "";

// Veza na bazu
$veza = mysql_connect($db_server,$db_user, $db_pass) or DIE ('Nisam se uspio spojiti na server'); 
$veza_baza = mysql_select_db($db_baza,$veza) or DIE ('Nisam uspio odabrati bazu');

 

And then you include that file on every page where you need to do some queries. And in the end of that file where you have included your connection file you close your connection.

 

mysql_close($name of connection);

Link to comment
https://forums.phpfreaks.com/topic/114557-connecting-to-db/#findComment-589075
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.