SlideShare a Scribd company logo
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

PDF
Migrating to-Drupal-8 by Bryan Manalo
PDF
Migrating Drupal 7 to Drupal 8
DOCX
Php update and delet operation
PPTX
Eden Hackathon Benilde (Mysql & SMTP)
PDF
jQuery secrets
PDF
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
PPTX
Magento Dependency Injection
KEY
Web storage
Migrating to-Drupal-8 by Bryan Manalo
Migrating Drupal 7 to Drupal 8
Php update and delet operation
Eden Hackathon Benilde (Mysql & SMTP)
jQuery secrets
jQuery UI Widgets, Drag and Drop, Drupal 7 Javascript
Magento Dependency Injection
Web storage

What's hot (20)

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

Similar to Mysocial databasequeries (20)

PDF
veracruz
PDF
veracruz
PDF
veracruz
PDF
veracruz
PPT
Download It
PPTX
CVJ531: Intro to MySQL
PPTX
Web Security - Hands-on
PDF
Php summary
PDF
Tutorial_4_PHP
PDF
Tutorial_4_PHP
PDF
Tutorial_4_PHP
PDF
Tutorial_4_PHP
PPTX
This slide show will brief about database handling
PPTX
Learn PHP Lacture2
PPT
Lect_04b_PhpMysqlKEY PERFORMANCE INDICATOR FOR ICT-UNIT (new).ppt
PDF
4.3 MySQL + PHP
PPTX
PHP FUNCTIONS
PPT
06 Php Mysql Connect Query
PPT
Part 2
veracruz
veracruz
veracruz
veracruz
Download It
CVJ531: Intro to MySQL
Web Security - Hands-on
Php summary
Tutorial_4_PHP
Tutorial_4_PHP
Tutorial_4_PHP
Tutorial_4_PHP
This slide show will brief about database handling
Learn PHP Lacture2
Lect_04b_PhpMysqlKEY PERFORMANCE INDICATOR FOR ICT-UNIT (new).ppt
4.3 MySQL + PHP
PHP FUNCTIONS
06 Php Mysql Connect Query
Part 2

More from Program in Interdisciplinary Computing (20)

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