This is a simple post designed to get you started in playing around with getting data in and out of databases. This is an OS X centric post, and if you're following along the first thing you'll need to install is MAMP.
Next we're going to open the MAMP app, which will start the server and take us to an information page with the MAMP logo and details.
From here we're going to select the phpMyAdmin tab from the menu and create a new database that I'm going to call "test" in the "Create new database" box. Once you've done the same click "Create".
Now we need to create a PHP file inside a text editor (like TextWrangler) and save it inside the htdocs folder, which is inside the MAMP application folder. All files need to be saved here when working with MAMP.
The file will be called mysqltest.php and it will contain the following code:
This is an HTML table inside which is placed some PHP code. This code creates a PHP Data Object connection using the host, username and password that we saw in the MAMP home screen in the very first screenshot above, then queries and fetches before outputting.
I'm not going to enter great detail about this because the post is long enough already, but the comments will guide you through to a certain extent and hopefully you can play around with this very basic code.
Finally we need to view the results. To do this point your browser at: http://localhost:8888/mysqltest.php
The next step is to handle errors.
Next we're going to open the MAMP app, which will start the server and take us to an information page with the MAMP logo and details.
From here we're going to select the phpMyAdmin tab from the menu and create a new database that I'm going to call "test" in the "Create new database" box. Once you've done the same click "Create".
Now we need to create a table inside our database and give it a name and let phpMyAdmin know how many fields we want. I've called the table "age" and asked for three fields. Do the same, then press "Go".
Now we have to label these fields and identify the type of content they are going to have. I've called them, "Number", "Name" and "Age". The first and last columns should be set to TINYINT and the middle one to TINYTEXT and set the index column of the "Number" field to UNIQUE.
We then click the "Insert" tab and add some data.
Click go and we're done.
Now we need to create a PHP file inside a text editor (like TextWrangler) and save it inside the htdocs folder, which is inside the MAMP application folder. All files need to be saved here when working with MAMP.
The file will be called mysqltest.php and it will contain the following code:
<html>
<table>
<tr>
<td>Name</td>
<td>Age</td>
</tr>
<?php
// Enter username and password
$username = root;
$password = root;
// Create database connection using PHP Data Object (PDO)
$db = new PDO("mysql:host=localhost;dbname=test", $username, $password);
// Identify name of table within database
$table = 'age';
// Create the query - here we grab everything from the table
$stmt = $db->query('SELECT * from '.$table);
// Close connection to database
$db = NULL;
while($rows = $stmt->fetch()){
echo "<tr><td>". $rows['Name'] . "</td><td>" . $rows['Age'] . "</td></tr>";
};
?>
</table>
</html>
This is an HTML table inside which is placed some PHP code. This code creates a PHP Data Object connection using the host, username and password that we saw in the MAMP home screen in the very first screenshot above, then queries and fetches before outputting.
I'm not going to enter great detail about this because the post is long enough already, but the comments will guide you through to a certain extent and hopefully you can play around with this very basic code.
Finally we need to view the results. To do this point your browser at: http://localhost:8888/mysqltest.php
I've had difficulties with this but this helped a lot and got my u and running. Thank you! :)
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteAfter scouring the internet for hours this finally solved my problem!
ReplyDelete