Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    408
    Thanks
    17
    Thanked 2 Times in 2 Posts

    connecting to database

    Maybe somebody can see what I'm doing wrong? my database connection is kicking out the 'error selecting' alert I put in. But I'm not sure where the problem is, as everything looks correct for database name



    Here's the code:
    PHP Code:
    <?php 



    function dbconn() {
        

        
    if(!include_once(
    '../filepath/dbc_include.php')) {
    die(
    'Error include file...');
    }


    if(!
    $link mysql_connect($db['hostname'],$db['username'],$db['password'],$db['database'])) {
    die(
    'Error connecting...');
    }


    if(!
    mysql_select_db($db)) {
    die(
    'Error selecting...');
    }

    return 
    $link;


    echo @
    mysql_ping() ? 'true' 'false';

    }
    $link dbconn(); 

    ?>

    and here's the include
    PHP Code:
    <?php 

    $db 
    = array(
    'hostname' => 'xxx.xxx',
    'username' => 'redacted1',
    'password' => 'redacted2',
    'database' => 'redacted1',
    ); 


    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    $db is an array. It cannot be used as the database name, that should be $db['database'].

    MySQL_Connect also does not accept a parameter for the database to select. The fourth parameter is to dictate if it should establish a new connection with the corresponding host, username and password, or if it should reuse an existing one that has connected with the same parameters. MySQLi on the otherhand does require that the database is provided (and you should consider updating to mysqli or pdo anyway).

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    408
    Thanks
    17
    Thanked 2 Times in 2 Posts
    ah... that fixed it..

    but now, I have to confess... mySQLi? This is something new to me - not that I'm all that experienced anyway, but I'll have to check that out...

    Do I need to do anything different with the database itself, or is that just new/extended functionality in the commands?

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    mysqli and PDO are object based database connections, complete with data sanitation built in (if you do it correctly).


  •  

    Posting Permissions

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