<?php
class Database {
public function __construct() {
}
public function connect() {
/** CONNECTION **/
$username = 'backstag_trythro';
$password = 'mhagja150793mk';
try {
$dbh = new PDO('mysql:host=localhost;dbname=backstag_levtest', $username, $password);
} catch (PDOException $e) {
echo $e->getMessage();
}
/** UPDATE **/
//PDO Class
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
$sth = $dbh->query("SELECT * FROM posts");
$result = $sth->fetchAll(PDO::FETCH_OBJ);
//print_r($result);
$this->daa = $result[1]->title;
}
}
I have a database class as seen above however I would like my code to have a different structure, like this:
<?php
class Database {
public function __construct() { }
public function connect() { }
public function select() { }
public function update() { }
public function delete() { }
}
So, everytime I call one of these functions I wouldn't have to repeat the connection code, instead I would call something like $this->connect();
I can't work out how to get variables to pass from one function to another within a class. When I split the code up, it has no trouble connecting but
I always get an error along the lines of "setAttribute undefined" or "query undefined". I think due to the connection not being passed to another function.
Is there an way I can do this?