Jump to content

Recommended Posts

Here is the dbconnect.php file that I've written for my website. My question is, I'm running one database with multiple tables, should I declare all the tables in this file so I can access them all from the different pages (in case i need to access diff tables)? I dont think I need to declare the fields though, I think that may be overkill from a tutorial I did previously. What about if I have multiple databases, do / can I declare both of those databases here also? What I'm trying to do is create one global file that I can include on all my php scripts as my db connector, then continue writing the code for my queries or other actions. Any advice would be great. Thanks much!

Tony

<?php
//Connect To Database
$hostname="hostnamehere";
$username="username";
$password="password";
$dbname="dbname";
$usertable="table_im_using";
$firstName = "field_iwant_to_query";
$lastName = "2ndfield_i_want_to_query";

mysql_connect($hostname,$username, $password) OR DIE ("Unable to connect to database! Please try again later.");
mysql_select_db($dbname);
?>
Link to comment
https://forums.phpfreaks.com/topic/5619-dbconnectphp-include-on-my-pages/
Share on other sites

[!--quoteo(post=357726:date=Mar 23 2006, 01:55 PM:name=tpupaz)--][div class=\'quotetop\']QUOTE(tpupaz @ Mar 23 2006, 01:55 PM) [snapback]357726[/snapback][/div][div class=\'quotemain\'][!--quotec--]
<?php
//Connect To Database
$hostname="hostnamehere";
$username="username";
$password="password";
$dbname="dbname";
$usertable="table_im_using";
$firstName = "field_iwant_to_query";
$lastName = "2ndfield_i_want_to_query";

/*
mysql_connect($hostname,$username, $password) OR DIE ("Unable to connect to database! Please try again later.");
mysql_select_db($dbname);
*/

?>
[/quote]

I've decided to comment out the mysql_connect section, so I could always select a new db if I have to.
and add the variables to the list above.

Does anyone have a good example of a global db.php file that I could look at to see if I'm doing this right?

Thanks
Tony
well you could declare all the tables but you would have to write a dbconnect for each table and that would be a pain in the a** all you have to do is this for your dbconnect.php

[code]

<?php
//Connect To Database
$hostname="hostnamehere";
$username="username";
$password="password";
$dbname="dbname";


/*
mysql_connect($hostname,$username, $password) OR DIE ("Unable to connect to database! Please try again later.");
mysql_select_db($dbname);
*/

?>

[/code]

and when you want to access a table you just put this in in your page

[code]

<?
include 'dbconnect.php';

mysql_query("SELECT * from table_im_using");

[/code]

that way you dont have to make more then one dbconnect.php file
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.