A Newbie Question

Discussion in 'PHP' started by PalSys, Mar 10, 2007.

  1. #1
    Alright, I'm just in the early process of learning PHP, so forgive what is like a newbie mistake.

    I've written a config file defining these variables:

    <?php

    // MYSQL CONFIGURATION
    $mysqlServer = "localhost";
    $mysqlUser = "user";
    $mysqlPass = "password";
    $mysqlDB = "database";

    ?>

    And I'm calling them to connect to the database like this after including the config file:

    mysql_connect('$server', '$mysqlUser', '$mysqlPass') or die('I cannot connect to the database.');

    mysql_select_db('$mysqlDB');


    But of course on load it's looking for a host named '$server', etc. Why aren't the variables taking?
     
    PalSys, Mar 10, 2007 IP
  2. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Don't enclose the variable names in single quotes. By doing that, you simply have the text '$server' instead of the value stored in the variable.

    mysql_connect ($server, $user, $passwd);
     
    exam, Mar 10, 2007 IP
  3. PalSys

    PalSys palsys.io

    Messages:
    2,628
    Likes Received:
    224
    Best Answers:
    0
    Trophy Points:
    230
    #3
    That removed the initial error but now I'm getting:

    Access denied for user 'nobody'@'localhost' (using password: NO)

    The database settings are definitely right, the script doesn't seem to be getting the user value from the variable (although it did get the server?).

    Any other ideas? Thanks for the help by the way!
     
    PalSys, Mar 10, 2007 IP
  4. peeps

    peeps Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I might be completly wrong, but this works for me
    In my config file I have
    
    $conn = mysql_connect($host, $userName, $password);
    return $conn;
    Code (markup):
    Then in the file I want to connect to the data base I have.
    	include "<config file>.php";
    	mysql_select_db($dbName);
    	
    	$result = mysql_query("your SQL statement");
    Code (markup):
     
    peeps, Mar 10, 2007 IP
  5. sadcox66

    sadcox66 Spirit Walker

    Messages:
    496
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #5
    In your file place
    phpinfo(); 
    Code (markup):
    and look at the variables
     
    sadcox66, Mar 10, 2007 IP
  6. srobona

    srobona Active Member

    Messages:
    577
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    88
    #6
    Try somthing like this:

    $mysqlServer = "localhost";
    $mysqlUser = "user";
    $mysqlPass = "";
    $mysqlDB = "database";

    In your local server, if you do not set any username or password, then you don't need to set them, just leave the password blank. And the username as you set. May be it will help you.
     
    srobona, Mar 10, 2007 IP