Database Basics

1,311 views

Published on

Learn the basics for connecting to a database and running queries.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,311
On SlideShare
0
From Embeds
0
Number of Embeds
34
Actions
Shares
0
Downloads
40
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Database Basics

  1. 1. Database BasicsThe power behind your web application
  2. 2. OverviewObjectiveLearn the basics of connecting to a database andexecuting sql queries ProdigyView.Requirements A Mysql, Postrgesql or MSSQL databaseEstimated Time10 Minutes
  3. 3. 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
  4. 4. 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
  5. 5. 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
  6. 6. 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.
  7. 7. 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
  8. 8. 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.
  9. 9. 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
  10. 10. 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
  11. 11. 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
  12. 12. 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.
  13. 13. 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
  14. 14. 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.
  15. 15. 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.
  16. 16. 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
  17. 17. 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();
  18. 18. 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

×