introtodatabase 1
TRANSCRIPT
Overview
• Download MYSQL GUI• What is a database, creating data• Creating a database in MySQL• SQL scripts• Connecting to a database in PHP• Viewing records
Download and install mysql gui• Download from herehttp://dev.mysql.com/
downloads/gui-tools/5.0.html
What is a database• About.comhttp://databases.about.com/od/specificproducts/a/whatisadatabase.htm
• Wikipediahttp://en.wikipedia.org/wiki/Database
• Tutorial on a databasehttp://www.htmlgoodies.com/beyond/reference/article.php/3472691• Mysql tutorialhttp://www.w3schools.com/php/php_mysql_intro.asp
What is a data
First Name Surname Gender County date of birth
John Murphy Male dublin 31/05/1972
John O'Sullivan Male dublin 01/05/1978
David Meagher Male meath 11/11/1977
Sheila Griffin Female meath 13/07/1980
Richard Clarke Male kildare
Aidan Connolly Male kildare 01/01/1987
Tristan Couper Male kildare
What is a database
id First Name Surname Gender County date of birth
1 John Murphy Male dublin 31/05/1972
2 John O'Sullivan Male dublin 01/05/1978
3 David Meagher Male meath 11/11/1977
4 Sheila Griffin Female meath 13/07/1980
5 Richard Clarke Male kildare
6 Aidan Connolly Male kildare 01/01/1987
7 Tristan Couper Male kildare
Address table
What are data types
integer varchar varchar varchar varchar date
1 John Murphy Male dublin 31/05/1972
2 John O'Sullivan Male dublin 01/05/1978
3 David Meagher Male meath 11/11/1977
4 Sheila Griffin Female meath 13/07/1980
5 Richard Clarke Male kildare
6 Aidan Connolly Male kildare 01/01/1987
7 Tristan Couper Male kildare
Address table
What is data normalization
id First Name Surname Gender County date of birth
1 John Murphy Male dublin 31/05/1972
2 John O'Sullivan Male dublin 01/05/1978
3 David Meagher Male meath 11/11/1977
4 Sheila Griffin Female meath 13/07/1980
5 Richard Clarke Male kildare
6 Aidan Connolly Male kildare 01/01/1987
7 Tristan Couper Male kildare
Address table
What is data normalization
id First Name Surname Gender Countydate of
birth
1 John Murphy Male dublin 31/05/1972
2 John O'Sullivan Male dublin 01/05/1978
3 David Meagher Male meath 11/11/1977
4 Sheila Griffin Female meath 13/07/1980
5 Richard Clarke Male kildare
6 Aidan Connolly Male kildare 01/01/1987
7 Tristan Couper Male kildare
Countydublin
kildare
meath
Address tablecounty table
What is data normalization
id First Name Surname Gender CIddate of
birth
1 John Murphy Male 1 31/05/1972
2 John O'Sullivan Male 1 01/05/1978
3 David Meagher Male 3 11/11/1977
4 Sheila Griffin Female 3 13/07/1980
5 Richard Clarke Male 2
6 Aidan Connolly Male 2 01/01/1987
7 Tristan Couper Male 2
Address tablecounty table
Cid County
1 dublin
2 kildare
3 meath
Connecting to MySQL
• Load up MYSQL ADMIN• Server is localhost• Username is root• Port is 3306
Creating a database in MySQL
• Click on catalogues in the list• And right click inside schemas to create a new
database
Creating a table in MySQL• Create a new
table called contacts with the following information
• ID is always aprimary key
• Should be set to autoincrement
Adding data in MySQL• Right click the new table and choose EDIT TABLE DATA, this
opens the table editor
Adding data in MySQL• To enter data click the edit button and enter details into
the fields. The ID key should autoincrement
MySQL Query to select dataSELECT * FROM contacts;SELECT CFName FROM contacts;SELECT * FROM contacts WHERE id = 4;SELECT * FROM contacts ORDER BY CFName;Operator Description= Equal!= Not equal> Greater than< Less than>= Greater than or equal<= Less than or equalBETWEEN Between an inclusive rangeLIKE Search for a pattern – use % for wild card characters
- use single quotes for strings, no quotes for numbers
MySQL Query to insert data
INSERT INTO contacts (CFName, CSName, CAge) values ('Big' ,'tom', 23);
MySQL Query to update data
The SQL statement UPDATE is used to change the value in a table. For example to change CFName from big to small
UPDATE contacts SET CFName = 'small' WHERE CFName = 'big';
Note: SQL statements are not case sensitive, e.g. WHERE is the same as where.
MySQL Query to Delete data
• The DELETE statement deletes rows from a table that satisfy the condition given by the WHERE clause. – For example to delete a record from contact table
where id equals 1
DELETE FROM contacts WHERE CId = 1;
Connecting to a Database through PHP
Opening a connection to MySQL from PHP is easy
Just use the mysql_connect() function like this
mysql_connect(servername,username,password);
Connecting to a Database through PHP
<?php$dbhost = 'localhost';$dbuser = 'root';$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql:'. mysql_error());
$dbname = 'dynamicweb';mysql_select_db($dbname);
?>
Connecting to a Database through PHP
It's a common practice to place the routine of opening a database connection in a separate file. Then every time you want to open a connection just include the file. Usually the host, user, password and database name are also separated in a configuration file.
<?php// This is an example of dbconfig.php$dbhost = 'localhost';$dbuser = 'root';$dbpass = 'password';$dbname = 'myDBname';?>
<?php// This is an example dbopen.php$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');mysql_select_db($dbname);?>
Connecting to a Database through PHP
<?phpinclude 'DBconfig.php';include 'DBopen.php';
// ... do something like insert or select, etc
?>
Closing a Database through PHP
<?php// an example of closedb.php// it does nothing but closing// a mysql database connectionmysql_close($conn);?>
<?phpinclude 'config.php';include 'opendb.php';// ... do something like // insert or select, etcinclude 'dbclose.php';?>
Listing data in a database using PHP
To get PHP to execute MySQL statements we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.
e.g.: Using PHP you can run a MySQL SELECT query to fetch the data out of the database.
You have several options in fetching information from MySQL. PHP provide several functions for this.
The first one is mysql_fetch_array() which fetches a result row as an associative array, a numeric array, or both.
Below is an example of fetching data from MySQL, the table contacts has three columns; cFname, cLname and cAge.
Listing data in a database using PHP
<?phpinclude 'DBconfig.php';include 'DBopen.php';$query = "SELECT cFname, cLname, cAge FROM contacts";$result = mysql_query($query);while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo "First Name: $row[Fname] <br>"."Second Name: $row[Lname] <br>"."Age: $row[age] <br><br>";
}include 'DBclose.php';
?>
Insert Data from a Form into a Database
<html> <body>
<form action="insert.php" method="post"> Firstname: <input type="text" name="firstname" />Lastname: <input type="text" name="lastname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form>
</body> </html>
Insert Data from a Form into a Database
<?php //php file “insert.php”$con = mysql_connect("localhost","peter","abc123"); if (!$con)
{ die('Could not connect: ' . mysql_error()); }
mysql_select_db("my_db", $con);$sql="INSERT INTO table_name (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }
echo "1 record added";mysql_close($con)
?>
Select Data From a Database Table with php
<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con)
{ die('Could not connect: ' . mysql_error()); }mysql_select_db("my_db", $con);$result = mysql_query("SELECT * FROM table_name");while($row = mysql_fetch_array($result)) {
echo $row['FirstName'] . " " . $row['LastName']; echo "<br />";
}mysql_close($con);
?>
Display the Result in an HTML Table
<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM table_name"); echo "<table border='1'> <tr> <th>Firstname</th><th>Lastname</th></tr>";while($row = mysql_fetch_assoc($result)) { echo "<tr>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "</tr>"; } echo "</table>";mysql_close($con);
?>