Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
One Main Database Connection File
Old 08-07-2009, 02:31 PM One Main Database Connection File
Super Talker

Posts: 115
Name: Not Telling
Trades: 0
Can someone take these 3 files and make one big database connection string in php file from them?

And remove the old connections from them and make like a include or require for those 3.

This is what I mean:

PHP Code:
<?
// Connect to the database
mysql_connect ('localhost''database Username''database password') ;
mysql_select_db ('database name');
?>


These are the 3 files
File 1:
PHP Code:
<?php
/* fill in your database name */
$database_name "db_name";
/* connect to MySQL */
if (!$link mysql_connect("localhost""username""password")) {
  die(
"Could not connect: " mysql_error());
}
/* specify table name */
  
$sql "DELETE FROM $database_name.Persons";
  if(
$result mysql_query($sql)){
    echo 
"";
  }
  else{
    echo 
"Error deleting from Persons. MySQL Error: " mysql_error() . "";
  }

?>
File 2:

PHP Code:
<?php
$con 
mysql_connect("localhost","username","password");
if (!
$con)
  {
  die(
'Could not connect: ' mysql_error());
  }
mysql_select_db("db_name"$con);
$result mysql_query("SELECT * FROM Persons");
while(
$row mysql_fetch_array($result))
  {
  echo 
"";
  echo 
"First Name: " $row['FirstName'] . "";
  echo 
"<br>";
  echo 
"Last Name: " $row['LastName'] . "";
  echo 
"<br>";
  echo 
"******************************";
  echo 
"<br>";
  }
echo 
"</table>";
mysql_close($con);
?>
File 3:

PHP Code:
<?php
$con 
mysql_connect("localhost","username","password");
if (!
$con)
  {
  die(
'Could not connect: ' mysql_error());
  }

mysql_select_db("db_name"$con);

$sql="INSERT INTO Persons (FirstName, LastName)
VALUES
('
$_POST[Username]','$_POST[Password]')";

if (!
mysql_query($sql,$con))
  {
  die(
'Error: ' mysql_error());
  }
echo 
"";

mysql_close($con)
?>
sith717 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-10-2009, 06:20 AM Re: One Main Database Connection File
EdB
Skilled Talker

Posts: 79
Name: Ed Barnett
Trades: 0
What is it that you are trying to do with these scripts? The first example you posted is literally how to connect to a database server and select the database you want. What are the extra functions you are incorporating in the other three files?
__________________

Please login or register to view this content. Registration is FREE
EdB is offline
Reply With Quote
View Public Profile Visit EdB's homepage!
 
Old 08-10-2009, 11:33 PM Re: One Main Database Connection File
Experienced Talker

Posts: 47
Trades: 0
If you mean a file so you dont have to type up database connection everytime.

1) Create a file e.g. config.php
2) Add
PHP Code:
$con mysql_connect("localhost","username","password"
to the file and fill it out so the connection works.
3) open up the three files and replace
PHP Code:
$con mysql_connect("localhost","username","password"); 
with
PHP Code:
@include('./config.php'); 
Thats the answer to the title anyway.
NextToNothing is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to One Main Database Connection File
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.19222 seconds with 12 queries