Jump to content

How avoid using mysql connection file ?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
shakeeb.u@briskon.com

shakeeb.u@briskon.com

    Newbie

  • Members
  • Pip
  • 5 posts
Hi All,

Am developing a project (small one) using php, I have created a database(MySQL) connection file, My problem is in any file where am writing mysql queries i need to include that connection file by using include funciton,
Is there any method by which i can avoid including connection file in every file where i need to write queries :confused:


Thanks & regards,
Shakeeb

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It's going to depend a lot on how your site is structured. For a secure site where index.php manages everything, you can include the file once and be done. If you have lots of independent files, then you probably don't have any good options.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
shakeeb.u@briskon.com

shakeeb.u@briskon.com

    Newbie

  • Members
  • Pip
  • 5 posts
Hi,

As am new to php and development so couldn't get you can you please explain in brief if possible by referring to some example (hope u don't mind).


Thanks & regards,
Shakeeb

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
There are several ways to organize a site. The most basic uses a bunch of static .html files. There is a tendency to use that model when you advance and use a bunch of .php files that are called directly by the browser. This would be something like page1.php, page2.php, page3.php. Each would need to include your connection file.

An alternative is to have a file like index.php, which is called with parameters. index.php?page=1, index.php?page=2, etc. Then it includes the dbconnection AND the appropriate pageX.php file.

You can also do some mix&match. For example, you could have pageX.php include resources.php, which then includes all the resource files you have created, including the db connection file. You could also have a cleanup.php file which closes db connections, etc.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
shakeeb.u@briskon.com

shakeeb.u@briskon.com

    Newbie

  • Members
  • Pip
  • 5 posts
ya now I got it, Thank you very much for such a good explanation.

Thanks & regards,
shakeeb

#6
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Possible the most common PHP architecture is called a "font controller." As WingedPanther suggested, you have an index.php file which will create your mysql connection, but it is also responsible for "including" a page that is requested - generally by using a switch block.