Jump to content

functions within functions and database connect issue


ajoo
Go to solution Solved by Barand,

Recommended Posts

Hi, Please can someone suggest if I need to connect to the database again and again if my main program calls subroutines which also need to connect to the SAME database or is there an alternative method by which I don't have to do this again and again. I read somewhere that connecting to the database time and again is a big waste of time resource.

 

I'll also try and illustrate my problem

 

///////////////////////////// main.php //////////////

<?php

 

mysqli_connect(host, user, pass, db)   // makes a connection to a database DB

 

 

get_field_1();      // a function in another file say functions.php 

 

?>

 

 

//////////////////// functions.php ////////////

 

<?

 

function get_field_1()

{

mysqli_connect(host, user, pass, db)

mysqli_connect(...)

 

get_field_2();     // calls another fucntion in functions.php

return val1;

 

 

function get_field_2()

{

mysqli_connect(host, user, pass, db)

mysqli_connect(...)

return val2;

}

 

?>

//////////////// END /////////////////// 

 

Is there a way by which I can avoid calling the following two lines within each function? 

 

mysqli_connect(host, user, pass, db)

mysqli_connect(...)

 

In fact I would like to call it just once in main and not have to call it again and again.

Thanks.

 

 

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.