Dear users,
I am new to PHP5. My LAMP is working just fine.I made small database connect scripts and they just worked as it should.Then I tried the following code and run the mysql.php but showing me following error:

Could not run query: Access denied for user 'www-data'@'localhost' (using password: NO)

My user name is root and password is root.Please check to help me.God bless you all.

<?php

/*database.inc.php
 *
 */
define($server, "localhost");
define($conn_username, "root");
define($conn_password, "root");
define($database_name, "database_1.1");
?>

I think there is some mistake somewhere here:

<?php
class mysql 
{ 
    var $server; 
    var $conn_username; 
    var $conn_password; 
    var $database_name; 
    var $connection; 
    var $select; 
    var $query; 

    function connect() 
 { 
    require "database.inc.php"; 
     
    $connection = mysql_connect($server,$conn_username,$conn_password); 
    $select = mysql_select_db($database_name,$connection); 
} 
    function query($query) 
    { 
        $result = mysql_query($query); 
        if (!$result) { 
            echo 'Could not run query: ' . mysql_error(); 
            exit; 
} 
    } 
    function end() 
    { 
        mysql_free_result($connection); 
    } 
}

$mysql = new mysql(); 
$mysql->connect(); 

$mysql->query("SELECT * FROM Employee"); 
$mysql->end();
?>

It will be really helpful if anyone can show me how to get the output in the browser.I want to see a row of the table.

define() is used to declare a constant

since you are in a class, use

$this->server="localhost";

you also want your $connection variable like that:

class mysql 
{ 
    var $server; 
    var $conn_username; 
    var $conn_password; 
    var $database_name; 
    var $connection; 
    var $select; 
    var $query; 

    function connect() 
 { 
    require "database.inc.php"; 
     
    $this->connection = mysql_connect($this->server,$this->conn_username,$this->conn_password); 
    $this->select = mysql_select_db($this->database_name,$this->connection); 
} 
    function query($query) 
    { 
        $result = mysql_query($query); 
        if (!$result) { 
            echo 'Could not run query: ' . mysql_error(); 
            exit; 
} 
    } 
    function end() 
    { 
        mysql_free_result($this->connection); 
    } 
}

try this and see what error it shows.

$connection = mysql_connect($server,$conn_username,$conn_password); 
if (!$connection)
  {
  die('Could not connect: ' . mysql_error());
  }

try this.

Maybe that would help?

/* database.inc.php */

<?php

	define("server","localhost");
	define("conn_username", "root");
	define("conn_password", "root");
	define("database_name", "database_1.1");
?>
/* index.php */

<?php
	require "database.inc.php";
	
	class mysql 
	{ 
	    function connect() { 
	    	@mysql_connect(server,conn_username,conn_password); 
	    	@mysql_select_db(database_name); 
		} 
	
	    function query($query) { 
	        $result = mysql_query($query); 
	        if (!$result) { 
	            echo 'Could not run query: ' . mysql_error(); 
	            exit; 
			} 
			else{
				return $result;
			}
	    } 
	    
	    function showresult($result) {
	    	$arr = array();
			 @mysql_data_seek($result);
			 while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
			 	array_push($arr, $row);
			 }
			 return $arr;
	    }
	    function end() { 
	        @mysql_close(); 
	    } 
	}
	
	$mysql = new mysql(); 
	$mysql->connect(); 
	print_r($mysql->showresult($mysql->query("SELECT * FROM Employee"))); 
	$mysql->end();
?>

read more here: http://www.vijayjoshi.org/2011/01/07/php-defining-configuration-values/

Thanks Vijay.It was really helpful.You've got another user of your nice blog.

I'm not vijay :/ I just linked you to the blog.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.