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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!