My Connection Class;firstcode.php
class DB_functions {
public $db;
function __construct() {
try{
$db = new PDO("mysql:localhost;d
You're almost there. In your class, you need to change each iteration of $db
with:
$this->db
So your class would look like this:
class DB_functions {
public $db;
function __construct() {
try{
$this->db = new PDO("mysql:localhost;dbname=xxx;charset=utf8","xxx","xxx");
echo 'Connected';
}catch(PDOException $e){
print $e->getMessage();
echo "No Connection";
}
}
function __destruct() {}
public function test(){
$query = $this->db->query("SELECT * FROM User", PDO::FETCH_ASSOC);
if($query->rowCount()){
foreach ($query as $row) {
print_r($row);
}
}
}
}
As you're referencing the internal class variable. It's only accessible within the class scope and is referenced through $this
.