SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm attempting to write my first function. Basically, I have 3 databases on my server: sohh, ohha2001 and vb114.

    For the past 4 days, I've only been connecting to one database, sohh, but now I want to connect to vb114.

    In a common.php file I had the following:

    $hostname = "hostname";
    $username = "username";
    $password = "password";

    $dbcnx = @mysql_connect($hostname, $username, $password);

    if (!$dbcnx) {
    echo("<P>Unable to connect to the database server at this time.</P>");
    exit();
    }

    // function to connect the database
    mysql_select_db(sohh, $dbcnx);

    // ensure the database is available
    if (! @mysql_select_db(sohh)) {
    echo("Unable to locate the <b>$dbase</b> database at this time.");
    exit();
    }

    Then in the top of cage_list.php I have:

    require("common.php");

    Then later on in in cage_list.php page I have:

    $result = mysql_query($query);

    if (!$result) {
    echo "<B>Error performing query/B> " . mysql_error();
    exit();
    }

    ...yadda-yadda-yadda...

    See http://www.sohh.com/pub2000/cage_list.php for the outcome.

    So I tried to make the way I connect to my database a function. So, in common.php I put:

    $hostname = "hostname";
    $username = "username";
    $password = "password";

    function connect_database($dbase) {

    $dbcnx = @mysql_connect($hostname, $username, $password);

    if (!$dbcnx) {
    echo("<P>Unable to connect to the database server at this time.</P>");
    exit();
    }

    // function to connect the database
    mysql_select_db($dbase, $dbcnx);

    // ensure the database is available
    if (! @mysql_select_db($dbase)) {
    echo("Unable to locate the <b>$dbase</b> database at this time.");
    exit();
    }
    return;
    }

    Then in my cage_list.php, I put:

    connect_database(sohh);

    I get the message "Can not connect to the databsae" or something like that.

    But no go...Any thoughts, comments or suggestions?
    <Edited by ibeblunt on 01-09-2001 at 03:57 PM>
    <///////~

    www.sohh.com

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Two things spring to mind..

    $dbcnx = @mysql_connect($hostname, $username, $password);
    are $hostname, $username and $password defined??
    i.e. is there a part in your script where you do
    $hostname = "localhost";
    or whatever your mysql server happens to be.

    Because you define your function as "function connect_database($dbase)", perhaps your mysql select should be mysql_select_db($dbase); (if no connection is specified, it uses the last opened connection).

  3. #3
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, they are defined. I'll add them in the example. For some reason I didn't cut and pass that part.
    Do you think it's because of where I placed my localhost, username and password settings? I placed them above the function.

    <Edited by ibeblunt on 01-09-2001 at 03:45 PM>
    <///////~

    www.sohh.com

  4. #4
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would try putting..

    $hostname = "hostname";
    $username = "username";
    $password = "password";

    actually in the function; your problem is that you cant connect to the mysql database.. have you checked with your admin that it is up? (or made sure it is running on your computer if that is where you are testing).

  5. #5
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, at 5:20pm my database was running. After I ran the following function, I shut down the database server.

    I changed the function to the following:

    function connect_database($dbase) {

    $hostname = "localhost";
    $username = "xxxx";
    $password = "xxxxxx";

    $dbcnx = @mysql_connect($hostname, $username, $password);

    if (!$dbcnx) {
    echo("<P>Unable to connect to the database server at this time.</P>");
    exit();
    }

    mysql_select_db($dbase, $dbcnx);

    if (! @mysql_select_db($dbase)) {
    echo("Unable to locate the <b>$dbase</b> database at this time.");
    exit();
    }

    return;

    }

    Weird.

    <///////~

    www.sohh.com

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you want to use a function to connect to the database you need to understand how php functions work in your first example you specified the host user and pass outside the function. There is no way for them to be accessible inside the function without using
    global $var; inside the function or passing them as args to the function if you want to use a function I would do it this way:

    in common.php

    function db_connect($host, $user, $pass, $db) {
    $dbcx = mysql_connect($host, $user, $pass);
    mysql_select_db($db);
    return $dbcx;
    }

    then on any of your pages where you need it.
    $dbcx = db_connect("localhost", "username", "password", "dbname");

    But really you are adding extra work when you could just keep the mysql_functions in a common folder and change the select db line for multiple dbs
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    SitePoint Addict ibeblunt's Avatar
    Join Date
    Jan 2001
    Location
    Jersey City, NJ
    Posts
    312
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not trying to blow up your brain Freddy, but what do you mean by "just keep the mysql_functions in a common folder and change the select db line for multiple dbs"

    This is all new to me. A bit confusing too, considering in CF to connect to a DB you just type:

    <CFQUERY NAME="NAMEOFQUERY" DATASOURCE="NAMEOFDATABASE">
    YOUR SQL STATEMENT WOULD GO HERE
    </CFQUERY>

    and that's it. THen again, CF is rather server intensive. So it's a trade off.
    <///////~

    www.sohh.com

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well if all you are concerned about is changing a db from time to time I would have this in my common.php

    $db = mysql_connect("host", "user", "password") or DIE("Can't connect");

    mysql_select_db($dbname);

    and then on pages you need db access

    $dbname = "sohh";
    include("common.php");
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

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