Connect to the database:

/*** connection credentials *******/
$servername = "www.watzekdi.net";
$username = "watzekdi_cs393";
$password = "KevinBac0n";
$database = "watzekdi_imdb";
$dbport = 3306;


/****** connect to database **************/

try {
$db = new PDO("mysql:host=$servername;dbname=$database;charset=utf8;port=$dbport", $username, $password);
}
catch(PDOException $e) {
echo $e->getMessage();
}


Basic query to select 10 rows from the movies table:

try {
$stmt = $db->prepare("select * from movies limit 10");

$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

/** see the resulting array **/
var_dump($rows);

/** loop through the rows: **/
foreach ($rows as $row){
$id=$row["id"];
$name=$row["name"];
$year=$row["year"];
echo "id: $id, name: $name, year: $year";
}

}
catch (Exception $e) {

echo $e;
}

Results:

array(10) { [0]=> array(4) { ["id"]=> string(1) "0" ["name"]=> string(3) "#28" ["year"]=> string(4) "2002" ["rank"]=> NULL } [1]=> array(4) { ["id"]=> string(1) "1" ["name"]=> string(35) "#7 Train: An Immigrant Journey, The" ["year"]=> string(4) "2000" ["rank"]=> NULL } [2]=> array(4) { ["id"]=> string(1) "2" ["name"]=> string(1) "$" ["year"]=> string(4) "1971" ["rank"]=> string(3) "6.4" } [3]=> array(4) { ["id"]=> string(1) "3" ["name"]=> string(13) "$1,000 Reward" ["year"]=> string(4) "1913" ["rank"]=> NULL } [4]=> array(4) { ["id"]=> string(1) "4" ["name"]=> string(13) "$1,000 Reward" ["year"]=> string(4) "1915" ["rank"]=> NULL } [5]=> array(4) { ["id"]=> string(1) "5" ["name"]=> string(13) "$1,000 Reward" ["year"]=> string(4) "1923" ["rank"]=> NULL } [6]=> array(4) { ["id"]=> string(1) "6" ["name"]=> string(15) "$1,000,000 Duck" ["year"]=> string(4) "1971" ["rank"]=> string(1) "5" } [7]=> array(4) { ["id"]=> string(1) "7" ["name"]=> string(22) "$1,000,000 Reward, The" ["year"]=> string(4) "1920" ["rank"]=> NULL } [8]=> array(4) { ["id"]=> string(1) "8" ["name"]=> string(22) "$10,000 Under a Pillow" ["year"]=> string(4) "1921" ["rank"]=> NULL } [9]=> array(4) { ["id"]=> string(1) "9" ["name"]=> string(8) "$100,000" ["year"]=> string(4) "1915" ["rank"]=> NULL } }

id: 0, name: #28, year: 2002

id: 1, name: #7 Train: An Immigrant Journey, The, year: 2000

id: 2, name: $, year: 1971

id: 3, name: $1,000 Reward, year: 1913

id: 4, name: $1,000 Reward, year: 1915

id: 5, name: $1,000 Reward, year: 1923

id: 6, name: $1,000,000 Duck, year: 1971

id: 7, name: $1,000,000 Reward, The, year: 1920

id: 8, name: $10,000 Under a Pillow, year: 1921

id: 9, name: $100,000, year: 1915


Calling a function to query the actor table by first and last name

function getActorByName($db, $firstName, $lastName){

try {
$stmt = $db->prepare("SELECT * FROM actors WHERE first_name=:firstName and last_name=:lastName");
$data=array(":firstName"=>$firstName, ":lastName"=>$lastName);
$stmt->execute($data);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $rows;

} catch (Exception $e) {
return false;
}

}



if ($rows=getActorByName($db, "Kevin", "Bacon")){
var_dump($rows);
}
else{
echo "no results";
}


Results:

array(1) { [0]=> array(5) { ["id"]=> string(5) "22591" ["first_name"]=> string(5) "Kevin" ["last_name"]=> string(5) "Bacon" ["gender"]=> string(1) "M" ["film_count"]=> string(2) "73" } }