Question
mySQL and PHP programming: Add a method called getAll() in the DBArtWork class that will read all rows from the artworks table and print them
mySQL and PHP programming:
Add a method called getAll() in the DBArtWork class that will read all rows from the artworks table and print them in the browsers window
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\index.php\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
require_once 'includes/db.config.php';
spl_autoload_register(function ($class) {
$file = 'lib/' . $class . '.class.php';
if (file_exists($file))
include $file;
});
echo "trying to connnect .... ";
$pdo = DBHelper::setConnection(CONNECTION_STR, USER, PASSWORD);
$artist = new DBArtist($pdo);
$resultSet = $artist->findById(200);
while ($row = $resultSet->fetch()) {
echo $row['name'] . ' ';
}
$artWork = new DBArtWork($pdo);
$artWorkRst = $artWork->findById(322);
foreach($artWorkRst as $row) {
echo 'Title: ' . $row['title'] . ' ';
}
echo "connection success! ";
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\DBArtist.class.php\\\\\\\\\\\\\\\\\\\\\\\\\
class DBArtist {
private $pdo = null;
private static $baseSQL = "SELECT * FROM Artists";
public function __construct($connection) {
$this->pdo = $connection;
}
public function getAll() {
$sql = self::$baseSQL;
$resultSet = $this->pdo->query($sql);
return $resultSet;
}
public function findById($id) {
$constraint = ' Where artistid='. $id;
$sql = self::$baseSQL . $constraint;
$stmt = $this->pdo->query($sql);
return $stmt;
}
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\DBArtWork.class.php\\\\\\\\\\\\\\\\\\\\
class DBArtWork {
public $pdo = null;
public static $base_sql = "SELECT * FROM artworks";
function __construct($con) {
$this->pdo = $con;
}
function findById($id) {
$constraints = " Where artworkid=" . $id;
$sql = self::$base_sql . $constraints;
$stmt = $this->pdo->query($sql);
return $stmt;
}
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\DBHelper.class.php\\\\\\\\\\\\\\\\\\\\\\\\\
class DBHelper{
public static function setConnection($connStr, $user, $pass) {
$pdo = new PDO($connStr, $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
}
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\db.config.php\\\\\\\\\\\\\\\\\\\\\\\\\\
define('DBNAME', 'csci2006');
define('HOST', 'localhost');
define('PORT', '8889');
define('USER', 'root');
define('PASSWORD', 'root');
define('CONNECTION_STR', 'mysql:host=' . HOST . ';dbname=' . DBNAME);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started