0

I would like some help with my connect class.This class is created by me it is my way of thinking so i would like to know why this class dossent get my variables and connect proprely to mysql.

the code

<?php /**
*

Exp:Connect to database in oop style

@params host dbname username password


*/

class Connect  
{
    //private only available forthe current class
    private $host=null;

    private $dbname = null;

    private $username = null;

    private $password = null;


    function __construct($host,$dbname,$username,$password)
    {

        $this->host = $host;

        $this->dbname = $dbname;

        $this->username = $username;

        $this->password = $password;

        $host = "localhost";
        $dbname = "testoop";
        $username = "root";
        $password = "";



    }



    function connect(){

    try {

    $conn = new PDO('mysql:host='.$this->host.';dbname='.$this->dbname, $this->username, $this->password);

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    } catch(PDOException $e) {

    echo 'Unable to connect';
    }

    }
} 

$con = new Connect();

$con->connect();




?>

Thank you for your time and any advices regarding security

Edited by dany12: Grammar mistake

2
Contributors
6
Replies
16
Views
3 Years
Discussion Span
Last Post by pritaeas
1

Lines 34-37 don't belong there, remove them. What you should do is change line 61 to:

$con = new Connect('localhost', 'testoop', 'root', '');
0

I would like to get the db connect values form the constructor or this is considered a bad practice?

Edited by dany12: grammar

1

Personally I wouldn't, but it's your choice. You can do this:

function __construct()
{
    $this->host = "localhost";
    $this->dbname = "testoop";
    $this->username = "root";
    $this->password = "";
}
0

Why would you not do this?

Is there any secuirty or performance issues involved?

0

Is there any secuirty or performance issues involved?

No. It's just that it's easier to reuse that class if it doesn't contain the connection information hard-coded.

This question has already been answered. 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.