Jump to content

dbconnect.php - include on my pages


tpupaz

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.