Download presentation
Presentation is loading. Please wait.
Published byPhillip Dennis Modified over 9 years ago
1
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
2
Stack System In order to develop a web page which runs off a back end data base you will need to have a stack system in place, e.g. It is possible to create a stack on a standalone PC or server by installing WAMP Server (freely available as a download); WAMP works on top of Windows. WAMP has Apache web server, MySQL database, and PHP PHP scripting language MySQL database Apache web server Windows 7 operating system
3
Download WAMP Download WAMP server (www.wampserver.com/en/)and install. By default it places a folder www inside the wamp folder on the C: drive
4
Make sure WAMP is running There should be a green W icon in the notification tray in the lower right of the screen. A green icon means the WAMP server is running. If it is red it is turned off. If it is orange, there may be a problem (e.g. another server already running, an application using the port). You can also run WAMP server by choosing Programs > WampServer > Start WampServer
5
phpMyAdmin Open a web browser and type localhost (the default name for a home web server URL address). You should come up with a screen:
6
phpMyAdmin Click phpmyadmin The phpmyadmin database screen should appear The first time you use it, the default username is root, with no password Enter the username root and click Go
7
phpMyAdmin The MySQL database should open. Click the databases tab Enter simpledata for the name of the new database and click Create
8
phpMyAdmin Decide on the database structure: it is usually best to sketch this. Data is contained in a table called feedback Use the sketch below (an example): The example table has 3 columns, each column representing a field name The rows are the records representing the data (examples shown)
9
phpMyAdmin A list of databases appears with simpledata in the list Create a table by clicking the name of the database (simpledata)
10
phpMyAdmin Type a name for the table: feedback Enter 3 as the number of columns (fields) Click Go (on the far right of the window)
11
phpMyAdmin The table structure should appear like below: The 3 columns are now represented by the 3 rows now
12
phpMyAdmin Enter the names for the first column. This represents the fields Enter the Type and Length/Values, then click Save (lower right):
13
phpMyAdmin Some common types include: INT = Integer (a number without a decimal point) CHAR = Characters (can hold text up to a specific length) VARCHAR = Variable Length Characters (a text field that is not a fixed width). TEXT = For holding large amounts of text. DATE = Can only hold dates. DATETIME = Can hold both a date and a time.
14
phpMyAdmin You should see the name feedback as the name of the table, and simpledata as the name of the database To add data click Insert along the row of tabs
15
phpMyAdmin Add some values, e.g. Click Go
16
phpMyAdmin Set one of the rows as a primary key. Click Primary on the row name This sets the name as the primary key. The primary key of a table uniquely identifies each record in the table. Every database table should have one or more columns designated as the primary key. When click, the primary key looks greyed out
17
phpMyAdmin Click Browse to view the data in the table:
18
The next step can be done in Dreamweaver Accessing the database using PHP
19
Before we can access data in a database, we must open a connection to the MySQL server. In PHP, this is done with the mysqli_connect() function. Syntax: mysqli_connect(host,username,password,dbname); The details you are using (from our initial plan): mysqli_connect(“locahost”,”root”,””,”simpledata”); simpledata
20
Accessing the database using PHP Create a new PHP file, e.g. in Dreamweaver. Enter some PHP script. The following opens the connection to the server and database simpledata.php <?php // Create connection $con=mysqli_connect("localhost","root","","simpledata"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: ". mysqli_connect_error(); } ?> An If statement checks for any errors and displays an error message if the connection fails. No error messages mean it works!
21
Accessing the database using PHP PHP script created in Dreamweaver.
22
Presenting a database using PHP The SELECT statement is used to select data from a database: Add the following PHP <?php $result = mysqli_query($con,"SELECT * FROM feedback"); while($row = mysqli_fetch_array($result)) { echo $row['name']. " ". $row['rating']; echo " "; } mysqli_close($con); ?> The data from the table feedback is returned by the mysqli_query() function and stored in the $result variable. The mysqli_fetch_array() function returns the first row from the table as an array variable &row. The while loop allows mysqli_fetch_array() to return the next row in the recordset. The while loop loops through all the rows. To echo the value of each row, we use the PHP $row array variable ($row['FirstName'] and $row['LastName']).
23
Presenting a database using PHP The source code and live view will look similar to this
24
Preview web page in browser You have used MySQL (Structured Query Language) with PHP to access a database. If you open the page in a browser and view source (right click the web page) you should see something like this: Notice your code entered in Dreamweaver is not shown to the end user This helps with security, as the database details are not shown
25
Add HTML tags (elements) When viewing source the HTML is very basic. Try adding HTML elements (tags), e.g.,,,, etc. \n is an escape character and allows line breaks in the code
26
Edit Database Add new records to the database in PHPmyAdmin Test the output in Dreamweaver
27
Edit Database Click Browse to see added rows:
28
Back to Dreamweaver Refresh the page in Dreamweaver
29
Editing MySQL You can change the PHP and edit the MySQL code to be more selective in the process. For more info on MySQL, check http://www.w3schools.com/php/php_mysql_intro.asp http://www.w3schools.com/php/php_mysql_intro.asp E.g. to select from database only records where the name is “Sam Lowry” change the line $result = mysqli_query($con,"SELECT * FROM feedback"); To $result = mysqli_query($con,"SELECT * FROM feedback WHERE name=‘Sam Lowry'");
30
Editing MySQL
31
MySQL This is the basics of using MySQL. Many websites use MySQL, including Google search engine. MySQL is freely downloadable, light and powerful, and simple to use once you know it more. Like PHP there are a lot of help forums and web pages providing support and guidance. Many Content Management Systems (CMS), e.g. WordPress, use PHP with MySQL Try finding out more about MySQL. Where else is it used?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.