I'm going to show you three ways to connect PHP and MySQL, from really old and unacceptable for this class, to slightly less old but still unacceptable, to the actual approach we are going to use. But you will still see the old approaches in various places, so you should know they exist.
The oldest style is shown in
the bookreportpub.phps example,
toward the bottom of the file establishes a MySQL connection and
selects a database. Your analogous commands would look like:
Now in the code that actually performs a query (higher up in the
file) you use the name of the table that you created using
phpMyAdmin. For example, if your table name is hw5, your select
statement might look something like:
$db = mysql_connect("mysql.truman.edu", "abc1234", "password");
$er = mysql_select_db("abc1234CS315");
$sql = "select lastname, firstname, bannerID from abc1234CS315.hw5";
$result = mysql_query($sql);
while ($r = mysql_fetch_array($result)) {
$db = mysqli_connect("mysql.truman.edu", "abc1234", "password");
$er = mysqli_select_db($db, "abc1234CS315");
$sql = "select lastname, firstname, bannerID from abc1234CS315.hw5";
$result = mysqli_query($db,$sql);
while ($r = mysqli_fetch_array($result)) {
Note that the $db object needs to be passed as a parameter to the doAll
function.
$db = new PDO("mysql:dbname=bookstuff;host=vh216602.truman.edu",
"bookuserSel", "AbQm");
$result = $db->query($sql) or die ("Mysql query failed");
foreach ($result as $r) {
Again $db needs to be passed as a parameter and the result is just
an array of associative arrays that can be stepped through.