+ All Categories
Home > Technology > Introtodatabase 1

Introtodatabase 1

Date post: 16-Apr-2017
Category:
Upload: digital-insights-digital-marketing-agency
View: 1,755 times
Download: 0 times
Share this document with a friend
29
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
Transcript
Page 1: Introtodatabase 1

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

Page 2: Introtodatabase 1

Download and install mysql gui• Download from herehttp://dev.mysql.com/

downloads/gui-tools/5.0.html

Page 4: Introtodatabase 1

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

Page 5: Introtodatabase 1

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

Page 6: Introtodatabase 1

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

Page 7: Introtodatabase 1

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

Page 8: Introtodatabase 1

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

Page 9: Introtodatabase 1

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

Page 10: Introtodatabase 1

Connecting to MySQL

• Load up MYSQL ADMIN• Server is localhost• Username is root• Port is 3306

Page 11: Introtodatabase 1

Creating a database in MySQL

• Click on catalogues in the list• And right click inside schemas to create a new

database

Page 12: Introtodatabase 1

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

Page 13: Introtodatabase 1

Adding data in MySQL• Right click the new table and choose EDIT TABLE DATA, this

opens the table editor

Page 14: Introtodatabase 1

Adding data in MySQL• To enter data click the edit button and enter details into

the fields. The ID key should autoincrement

Page 15: Introtodatabase 1

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

Page 16: Introtodatabase 1

MySQL Query to insert data

INSERT INTO contacts (CFName, CSName, CAge) values ('Big' ,'tom', 23);

Page 17: Introtodatabase 1

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.

Page 18: Introtodatabase 1

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;

Page 19: Introtodatabase 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);

Page 20: Introtodatabase 1

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);

?>

Page 21: Introtodatabase 1

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);?>

Page 22: Introtodatabase 1

Connecting to a Database through PHP

<?phpinclude 'DBconfig.php';include 'DBopen.php';

// ... do something like insert or select, etc

?>

Page 23: Introtodatabase 1

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';?>

Page 24: Introtodatabase 1

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.

Page 25: Introtodatabase 1

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';

?>

Page 26: Introtodatabase 1

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>

Page 27: Introtodatabase 1

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)

?>

Page 28: Introtodatabase 1

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);

?>

Page 29: Introtodatabase 1

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);

?>


Recommended