Jump to content


Photo

dbconnect.php - include on my pages


  • Please log in to reply
2 replies to this topic

#1 tpupaz

tpupaz
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 23 March 2006 - 06:55 PM

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);
?>

#2 tpupaz

tpupaz
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 23 March 2006 - 07:15 PM

[!--quoteo(post=357726:date=Mar 23 2006, 01:55 PM:name=tpupaz)--][div class=\'quotetop\']QUOTE(tpupaz @ Mar 23 2006, 01:55 PM) View Post[/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

#3 shortj75

shortj75
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts
  • LocationPort Huron,mi

Posted 25 March 2006 - 01:02 AM

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


<?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);
*/

?>


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


<?
include 'dbconnect.php';

mysql_query("SELECT * from table_im_using");


that way you dont have to make more then one dbconnect.php file
The Short One

hopfully this helps if it doesn't sorry
i won't write the code for you!!! you don't learn that way




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users