0

Hi guys,
I'm trying to connect my new project with PDO, and for simple reason witch i don't know and i've no idea i'm getting

connection failedSQLSTATE[28000] [1045] Access denied for user 'www-data'@'localhost' (using password: NO)

I tried everything was in my mind, i tried log to mySql by terminal, i can log properly,and with some credencial work for another connection, native mysql, and i checked if PDO driver is enabled, and is connected, so bellow my pdo settins are:

config.php
<?php
define('DB_NAME', 'dbname');
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', 'pass');
?>
class.database.php
<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
include_once 'connection.php';

class dbConnection{
    protected $db_conn ;
    public  $db_name = DB_NAME;
    public  $db_host = DB_HOST;
    public  $db_user = DB_USER;
    public  $db_pass = DB_PASS   ;

    public function connect(){
        try {

        $this ->db_conn = new PDO("mysql:host = $this->db_host; dbname= $this->db_name", $this->user, $this->pass);
        return $this->db_conn;
        } catch (PDOException $e) {
            echo 'connection failed'. $e->getMessage();
        }
    }

}
?>
class.address.php
<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class manageAddress{
    public $link;

    function __construct() {
        include_once ('class.database.php');
        $conn = new dbConnection();
        $this->link =$conn->connect();
        return $this->link;
    }
    function getData( $table_name, $id = null){
        if(isset($id)){
        $query = $this->link->query("SELECT * FROM $table_name WHERE id = '$id' ORDER BY id ASC");    
        }
        else
        {
        $query = $this->link->query("SELECT * FROM $table_name ORDER BY id ASC");     
        }
        $rowCount = $query ->rowCount();
        if($rowCount >= 1)
        {
            $result = $query->fetchAll();
        }
        else 
        {
            $result = 0; 
    }
    return $result;
    }
}
?>
getData.php
<?php
session_start();

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
include_once ('class.address.php');
$init = new manageAddress();

$table_name = 'tbl_users';
$data = $init->getData($table_name);
print_r($data);

?>

So i post all code i'm using for now, please can someone point to right side!

Edited by hell hansen: sintax

2
Contributors
2
Replies
9
Views
2 Years
Discussion Span
Last Post by hell hansen
0

Connection string should be like

"mysql:host = $this->db_host; dbname= $this->db_name; $this->user; $this->pass;");

Edited by Shark_1

0

Hi Shark, Thanks for replyng, and u helped solve the error.
I review my code and i found that user and pass was incorrect defined.
so i solved using this line

  $this ->db_conn = new PDO("mysql:host = $this->db_host; dbname = $this->db_name", $this->db_user, $this->db_pass);

So now, my big problem is when i run the file the page is comming blank, no error message, no response for query i made to get data, and i tried debug the browser is getting error 500 Internal server error, so once againg i've no idea how to salve it, i don't know what's missing on php or mysql or apache

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.