SlideShare a Scribd company logo
1 of 12
Integration: MySocial
    Database Queries
Interdisciplinary Web Development
              CGS2835
database_queries.php
• Contains many php functions which query the
  database for information.
• This data is sometimes “returned” with return
  to where the function was called
• Other times, this data is simply echoed from
  database_queries.php



                    CGS2835 WebDev
MySocial database
• What information can we ask about this data?




                    CGS2835 WebDev
Get data for a user
• We can ask the database columns of
  information for a particular userID
database_get_username($userID)
===== In MODEL database_queries.php =====
function database_get_username($userID)
{
    $userID = sanitize_input($userID);
    $data = mysql_query("SELECT username FROM users WHERE userID='$userID'");
    $row = mysql_fetch_array($data);
    $result = $row['username'];
    return $result;
}


===== “Calling” the function, printing the data on the VIEW (user.php) =====
$username = database_get_username($userID);
echo $username;


                                 CGS2835 WebDev
Insert posts for a user
• The VIEW user.php has a form that displays if
  $userID is the same as the $loggedInUser
• This form collects a post and sends it to the
  controller post_process.php
Calling database_add_user_post($userID, $message)
== In post_process.php (Controller, processing the data)==
// Get the message to post
$post = $_POST["post"];

// Get the user logged in
$userID = get_user_logged_in();

// Insert the new post into the database for that user
// call the database_add_user_post function and provide the variables
// $userID and $post
database_add_user_post($userID, $post);

// Go back to the user's page
header('Location: user.php?userID=' . $userID);
                                  CGS2835 WebDev
database_add_user_post($userID, $message)

== In database_queries.php (Model inserting the data) ==

function database_add_user_post($userID, $message) {
   // Sanitize the variables $userID and $message
   $userID = sanitize_input($userID);
   $message = sanitize_input($message);
   // Insert the data (userID, message) into the posts table
   $q = "INSERT INTO posts (userID, message) VALUES ('$userID',
   '$message')";
   mysql_query($q);
}



                                CGS2835 WebDev
Select posts for a user
• With post data inserted for a user, we can
  select the post data out to display it.
• This will require a VIEW (echo data) and
  MODEL (select data)
database_get_user_posts($userID)

===== In VIEW user.php =====
$posts = database_get_user_posts($userID);
<h2>Posts:</h2>
<div id = "posts_all">
<?php echo($posts); ?>
</div>
database_get_user_posts($userID)
===== In database_queries.php =====
// Get all of the posts for a userID
function database_get_user_posts($userID)
{
     $userID = sanitize_input($userID);
     $posts = "";
     $q = "SELECT message,timestamp FROM posts WHERE userID='$userID' ORDER BY
    timestamp DESC";
     $result = mysql_query($q);
     while($row = mysql_fetch_array($result))
     {
          $message = stripslashes($row['message']);
          $timestamp = $row['timestamp'];
          $posts = $posts . $timestamp . ": " . $message . "<br />";
     }
     return $posts;
}
Many more queries
• There are many more interactions in the
  database in database_queries.php
• Later topics:
  – password hashing, database security.
  – Designing the layout
  – Adding functionality

More Related Content

What's hot

Flask 소수전공 강의자료 - 3차시
Flask 소수전공 강의자료 - 3차시Flask 소수전공 강의자료 - 3차시
Flask 소수전공 강의자료 - 3차시Junha Jang
 
Flask 소수전공 강의자료 - 4차시
Flask 소수전공 강의자료 - 4차시Flask 소수전공 강의자료 - 4차시
Flask 소수전공 강의자료 - 4차시Junha Jang
 
Magento2&amp;java script (2)
Magento2&amp;java script (2)Magento2&amp;java script (2)
Magento2&amp;java script (2)EvgeniyKapelko1
 
Special Events: Beyond Custom Events
Special Events: Beyond Custom EventsSpecial Events: Beyond Custom Events
Special Events: Beyond Custom EventsBrandon Aaron
 
Agile Data concept introduction
Agile Data   concept introductionAgile Data   concept introduction
Agile Data concept introductionRomans Malinovskis
 
Big Data for each one of us
Big Data for each one of usBig Data for each one of us
Big Data for each one of usOSCON Byrum
 
Windows 8 Camp Montreal - 2012-04-10 - Integrating the Windows 8 experience w...
Windows 8 Camp Montreal - 2012-04-10 - Integrating the Windows 8 experience w...Windows 8 Camp Montreal - 2012-04-10 - Integrating the Windows 8 experience w...
Windows 8 Camp Montreal - 2012-04-10 - Integrating the Windows 8 experience w...Frédéric Harper
 
WooCommerce CRUD and Data Store by Akeda Bagus
WooCommerce CRUD and Data Store by Akeda BagusWooCommerce CRUD and Data Store by Akeda Bagus
WooCommerce CRUD and Data Store by Akeda BagusWordCamp Indonesia
 
Integrating With The Windows 8 Experiences
Integrating With The Windows 8 ExperiencesIntegrating With The Windows 8 Experiences
Integrating With The Windows 8 ExperiencesRami Sarieddine
 
Drupal sins 2016 10-06
Drupal sins 2016 10-06Drupal sins 2016 10-06
Drupal sins 2016 10-06Aaron Crosman
 
Sins Against Drupal 2
Sins Against Drupal 2Sins Against Drupal 2
Sins Against Drupal 2Aaron Crosman
 
Building complex User Interfaces with Sitecore and React
Building complex User Interfaces with Sitecore and ReactBuilding complex User Interfaces with Sitecore and React
Building complex User Interfaces with Sitecore and ReactJonne Kats
 
Self join in active record association
Self join in active record associationSelf join in active record association
Self join in active record associationRORLAB
 
Running ms sql stored procedures in mule
Running ms sql stored procedures in muleRunning ms sql stored procedures in mule
Running ms sql stored procedures in muleAnilKumar Etagowni
 
Immutability
ImmutabilityImmutability
Immutabilitybyanjati
 

What's hot (20)

Flask 소수전공 강의자료 - 3차시
Flask 소수전공 강의자료 - 3차시Flask 소수전공 강의자료 - 3차시
Flask 소수전공 강의자료 - 3차시
 
Flask 소수전공 강의자료 - 4차시
Flask 소수전공 강의자료 - 4차시Flask 소수전공 강의자료 - 4차시
Flask 소수전공 강의자료 - 4차시
 
Magento2&amp;java script (2)
Magento2&amp;java script (2)Magento2&amp;java script (2)
Magento2&amp;java script (2)
 
Special Events: Beyond Custom Events
Special Events: Beyond Custom EventsSpecial Events: Beyond Custom Events
Special Events: Beyond Custom Events
 
Agile Data concept introduction
Agile Data   concept introductionAgile Data   concept introduction
Agile Data concept introduction
 
Code Igniter 2
Code Igniter 2Code Igniter 2
Code Igniter 2
 
Big Data for each one of us
Big Data for each one of usBig Data for each one of us
Big Data for each one of us
 
Windows 8 Camp Montreal - 2012-04-10 - Integrating the Windows 8 experience w...
Windows 8 Camp Montreal - 2012-04-10 - Integrating the Windows 8 experience w...Windows 8 Camp Montreal - 2012-04-10 - Integrating the Windows 8 experience w...
Windows 8 Camp Montreal - 2012-04-10 - Integrating the Windows 8 experience w...
 
Special Events
Special EventsSpecial Events
Special Events
 
Hands On React
Hands On React Hands On React
Hands On React
 
WooCommerce CRUD and Data Store by Akeda Bagus
WooCommerce CRUD and Data Store by Akeda BagusWooCommerce CRUD and Data Store by Akeda Bagus
WooCommerce CRUD and Data Store by Akeda Bagus
 
Integrating With The Windows 8 Experiences
Integrating With The Windows 8 ExperiencesIntegrating With The Windows 8 Experiences
Integrating With The Windows 8 Experiences
 
Drupal sins 2016 10-06
Drupal sins 2016 10-06Drupal sins 2016 10-06
Drupal sins 2016 10-06
 
Sins Against Drupal 2
Sins Against Drupal 2Sins Against Drupal 2
Sins Against Drupal 2
 
Bacbkone js
Bacbkone jsBacbkone js
Bacbkone js
 
Building complex User Interfaces with Sitecore and React
Building complex User Interfaces with Sitecore and ReactBuilding complex User Interfaces with Sitecore and React
Building complex User Interfaces with Sitecore and React
 
Self join in active record association
Self join in active record associationSelf join in active record association
Self join in active record association
 
Running ms sql stored procedures in mule
Running ms sql stored procedures in muleRunning ms sql stored procedures in mule
Running ms sql stored procedures in mule
 
Immutability
ImmutabilityImmutability
Immutability
 
J querypractice
J querypracticeJ querypractice
J querypractice
 

Viewers also liked (9)

Webdev
WebdevWebdev
Webdev
 
01 intro tousingjava
01 intro tousingjava01 intro tousingjava
01 intro tousingjava
 
Mysocial databasequeries
Mysocial databasequeriesMysocial databasequeries
Mysocial databasequeries
 
Xhtml
XhtmlXhtml
Xhtml
 
CGS2835 HTML5
CGS2835 HTML5CGS2835 HTML5
CGS2835 HTML5
 
Web architecture
Web architectureWeb architecture
Web architecture
 
CGS2835 HTML5
CGS2835 HTML5CGS2835 HTML5
CGS2835 HTML5
 
Database basics
Database basicsDatabase basics
Database basics
 
Web architecture v3
Web architecture v3Web architecture v3
Web architecture v3
 

Similar to Mysocial databasequeries

Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For BeginnersJonathan Wage
 
Rapid Prototyping with PEAR
Rapid Prototyping with PEARRapid Prototyping with PEAR
Rapid Prototyping with PEARMarkus Wolff
 
Learn PHP Lacture2
Learn PHP Lacture2Learn PHP Lacture2
Learn PHP Lacture2ADARSH BHATT
 
Moodle 3.3 - API Change Overview #mootieuk17
Moodle 3.3 - API Change Overview #mootieuk17Moodle 3.3 - API Change Overview #mootieuk17
Moodle 3.3 - API Change Overview #mootieuk17Dan Poltawski
 
How to Create Login and Registration API in PHP.pdf
How to Create Login and Registration API in PHP.pdfHow to Create Login and Registration API in PHP.pdf
How to Create Login and Registration API in PHP.pdfAppweb Coders
 
Creating a Simple PHP and MySQL-Based Login System
Creating a Simple PHP and MySQL-Based Login SystemCreating a Simple PHP and MySQL-Based Login System
Creating a Simple PHP and MySQL-Based Login SystemAzharul Haque Shohan
 
Bag Of Tricks From Iusethis
Bag Of Tricks From IusethisBag Of Tricks From Iusethis
Bag Of Tricks From IusethisMarcus Ramberg
 
Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Leonardo Proietti
 
Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the TrenchesJonathan Wage
 
Django Class-based views (Slovenian)
Django Class-based views (Slovenian)Django Class-based views (Slovenian)
Django Class-based views (Slovenian)Luka Zakrajšek
 
[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018Adam Tomat
 
Ex[1].3 php db connectivity
Ex[1].3 php db connectivityEx[1].3 php db connectivity
Ex[1].3 php db connectivityMouli Chandira
 
You're Doing it Wrong - WordCamp Orlando
You're Doing it Wrong - WordCamp OrlandoYou're Doing it Wrong - WordCamp Orlando
You're Doing it Wrong - WordCamp OrlandoChris Scott
 
Introduction to Backbone.js for Rails developers
Introduction to Backbone.js for Rails developersIntroduction to Backbone.js for Rails developers
Introduction to Backbone.js for Rails developersAoteaStudios
 

Similar to Mysocial databasequeries (20)

Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
Rapid Prototyping with PEAR
Rapid Prototyping with PEARRapid Prototyping with PEAR
Rapid Prototyping with PEAR
 
RESTful web services
RESTful web servicesRESTful web services
RESTful web services
 
Learn PHP Lacture2
Learn PHP Lacture2Learn PHP Lacture2
Learn PHP Lacture2
 
Moodle 3.3 - API Change Overview #mootieuk17
Moodle 3.3 - API Change Overview #mootieuk17Moodle 3.3 - API Change Overview #mootieuk17
Moodle 3.3 - API Change Overview #mootieuk17
 
How to Create Login and Registration API in PHP.pdf
How to Create Login and Registration API in PHP.pdfHow to Create Login and Registration API in PHP.pdf
How to Create Login and Registration API in PHP.pdf
 
Creating a Simple PHP and MySQL-Based Login System
Creating a Simple PHP and MySQL-Based Login SystemCreating a Simple PHP and MySQL-Based Login System
Creating a Simple PHP and MySQL-Based Login System
 
Framework
FrameworkFramework
Framework
 
Bag Of Tricks From Iusethis
Bag Of Tricks From IusethisBag Of Tricks From Iusethis
Bag Of Tricks From Iusethis
 
Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5
 
Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the Trenches
 
Add loop shortcode
Add loop shortcodeAdd loop shortcode
Add loop shortcode
 
Mysocial
MysocialMysocial
Mysocial
 
Django Class-based views (Slovenian)
Django Class-based views (Slovenian)Django Class-based views (Slovenian)
Django Class-based views (Slovenian)
 
8. vederea inregistrarilor
8. vederea inregistrarilor8. vederea inregistrarilor
8. vederea inregistrarilor
 
[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018[WLDN] Supercharging word press development in 2018
[WLDN] Supercharging word press development in 2018
 
Ex[1].3 php db connectivity
Ex[1].3 php db connectivityEx[1].3 php db connectivity
Ex[1].3 php db connectivity
 
You're Doing it Wrong - WordCamp Orlando
You're Doing it Wrong - WordCamp OrlandoYou're Doing it Wrong - WordCamp Orlando
You're Doing it Wrong - WordCamp Orlando
 
Yii Introduction
Yii IntroductionYii Introduction
Yii Introduction
 
Introduction to Backbone.js for Rails developers
Introduction to Backbone.js for Rails developersIntroduction to Backbone.js for Rails developers
Introduction to Backbone.js for Rails developers
 

More from Program in Interdisciplinary Computing (20)

Phpmysqlcoding
PhpmysqlcodingPhpmysqlcoding
Phpmysqlcoding
 
Sdlc
SdlcSdlc
Sdlc
 
Javascript
JavascriptJavascript
Javascript
 
Javascript
JavascriptJavascript
Javascript
 
Html5
Html5Html5
Html5
 
Frameworks
FrameworksFrameworks
Frameworks
 
Drupal
DrupalDrupal
Drupal
 
Database
DatabaseDatabase
Database
 
Javascript2
Javascript2Javascript2
Javascript2
 
12 abstract classes
12 abstract classes12 abstract classes
12 abstract classes
 
11 polymorphism
11 polymorphism11 polymorphism
11 polymorphism
 
13 interfaces
13 interfaces13 interfaces
13 interfaces
 
15b more gui
15b more gui15b more gui
15b more gui
 
15a gui
15a gui15a gui
15a gui
 
14b exceptions
14b exceptions14b exceptions
14b exceptions
 
14a exceptions
14a exceptions14a exceptions
14a exceptions
 
13 life and scope
13 life and scope13 life and scope
13 life and scope
 
11 interfaces
11 interfaces11 interfaces
11 interfaces
 
10 abstract
10 abstract10 abstract
10 abstract
 
09 polymorphism
09 polymorphism09 polymorphism
09 polymorphism
 

Mysocial databasequeries

  • 1. Integration: MySocial Database Queries Interdisciplinary Web Development CGS2835
  • 2. database_queries.php • Contains many php functions which query the database for information. • This data is sometimes “returned” with return to where the function was called • Other times, this data is simply echoed from database_queries.php CGS2835 WebDev
  • 3. MySocial database • What information can we ask about this data? CGS2835 WebDev
  • 4. Get data for a user • We can ask the database columns of information for a particular userID
  • 5. database_get_username($userID) ===== In MODEL database_queries.php ===== function database_get_username($userID) { $userID = sanitize_input($userID); $data = mysql_query("SELECT username FROM users WHERE userID='$userID'"); $row = mysql_fetch_array($data); $result = $row['username']; return $result; } ===== “Calling” the function, printing the data on the VIEW (user.php) ===== $username = database_get_username($userID); echo $username; CGS2835 WebDev
  • 6. Insert posts for a user • The VIEW user.php has a form that displays if $userID is the same as the $loggedInUser • This form collects a post and sends it to the controller post_process.php
  • 7. Calling database_add_user_post($userID, $message) == In post_process.php (Controller, processing the data)== // Get the message to post $post = $_POST["post"]; // Get the user logged in $userID = get_user_logged_in(); // Insert the new post into the database for that user // call the database_add_user_post function and provide the variables // $userID and $post database_add_user_post($userID, $post); // Go back to the user's page header('Location: user.php?userID=' . $userID); CGS2835 WebDev
  • 8. database_add_user_post($userID, $message) == In database_queries.php (Model inserting the data) == function database_add_user_post($userID, $message) { // Sanitize the variables $userID and $message $userID = sanitize_input($userID); $message = sanitize_input($message); // Insert the data (userID, message) into the posts table $q = "INSERT INTO posts (userID, message) VALUES ('$userID', '$message')"; mysql_query($q); } CGS2835 WebDev
  • 9. Select posts for a user • With post data inserted for a user, we can select the post data out to display it. • This will require a VIEW (echo data) and MODEL (select data)
  • 10. database_get_user_posts($userID) ===== In VIEW user.php ===== $posts = database_get_user_posts($userID); <h2>Posts:</h2> <div id = "posts_all"> <?php echo($posts); ?> </div>
  • 11. database_get_user_posts($userID) ===== In database_queries.php ===== // Get all of the posts for a userID function database_get_user_posts($userID) { $userID = sanitize_input($userID); $posts = ""; $q = "SELECT message,timestamp FROM posts WHERE userID='$userID' ORDER BY timestamp DESC"; $result = mysql_query($q); while($row = mysql_fetch_array($result)) { $message = stripslashes($row['message']); $timestamp = $row['timestamp']; $posts = $posts . $timestamp . ": " . $message . "<br />"; } return $posts; }
  • 12. Many more queries • There are many more interactions in the database in database_queries.php • Later topics: – password hashing, database security. – Designing the layout – Adding functionality