The assignment for web programming for this module was to make two connections from PHP to MySQL.
The web page I created for the assignment is here, containing links to pages for the first and second connections:
http://jered.0fees.us/module9/index.php
Connection 1
For the first connection, just pasting the code into a page and loading it naturally failed. After that I fixed a bad quote and added the closing “?>”, to get:
<?php
$db = mysql_connect("sql211.0fees.us","username","password");
if (!$db) {
die("Database connection failed miserably: " . mysql_error());
}
?>
I replaced the MySQL server, username, and password in the code with the login information for my 0fees account.
After that, the page was blank when it loaded (because it didn’t throw an error). I added a sentence in HTML to the page just so I’d know for certain that the page had loaded.
That was pretty straightforward. I just needed to get my MySQL login information from the 0fees cPanel.
Connection 2
I had to tweak the first quotation mark in this code too, as well as set information like the login information and database info. I tried loading the page before creating the database and saw the expected error, with a notice about a failure to connect to the database. Then I made the database and no table, and got the third error in the PHP code, also as expected. Then I created the table and added some data, and the PHP code ran with no error messages (or output, so I added a line of text to the second page too). The code I used was (with my username and password removed):
<?php
//Step1
$db = mysql_connect("sql211.0fees.us","username","password");
if (!$db) {
die("Database connection failed miserably: " . mysql_error());
}
//Step2
$db_select = mysql_select_db("username_module9",$db);
if (!$db_select) {
die("Database selection also failed miserably: " . mysql_error());
}
?>
<html>
<head>
<title>Step 3</title>
</head>
<body>
<?php
//Step3
$result = mysql_query("SELECT * FROM testdata", $db);
if (!$result) {
die("Database query failed: " . mysql_error());
}
?>
<p>If there were any errors, they will appear above this sentence.</p>
</body>
</html>
The code ran fine, and I didn’t have any problems connecting to the database or creating data through PHPMyAdmin. The instructions in the slides for working with cPanel and PHPMyAdmin worked well.