0

In my index.php file I have included my db_connect.php file.

If I include another file (page1.php) in index.php, can I call my DB::query() function in page1.php? I must include db_connect.php to page1.php too?

Manos
  • 35
  • 1
  • 8
  • If you included page1.php after db_connect.php then yes – Hudson Jun 22 '18 at 16:09
  • If you don't want to have page1.php dependent on index.php, you could also use require_once() in page1.php. – Devon Bessemer Jun 22 '18 at 16:13
  • Thanks for your response! I try it without luck.I think it does not work because I load the page1.php with javascript... – Manos Jun 22 '18 at 16:13
  • ...what do you mean you load it with javascript? page1.php won't know about index.php if you load page1.php in a separate request... – Devon Bessemer Jun 22 '18 at 16:14
  • I do not have just one page and one class. – Manos Jun 22 '18 at 16:15
  • I load the page1.php in a div in index.php – Manos Jun 22 '18 at 16:15
  • @NoNameMan if you're loading page1.php inside index.php or you pass your db as a variable or you have to include it. – Lucarnosky Jun 22 '18 at 16:16
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Devon Bessemer Jun 22 '18 at 16:22
  • 1
    If you would share your code, we could better understand it. And if you had any problem, this might really be a good question..... – Nico Haase Jun 22 '18 at 19:43
  • If you're loading page1.php with javascript .load or .html like methods (that's what I undertand reading your comments) you will have to include your conection in that page. But agree with Nico, if you want concrete help, please provide your sample code. – Felipe Lima Jun 22 '18 at 21:21
  • Thank you all for your answers. I had a wrong impression. I did not include page1.php to my index.php. I have an input textbox (for search) in my index.php and with jquery .keyup() I load the results in a div in index.php. In page1.php I use DB::query() but I had include db_conect.php to index.php. I think the only way is to include db_connect.php to page1.php too. The problem is if I have too many files to include, I must include them to all pages. – Manos Jun 23 '18 at 17:14

1 Answers1

0

You may want to include_once('db_connect.php'); or even require_once('db_connect.php'); in any file that you are including that will need to make a database connection. Using these methods the file will not be loaded again if already included with another file. include_once() and require_once() act about the same, but they handle errors differently. You can find a good explanation of that here: Difference between require, include, require_once and include_once?

Rando
  • 11
  • 3