Results 1 to 13 of 13

Thread: [RESOLVED] More efficient way to change user connection

  1. #1

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Resolved [RESOLVED] More efficient way to change user connection

    Hi,

    What is a more efficient way to change user access to a database? At the moment I use:

    PHP Code:
    //connect to server or exit
    $conn = @mysql_connect("localhost""root""");
    if (!
    $conn) {
    die(
    "Connection failed: " .mysql_error());

    and just replace "root" with whatever the new user account is called. However, this means I have a lot of connections to my databases. What would be the best way to achieve this?
    Last edited by Nightwalker83; Dec 9th, 2009 at 12:08 PM. Reason: Fixing spelling
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: More efficient way to change user connection

    I'm not sure I know what you mean... From what I've gotten out of this, why don't you just use a variable?

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: More efficient way to change user connection

    why, exactly, do you need to have multiple users on the same database? that's incredibly inefficient if you don't have a real reason to.

    and, you can re-use connections by using mysql_pconnect() to create a persistent connection for each session.

  4. #4
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: More efficient way to change user connection

    Ditto to both prior posters - ideally don't use multiple user connections.

    If what you mean is that you have mysql_connect() all over your files and suddenly you need to change the connection info for all of them, I'd suggest either...

    A) Making a single function in a globally included file:
    Code:
    function myConnectFunction(){
      $conn = @mysql_connect("localhost", "root", "");
      if (!$conn) {die("Connection failed: " .mysql_error());}
      return $conn;
    }
    Now use that function instead of mysql_connect():
    Code:
    $conn = myConnectFunction();
    And if you need to change the connection info, there's only one spot to do it in.

    Or B) You could define some constants inside a globally included file:
    Code:
    define("MYSQL_HOST","localhost");
    define("MYSQL_USER","root");
    define("MYSQL_PASS","");
    And use those whenever you're connecting:
    Code:
    $conn = @mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_PASS);

  5. #5

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: More efficient way to change user connection

    Quote Originally Posted by kfcSmitty View Post
    I'm not sure I know what you mean... From what I've gotten out of this, why don't you just use a variable?
    Quote Originally Posted by kows View Post
    why, exactly, do you need to have multiple users on the same database? that's incredibly inefficient if you don't have a real reason to.

    and, you can re-use connections by using mysql_pconnect() to create a persistent connection for each session.
    With two websites I created for school, the databases use multiple functions. The "root" is only required to setup the other accounts and the admin and users are accounts used by the users of the website.

    Quote Originally Posted by SambaNeko View Post
    Ditto to both prior posters - ideally don't use multiple user connections.

    If what you mean is that you have mysql_connect() all over your files and suddenly you need to change the connection info for all of them, I'd suggest either...

    A) Making a single function in a globally included file:
    Code:
    function myConnectFunction(){
      $conn = @mysql_connect("localhost", "root", "");
      if (!$conn) {die("Connection failed: " .mysql_error());}
      return $conn;
    }
    Now use that function instead of mysql_connect():
    Code:
    $conn = myConnectFunction();
    And if you need to change the connection info, there's only one spot to do it in.

    Or B) You could define some constants inside a globally included file:
    Code:
    define("MYSQL_HOST","localhost");
    define("MYSQL_USER","root");
    define("MYSQL_PASS","");
    And use those whenever you're connecting:
    Code:
    $conn = @mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_PASS);
    Thanks! I will try that.

    Edit: What is the difference between mysql_connect() and myConnectFunction()?
    Last edited by Nightwalker83; Dec 9th, 2009 at 08:12 PM. Reason: Adding more!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: More efficient way to change user connection

    myConnectFunction is a user created function (created within his post) that calls and returns a MySQL connection. this is no different from calling mysql_connect() on its own. this would just allow you to keep the same connection details within a function that you could use in every one of your scripts. still, I would personally just have an include file that connects to the database, and then you can include it into whatever pages do need database connectivity. both would accomplish the same thing.

    but -- neither really accomplish what you're trying to do. you want to connect to your database server using multiple users, and each user you connect with requires a new connection. there is no way to avoid this except to ... not do it. or close connections when you don't use them, as you are opening new ones, assuming that that's possible in your situation.

    still, I would need a better understanding of what you're doing to gauge whether or not you should even be doing what you're doing. if this is a requirement in your class, that may be a little different. but it sounds silly to me. why can't your regular database user account insert into the "users" table? .. if it can do everything else, there would be no real reason to make you use your root account just to create users. and, in general, I don't think anyone really does this as far as web applications go. I could be wrong, and I'm interested in hearing what penagate might have to say about it.

  7. #7

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: More efficient way to change user connection

    I have moved the connection string for the database into its own files and it does what I want (on localhost at least). I doubt I will be able run my script on the server because it actually creates the admin and user accounts via code instead of manually putting them in to the database.

    On localhost this required adding new fields to the users table of the mysql database. I doubt I'd be allowed to do that on their server particularly a free one.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  8. #8
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: More efficient way to change user connection

    it just sounds like you're over complicating whatever you're doing, or something. if you'd like some help re-designing something, you can ask -- otherwise, I'm not sure how to respond from here on out.

  9. #9

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: More efficient way to change user connection

    Quote Originally Posted by kows View Post
    it just sounds like you're over complicating whatever you're doing, or something. if you'd like some help re-designing something, you can ask -- otherwise, I'm not sure how to respond from here on out.
    I was just experimenting with those two projects! I was seeing whether creating the entire database via code would be less effort rather than creating the database manually each time. This is the code I created to do the job. However, whether that script would work in real world situations is a different matter which I have now run into.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  10. #10
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: More efficient way to change user connection

    uhh, on any production servers most likely you're never going to have a root MySQL account to create users with. you usually create your user, then your database, then associate them both, then create your tables. you don't need more than one user, and you would pretty much never want to or need to create a MySQL user via PHP.

  11. #11

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: More efficient way to change user connection

    Ah well, I guess I'm going to have to set the database up manually which in this case I guess I could manage but bigger databases would be a hassle.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  12. #12
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: [RESOLVED] More efficient way to change user connection

    but bigger databases would be a hassle.
    Why?

  13. #13

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: [RESOLVED] More efficient way to change user connection

    Quote Originally Posted by SambaNeko View Post
    Why?
    Never mind! I was thinking that I would have to create the fields manually.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width