1Webpage to a Database_did_mat1_1 variant

  • pptx
  • 09.05.2020
Публикация на сайте для учителей

Публикация педагогических разработок

Бесплатное участие. Свидетельство автора сразу.
Мгновенные 10 документов в портфолио.

Иконка файла материала 1Webpage to a Database_did_mat1_1 variant.pptx

Linking a Webpage to a Database

10.3.3.3 link a webpage to a database

Database

Structured collection of data.
Tables
Fields
Query
Reports
Essentially a much more sophisticated implementation of the flat files.

Relational Database

Stores data in separate tables instead of a single store.
Relationships between tables are set
In theory, this provides a faster, more flexible database system.


Example

We wish to maintain a database of student names, IDs, addresses, and any other information.

Will be updated frequently with new names and information.

Will want to retrieve data based on some predicate.
e.g, ‘give me the names of all Massey students who live in Albany’.

Will want to update database with new information about students, not previously recorded.
e.g., may decide we want to include IRD nos.
Very difficult to manage using ‘flat file’ systems

MySQL Database

world's most popular open source database because of its consistent fast performance, high reliability and ease of use
Open Source License:- free
GNU General Public License
Free to modify and distribute but all modification must be available in source code format
Commercial:- not free
Fully paid up professional support
used by Google, Facebook Nokia, YouTube, Yahoo!, Alcatel-Lucent, Zappos.com, etc.

Database Management System

Manages the storage and retrieval of data to and from the database and hides the complexity of what is actually going on from the user.

Database

User

Database Management Ssytem

MySQL is a relational database management system

Client

Web browser

Web server

HTML

Server

MySQL

Operating System

PHP
interpreter

Internet

My codes

HTTP

TCP/IP

Webserver supports HTTP.

Server: responds

phpMyAdmin

MySQL can be controlled through a simple command-line interface; however, we can use phpMyAdmin as an interface to MySQL.

phpMyAdmin is a very powerful tool; it provides a large number of facilities for customising a database management system.

Connecting to a MySQL DBMS

In order for our PHP script to access a database we need to form a connection from the script to the database management system.

resourceId = mysql_connect(server, username, password);

Server is the DBMS server
username is your username
password is your password

Connecting to a MySQL DBMS

In order for our PHP script to access a database we need to form a connection from the script to the database management system.

resourceId = mysql_connect(server, username, password);

The function returns a resource-identifier type.
a PHP script can connect to a DBMS anywhere in the world,
so long as it is connected to the internet.
we can also connect to multiple DBMS at the same time.

Selecting a database

Once connected to a DBMS, we can select a database.

mysql_select_db(databasename, resourceId);

the resourceId is the one returned by mysql_connect()
the function returns true if the selection succeeded; false, otherwise.

Example: Connect to a DBMS and access database

$dbLocalhost = mysql_connect("localhost", "root", "")
or die("Could not connect: " . mysql_error());
mysql_select_db("glassesrus", $dbLocalhost)
or die("Could not find database: " . mysql_error());
echo "

Connected To Database

";
?>

die() stops execution of script if the database connection attempt failed.
mysql_error() returns an error message from the previous MYSQL operation.

Reading from a database

We can now send an SQL query to the database to retrieve some data records.

resourceRecords = mysql_query(query, resourceId);

the resourceId is the one returned by mysql_connect()
the function returns a resource identifier to the returned data.

Example: Connect to a DBMS, access database, send query

$dbLocalhost = mysql_connect("localhost", "root", "")
or die("Could not connect: " . mysql_error());
mysql_select_db("glassesrus", $dbLocalhost)
or die("Could not find database: " . mysql_error());
$dbRecords = mysql_query("SELECT * FROM customers", $dbLocalhost)
or die("Problem reading table: " . mysql_error());
echo "

Connected To Database

";
?>

the function will return a resource pointer (not the actual data) to all the records that match the query.
If all goes well, this script will output nothing on screen.

Example: Connect to a DBMS, access database, send query

$dbLocalhost = mysql_connect("localhost", "root", "")
or die("Could not connect: " . mysql_error());
mysql_select_db("glassesrus", $dbLocalhost)
or die("Could not find database: " . mysql_error());
$dbRecords = mysql_query("SELECT * FROM customers", $dbLocalhost)
or die("Problem reading table: " . mysql_error());
$strSurname = mysql_result($dbRecords, 0, "Surname");
echo "

$strSurname

";
?>

the function will return a resource pointer (not the actual data) to all the records that match the query.
If all goes well, this script will output a surname on screen.