I have just got a school assignment to practice PDO. The problem is i seem to get it to connect to database but can't get it to insert or count rows in database. This might be a possible duplicate question did try to search for answers but might just been asking wrong questions.
<?php
include("db.class.php");
class uppClass extends Database {
function __construct() {
parent::__construct();
}
public function countUsers() {
$stmt = $this->connection->prepare("SELECT * FROM users LIMIT 500");
$stmt->execute();
return count( $stmt->fetchAll(PDO::FETCH_ASSOC) );
}
}
?>
next class handles the pdo connection
<?php
include("db-details.php");
class Database {
public $connection;
/**
* Opens a connection to the DB
*/
public function __construct() {
try {
$this->connection = new PDO("mysql:host=$this->DATABASE_ADDRESS;dbname=$this->DATABASE_NAME;", DATABASE_USERNAME, DATABASE_PASSWORD);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
$this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
}
}
?>