Page 1 of 1

Warning: mysql_connect() [function.mysql-connect]: Access...

Posted: Mon Mar 10, 2008 6:09 am
by Metaphor
Hello all,

I'm getting an error like this :

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: NO) in /www/987mb.com/r/i/d/ridingwavelda/htdocs/addons/stockms-0.1/includes/database.php on line 69
Database error : could not connect to

I've searched several posts and couldn't find the answer...

I have created a database, username and password. Am I supposed to insert them in the database.php code? If yes, please tell me where, I'm a complete begginer.

Here's the database.php code.

Thank you

Code: Select all

<?php
/*******************************************************************************
 *
 *   database.php
 * ----------------
 *
 * Start   : 18-10-2003
 * Version : 26-10-2003
 * Author  : A. Bietti (alexisbietti at yahoo dot fr)
 *
 ******************************************************************************/
 
/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/
 
class Database {
  var $classname = 'Database';
  var $_user = '';
  var $_pass = '';
  var $_database = '';
  var $_host = '';
  var $_queries = array();
 
  /**
   * Constructor.
   */
  function Database($host, $user, $pass, $database) {
    $this->_host = $host;
    $this->_user = $user;
    $this->_pass = $pass;
    $this->_database = $database;
 
    $this->_queries = array(
      'num_categories' => 'SELECT COUNT(`cat_id`) as stat ' .
                          'FROM `' . CATEGORIES_TABLE . '`',
      'num_brands'     => 'SELECT COUNT(`brand_id`) as stat ' .
                          'FROM `' . BRANDS_TABLE . '`',
      'num_references' => 'SELECT COUNT(`ref_id`) as stat ' .
                          'FROM `' . REFERENCES_TABLE . '`',
      'total_stock'    => 'SELECT SUM(`ref_price` * `ref_stock`) as stat ' .
                          'FROM `' . REFERENCES_TABLE . '`',
      'brand_name'     => 'SELECT `brand_name` as stat ' .
                          'FROM `' . BRANDS_TABLE . '` ' .
                          'WHERE `brand_id`=\'{_ID_}\' ' .
                          'LIMIT 1',
      'cat_name'       => 'SELECT `cat_name` as stat ' .
                          'FROM `' . CATEGORIES_TABLE .  '` ' .
                          'WHERE `cat_id`=\'{_ID_}\' ' .
                          'LIMIT 1',
      'brand_num_refs' => 'SELECT COUNT(`ref_id`) as stat ' .
                          'FROM `' . REFERENCES_TABLE . '` ' .
                          'WHERE `ref_brand`=\'{_ID_}\'',
      'cat_num_refs'   => 'SELECT COUNT(`ref_id`) as stat ' .
                          'FROM `' . REFERENCES_TABLE . '` ' .
                          'WHERE `ref_cat`=\'{_ID_}\'',
      'cat_inventory'  => 'SELECT SUM(`ref_price` * `ref_stock`) as stat ' .
                          'FROM `' . REFERENCES_TABLE . '` ' .
                          'WHERE `ref_cat`=\'{_ID_}\'',
    );
  }
 
  function connect() {
    mysql_connect($this->_host, $this->_user, $this->_pass)
      or die('Database error : could not connect to ' . $host);
    mysql_select_db($this->_database);
  }
 
  function query($sql) {
    return mysql_query($sql);
  }
 
  function fetch_object($result) {
    return mysql_fetch_object($result);
  }
 
  function fetch_array($result) {
    return mysql_fetch_array($result);
  }
 
  function num_rows($result) {
    return mysql_num_rows($result);
  }
 
  function free_result($result) {
    mysql_free_result($result);
  }
 
  function close() {
    mysql_close();
  }
 
  function error() {
    return mysql_error();
  }
 
  /**
   * Read an information in the database and return the value.
   *
   * @param $what   an identifier for the type of information
   * @param $id     optional, a primary key
   *
   * @return FALSE if parameters are wrong
   */
  function read($what, $id = 0) {
    $sql = $this->_queries[$what];
    if (!$sql) return FALSE;
    if ($id) $sql = str_replace('{_ID_}', $id, $sql);
 
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    mysql_free_result($result);
 
    return $row['stat'];
  }
 
  /**
   * Read a save file and restore the database with it.
   *
   * @param $fileName   file name
   *
   * @return array      containing errors if any
   */
  function restore($fileName) {
    $file = fopen($fileName, 'r'); // Open the file
    $sql = '';
    $line = '';
    $errors = array();
 
    // Stop when EOF is reached, even if there is an unfinished command
    while (!feof($file)) {
      // Read the next line of the file
      $line = trim(fgets($file));
 
      // Discard empty lines and comments
      if (strlen($line) > 0 && $line{0} != '#') {
        $sql = $sql . CRLF . $line;
 
        // Commands MUST end with a ';'
        if ($sql{strlen($sql) - 1} == ';') {
          // Execute the command
          $sql{strlen($sql) - 1} = ' ';
 
          @ mysql_query($sql);
          // Store any error
          if (mysql_error()) {
            $errors[] = mysql_error();
          }
          // Start a new command
          $sql = '';
        }
      }
    }
    return $errors;
  } // end function restore()
 
  /**
   * Save the database. Return a string containing SQL statements that
   * INSERT all the current records.
   *
   * @return   string
   */
  function save() {
 
    // Lock the tables
    mysql_query('LOCK TABLES `' . REFERENCES_TABLE . '` READ, `' . 
                 BRANDS_TABLE . '` READ, `' . CATEGORIES_TABLE . '`');
 
    // File starts with the magic line
    $dump = SAVE_FILE_MAGIC . CRLF;
 
    // 1. Save categories
    // 1.1. Get categories in database
    $sql = 'SELECT * FROM `' . CATEGORIES_TABLE . '`';
    $result = mysql_query($sql);
    $array = array();
 
    while ($row = mysql_fetch_object($result)) {
      $array[] = "('{$row->cat_id}', '" . escapeQuote($row->cat_name) . "')";
    }
 
    mysql_free_result($result);
 
    // 1.2. Add categories in dump
    $dump = $dump . '# Categories' . CRLF;
    if (count($array)) {
      $dump = $dump . 'INSERT INTO `' . CATEGORIES_TABLE .
              '` (`cat_id`, `cat_name`)' . CRLF . 'VALUES' . CRLF .
              implode(',' . CRLF, $array) . ';' . CRLF;
    } else {
      $dump = $dump . '# (no category)' . CRLF;
    }
 
    // 2. Save brands
    // 2.1. Get brands from database
    $sql = 'SELECT * FROM `' . BRANDS_TABLE . '`';
    $result = mysql_query($sql);
    $array = array();
    while ($row = mysql_fetch_object($result)) {
      $array[] = "('{$row->brand_id}', '" . escapeQuote($row->brand_name). "')";
    }
    mysql_free_result($result);
 
    // 2.2. Add brands in dump
    $dump = $dump . '# Brands' . CRLF;
    if (count($array)) {
      $dump = $dump . 'INSERT INTO `' . BRANDS_TABLE .
              '` (`brand_id`, `brand_name`)' . CRLF . 'VALUES' . CRLF .
              implode(',' . CRLF, $array) . ';' . CRLF;
    } else {
      $dump = $dump . '# (no brand)' . CRLF;
    }
 
    // 3. Save references
    // 3.1. Get references from database
    $sql = 'SELECT * FROM `' .REFERENCES_TABLE . '`';
    $result = mysql_query($sql);
    $array = array();
    while ($row = mysql_fetch_object($result)) {
      $array[] = "('{$row->ref_id}', '" . escapeQuote($row->ref_ref) . "', " .
        "'" . escapeQuote($row->ref_desc) . "', '{$row->ref_brand}', " .
        "'{$row->ref_cat}', '{$row->ref_price}', '{$row->ref_stock}')";
    }
    mysql_free_result($result);
 
    // 3.2. Add references in dump
    $dump = $dump . '# References' . CRLF;
    if (count($array)) {
      $dump = $dump . 'INSERT INTO `' . REFERENCES_TABLE .
              '` (`ref_id`, `ref_ref`, `ref_desc`, `ref_brand`, `ref_cat`, ' .                '`ref_price`, `ref_stock`)' . CRLF . 'VALUES' . CRLF .
              implode(',' . CRLF, $array) . ';' . CRLF;
    } else {
      $dump = $dump . '# (no reference)' . CRLF;
    }
 
    // Unlock the tables
    mysql_query('UNLOCK TABLES');
 
    return $dump;
  } // end function save()
 
  /**
   * Remove all the records stored in the database.
   */
  function flush() {
    mysql_query('TRUNCATE TABLE `' . REFERENCES_TABLE . '`');
    mysql_query('TRUNCATE TABLE `' . CATEGORIES_TABLE . '`');
    mysql_query('TRUNCATE TABLE `' . BRANDS_TABLE . '`');
  }
}
 
?>
 

Re: Warning: mysql_connect() [function.mysql-connect]: Access...

Posted: Mon Mar 10, 2008 6:30 am
by Sekka
When you use this Database object, are you creating it along the lines of...

Code: Select all

$database = new Database ("localhost", "username", "password", "dbname");

Re: Warning: mysql_connect() [function.mysql-connect]: Access...

Posted: Mon Mar 10, 2008 6:40 am
by Metaphor
Sorry I don't understand what you mean...

I created the database in my host control panel. I'm using the same database for this object and for the website in which I want to insert it. Is that right?

Then, when I ran install.php of this new object, I used the host, database name, database username and password of that database I created.

This error appears when I try to enter the new application...

Re: Warning: mysql_connect() [function.mysql-connect]: Access...

Posted: Mon Mar 10, 2008 8:41 am
by hannnndy
your username and password for the mysql does not match each other i think you have test your page on localhost

mysql user = root
mysql pass = null

then fter uploading files into the web you should change those to the new ones on the host

Re: Warning: mysql_connect() [function.mysql-connect]: Access...

Posted: Mon Mar 10, 2008 9:57 am
by Metaphor
Sorry can you be more specific?

Which values am i supposed to change?