+ Reply to Thread
Results 1 to 4 of 4

Thread: SQL database error

  1. #1
    saxasm is offline x10Hosting Member saxasm is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    1

    SQL database error

    I'm trying to make a system to register on mysite.

    Unfortunately i get loads of errors when i try to connect to the database.


    So, what should the parameters be to mysql_connect() be here? I'm a bit of a noob when it comes to php-programming, but i have a few years of C++ experience, so learning it shouldn't be a problem.

    When i try what i think is intuitive ("database_name","database_username",database_pass word") i get errors. :eek4:

    So how should i be doing it?

  2. #2
    OdieusG is offline x10Hosting Member OdieusG is an unknown quantity at this point
    Join Date
    Jan 2009
    Location
    Bangor, Maine
    Posts
    50

    Re: SQL database error

    MySQL/PHP connections start with:
    $link = mysql_connect(<SERVER>, <USERNAME>, <PASSWORD>)

    I'm thinking once you connect, you're not selecting a database to use, most common error of all....just do this
    mysql_selectdb(<tablename>, $link)
    Put that in the code, and use the aforementioned function to connect, so it should look like:
    $link = mysql_connect(<server>, <username>, <password>);
    $result = mysql_selectdb(<databasename>, $link);

    I you want to do typechecking to make sure it didn't fail, you could always add after that code:
    if(!$result) { print mysql_error(); }

    Hope that was what you were looking for

  3. #3
    trike97 is offline x10Hosting Member trike97 is an unknown quantity at this point
    Join Date
    May 2009
    Posts
    5

    Re: SQL database error

    Code:
      // Define database connection constants
      define('DB_HOST', 'localhost');
      define('DB_USER', 'username');//put in username
      define('DB_PASSWORD', password');//put in password
      define('DB_NAME', 'databaseName');       //put in DB name
    
    
     //connect to database
             $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
             or die('Error connecting to MySQL server.');
    
             //display existing entries
            $query = "SELECT field FROM dataBase";
            $result = mysqli_query($dbc, $query);
            $row = mysqli_fetch_array($result);
    define constants for the database in case you change your password you wont have to change it on every page, and then put constants on seperate page and link to it.

  4. #4
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: SQL database error

    @trike97 Although your suggestion works well, it's not good coding practice to define passwords. Most of the time you will see coders use the password once and then unset the variable it is held in to be even more secure.

    What I do is create an abstract class with a static property that is the database connection:

    Code:
    abstract class DB {
    public static $DB;
    // inline parameters mean that the password will fall out of scope after this function executes
    // and no one can ever see them unless they can access the code
    final public static function connect($server,$user,$password) {
        self::$DB = mysql_connect($server,$user,$password); } } //password is not defined after this point
    
    
    DB::connect('localhost', 'username', 'password);
    
    // now any time you want to access your connection you can access it by using DB::$DB
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

+ Reply to Thread

Similar Threads

  1. Database Error
    By archianabhishek in forum Free Hosting
    Replies: 5
    Last Post: 04-04-2009, 05:03 PM
  2. Transferring SQL Database
    By Cavanaugh Corporation in forum Free Hosting
    Replies: 0
    Last Post: 08-16-2008, 07:50 PM
  3. WP mysql error : Error establishing a database connection
    By orangpelupa in forum Free Hosting
    Replies: 2
    Last Post: 12-19-2007, 05:08 AM
  4. Selecting MySQL database error
    By Nemcon in forum Free Hosting
    Replies: 0
    Last Post: 11-08-2007, 10:28 PM
  5. sQl error
    By THeLL in forum Free Hosting
    Replies: 1
    Last Post: 02-11-2005, 06:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers