Connecting to underlying data sources: Difference between revisions
Mr. MacKenty (talk | contribs) No edit summary |
Mr. MacKenty (talk | contribs) No edit summary |
||
(4 intermediate revisions by the same user not shown) | |||
Line 9: | Line 9: | ||
# The name of the database. | # The name of the database. | ||
As an example, the code below creates a connection to a database: | As an example, the code below creates a connection to a relational database: | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="PHP"> | ||
<?php | <?php | ||
$connect = mysqli_connect("localhost","YOURUSERNAME","YOURPASSWORD","YOURDATABASE"); | $connect = mysqli_connect("localhost","YOURUSERNAME","YOURPASSWORD","YOURDATABASE"); | ||
// | // the code below handles errors | ||
if (mysqli_connect_errno()) | if (mysqli_connect_errno()) | ||
{ | { | ||
Line 23: | Line 22: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
As another example, you can connect to a flat file (or plain text) data source using file open, file read and file write operations<ref>http://php.net/manual/en/function.fopen.php</ref> | |||
<syntaxhighlight lang="PHP"> | |||
<?php | |||
$handle = fopen("c:\\folder\\resource.txt", "r"); | |||
?> | |||
</syntaxhighlight> | |||
When a web page connects to an underlying datasource, it is '''protected access''', meaning a web user cannot see the authentication details being passed to the database. | |||
== Standards == | == Standards == |
Latest revision as of 14:05, 29 June 2019
A webpage can be connected to a data source. A datasource can be a relational database, a flat-file data source (such as a JSON file or an XML file or just a simple plaintext file).
In order to connect to an underlying datasource, we generally require three things:
- The location of the database. In many cases this is localhost but there is NO REQUIREMENT that the database is on the same server as the webpage.
- The credentials to authenticate (which ois usually a username and password)
- The name of the database.
As an example, the code below creates a connection to a relational database:
<?php
$connect = mysqli_connect("localhost","YOURUSERNAME","YOURPASSWORD","YOURDATABASE");
// the code below handles errors
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
As another example, you can connect to a flat file (or plain text) data source using file open, file read and file write operations[2]
<?php
$handle = fopen("c:\\folder\\resource.txt", "r");
?>
When a web page connects to an underlying datasource, it is protected access, meaning a web user cannot see the authentication details being passed to the database.
Standards[edit]
These standards are used from the IB Computer Science Subject Guide[3]
- Describe how web pages can be connected to underlying data sources.
References[edit]
- ↑ http://www.flaticon.com/
- ↑ http://php.net/manual/en/function.fopen.php
- ↑ IB Diploma Programme Computer science guide (first examinations 2014). Cardiff, Wales, United Kingdom: International Baccalaureate Organization. January 2012.