Database BasicsThe power behind your web application
OverviewObjectiveLearn the basics of connecting to a database andexecuting sql queries ProdigyView.Requirements A Mysql, Postrgesql or MSSQL databaseEstimated Time10 Minutes
Follow Along With Code Example1. Download a copy of the example code at www.prodigyview.com/source.2. Install the system in an environment you feel comfortable testing in.3. Proceed to examples/database/Database.php
A Brief Note About The DatabaseEven though you can write a complete application inProdigyView without using the database, it is more than likelythat you are going to need the database at some point. Thegreat feature that PVDatabase has is that it can connect toMySQL, PostgreSQL and MSSQL. In this tutorial we aregoing to learn: Connecting to a database Executing A Query Iterating Through ResultsCurrent development is being done for MongoDB andSQLITE
Disabling the DatabaseIf we are going to learn about truly manipulating thedatabase, let’s first boot ProdigyView in a way thatdisables the database. Turn the database initialization offand loading the plug-ins off since they require a databaseconnection.Boot System Disable Options
Set a databaseGood, now with the database not initialized, we are goingto configure a connection. Similar to the databaseconfiguration file, we are putting our database variables inan array.
Time to ConnectIn our last slide, we created configuration paramters for adatabase we have access too. Next we are going to addthe connection to PVDatabase and then connect to it. Name of the connection Connection Parameters Connect to a specified database. Use to switch between connections
Create a tableWe are going to need a table to test with, so now letscreate one. The process of creating tables is in anothertutorial but we will give you a glimpse of it.
Sanitizing Data When a user is inputting data, it is a general rule NOT to trust that data because it might contain malicious code that can wreak havoc on your database. Before inputting any information into your database through a query, sanitize it through makeSafe() function.Data after it is sanitized Data to be sanitized Data to be sanitized
Executing1. We have our connection set2. We have a table and columns3. We sanitized our information4. We are ready for executing our first query. Insert query with our sanitized data Executes any query
Again but get the id Great, we now know how to insert data into the database. But what if we want the id of the last entry? No problem. Let’s run our example again except this time we are going to execute using the return_last_insert_query(); The generated idThe column that contains the auto incrementing id The table to return the column from
A Closer LookThe method return_last_insert_query(); takes the queryand to additional arguments. The arguments are thecolumn to be returned and the table to return the columnfrom.These options are here because some databases requirethat there is a specified column or table to return the lastgenerated id. If you want to make your applicationdatabase agnostic, it is suggested that theseparameters be used.
Find Results – Row CountPVDatabase::query executes any query so it can beused for creating, updating, finding and deleting. But howto we get the results that are found?Not a problem. Let’s create a search querying and get rowcount. Count the rows in the result
Find Results – Row IterationThat query in the previous slide will find the rows that wehad previously inputted. Use the same $result with themethod PVDatabase::fetchArray() to return an array offound data to iterate through.
Challenge!Ok so we have the database basics down. So here is yourchallenge, it two parts1. Add connections to multiple database and switch between those connections2. Execute a select query on a database but use fetchFields() instead of fetchArray.
The Not So Obvious1. The function setConnection will close the connection to the current database and open a connection to a new one.2. All the database methods are designed to automatically work with the set database3. Other methods in ProdigyView, such as when creating content, will automatically sanitize data for you.4. PVDatabase::makeSafe can also sanitize arrays
Summary1. Add a connection by using PVDatabase::addConnection()2. Connection to a database using PVDatabase::setConnection()3. Sanitize data before inserting into the database by using PVDatabase::makeSafe()4. Execute a query using PVDatabase::query();5. Fetch results by using PVDatabase::fetchArray();
API ReferenceFor a better understanding of the database, visit the apiby clicking on the link below.PVDatabase More TutorialsFor more tutorials, please visit:http://www.prodigyview.com/tutorials www.prodigyview.com