3 Replies - 305 Views - Last Post: 15 March 2018 - 03:36 PM Rate Topic: -----

#1 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 261
  • Joined: 28-March 17

problem with include

Posted 15 March 2018 - 08:28 AM

I have 4 php files:
1)config.php (Db connection)
2)functions.php (with some functions)
3)form.php (the form that the user needs to submit)
4)form_handle.php (the form handling file)

Now, I use some functions from 'function.php' in 'form_handle.php', so I do the following:

I do 'include' to 'functions.php' and 'form_handle.php' on top of 'form.php', and inside each of them: 'function.php' and 'form_handle.php' I do 'include' to 'config.php'.

inside 'config.php' there is a '$conn' variable (the connection to the db), but I can't access it from 'form.php'
Why is that? I must include 'config.php' in 'form.php' as well? can't it "see" it through the other 2 files?

Thanks

This post has been edited by Foobarer: 15 March 2018 - 08:30 AM


Is This A Good Question/Topic? 0
  • +

Replies To: problem with include

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3801
  • View blog
  • Posts: 13,787
  • Joined: 08-August 08

Re: problem with include

Posted 15 March 2018 - 09:07 AM

Without seeing your code I'll guess that your config is loaded after $conn is needed in one of those files.

You could include the config inside a constructor like so:

class myClass {
	protected $conn;
	
	public function __construct() {
		include "config.php"; // Don't use $conn since it will go out of scope after the constructor closes.
		// Use $this->conn
	}
	
	/*
	* your methods would access $this->conn
	*/
}

Was This Post Helpful? 1
  • +
  • -

#3 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 261
  • Joined: 28-March 17

Re: problem with include

Posted 15 March 2018 - 03:21 PM

not using oop yet, but i will start learning it now, thanks!
Was This Post Helpful? 0
  • +
  • -

#4 benanamen   User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 194
  • Joined: 28-March 15

Re: problem with include

Posted 15 March 2018 - 03:36 PM

Your form code should all be in one page. The config should include the functions file, then you just include the config in your form.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1