Duel MySQL Connect

  • Bogey
  • Disturbed
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 7127
  • Loc: Ozzuland
  • Status: Offline

Post June 21st, 2008, 11:57 am

Alright, I have a problem at my site... I use one class to connect and select a database... all done at the same function... my database name, host, user and the password is defined in the siteConfig. Here is the class that connects and selects that I use...
PHP Code: [ Download ] [ Select ]
<?php
class db {
 
 # Class constructor and main
# function
 
 function connect($constant_url,$select = true) {
   include($constant_url);
   $con = mysql_connect($dbHost,$dbUser,$dbPass) or die(mysql_error() ." ( at line <strong>". (__LINE__) ."</strong> in <strong>". (__FILE__) ."</strong>)");
   if($con)
   {
    if($select == true)
    {
      $selectdb = mysql_select_db($dbDb) or die(mysql_error() ." ( at line <strong>". (__LINE__) ."</strong> in <strong>". (__FILE__) ."</strong>)");
      if($selectdb)
      {
       return true;
      } else {
       return false;
      }
    } else {
      return true;
    }
   } else {
    return false;
   }
 }
}
?>
  1. <?php
  2. class db {
  3.  
  4.  # Class constructor and main
  5. # function
  6.  
  7.  function connect($constant_url,$select = true) {
  8.    include($constant_url);
  9.    $con = mysql_connect($dbHost,$dbUser,$dbPass) or die(mysql_error() ." ( at line <strong>". (__LINE__) ."</strong> in <strong>". (__FILE__) ."</strong>)");
  10.    if($con)
  11.    {
  12.     if($select == true)
  13.     {
  14.       $selectdb = mysql_select_db($dbDb) or die(mysql_error() ." ( at line <strong>". (__LINE__) ."</strong> in <strong>". (__FILE__) ."</strong>)");
  15.       if($selectdb)
  16.       {
  17.        return true;
  18.       } else {
  19.        return false;
  20.       }
  21.     } else {
  22.       return true;
  23.     }
  24.    } else {
  25.     return false;
  26.    }
  27.  }
  28. }
  29. ?>

And the problem can be seeing here.

Go to the forum and you will see that it opens properly and that you can read the topics and everything... now go to guides/tutorials and you will see that the thing tries to connect to mysql using root as a user and my user is not root. Of course the forum uses MySQL so it connects there properly but at the guides/tutorials it does not connect properly.

No where in guides_tutorials.php do I re-define the MySQL variables... I can't find the problem as I use one class and one set of defined functions to connect... how can one variable hold two different information in them? Please help... I'm stuck :!:

The problem occured after I fixed it with a bug where it interpreted the \r\n's... It doesn't anymore :) but it also doesn't work anymore as well :(
Learn PHP | I got 10 PHP tutorials! Check them out!
Dreamtale - Farewell
Just a note... I've giving up on web development and that stuff... Just lost all interest in it.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post June 21st, 2008, 11:57 am

  • George L.
  • Bronze Member
  • Bronze Member
  • User avatar
  • Joined: Nov 05, 2007
  • Posts: 1836
  • Loc: sinister passage
  • Status: Offline

Post June 22nd, 2008, 8:22 am

How many tables do you have?
I do have a drug but it's not like pot or ecstasy it's love that's my drug and it will always be that way
(Thanks to boogs2007 & evenstar), this heart?
  • Bogey
  • Disturbed
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 7127
  • Loc: Ozzuland
  • Status: Offline

Post June 22nd, 2008, 11:45 am

1 Database created by me and it has 9 tables so far...
Learn PHP | I got 10 PHP tutorials! Check them out!
Dreamtale - Farewell
Just a note... I've giving up on web development and that stuff... Just lost all interest in it.
  • spork
  • HB
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 5479
  • Loc: Rochester, NY
  • Status: Offline

Post June 22nd, 2008, 11:50 am

Let's step back a moment and look at how your code is structured. Why are you using a class for your DB functionality if all it contains is a function? It looks to me like you're calling the function and including a bunch of DB constants from another file. These really should be data members of the class.
How to Maintain Simple, Static Pages in a CakePHP Application
EEEEEEEEE! It's here!!
  • Bogey
  • Disturbed
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 7127
  • Loc: Ozzuland
  • Status: Offline

Post June 22nd, 2008, 11:58 am

The reason I do this is because the files are in different directories so the file that is supposed to be included changes from Includes/siteConfig.php to ../Includes/siteCOnfig.php and all of the database constants are stored in the site configuration file
Learn PHP | I got 10 PHP tutorials! Check them out!
Dreamtale - Farewell
Just a note... I've giving up on web development and that stuff... Just lost all interest in it.
  • SpooF
  • Ice Cream
  • Bronze Member
  • User avatar
  • Joined: May 22, 2004
  • Posts: 2826
  • Loc: Richland, WA
  • Status: Offline

Post June 22nd, 2008, 12:41 pm

I would suggest that you pass your database information to the function in the class. You can store the values in another file, but one of the idea behind a class is that can be used in more than one instance.
College Taught Me - And we're not talking about the classes!
@travisperson
  • Bogey
  • Disturbed
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 7127
  • Loc: Ozzuland
  • Status: Offline

Post June 22nd, 2008, 1:26 pm

Nevermind... stupid me, I accidentally uploaded the SiteConfig to another directory and for some reason, it was taking that siteConfig rather than the one in the includes... it's all fixed now... thanks :)
Learn PHP | I got 10 PHP tutorials! Check them out!
Dreamtale - Farewell
Just a note... I've giving up on web development and that stuff... Just lost all interest in it.

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 183 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© Unmelted Enterprises 1998-2009. Driven by phpBB © 2001-2009 phpBB Group.