code_junkie

How do you embeded your sql queries in php scripts (coding-style)?

Nov 13th, 2011
643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. public function getTasksReport($rmId, $stage, $mmcName) {
  2. $rmCondition = $rmId ? 'mud.manager_id = :rmId' : 'TRUE';
  3. $stageCondition = $stage ? 't.stage_id = :stageId' : 'TRUE';
  4. $mmcCondition = $mmcName ? 'mmcs.username = :mmcName' : 'TRUE';
  5. $sql = "
  6. SELECT
  7. mmcs.id AS mmc_id,
  8. mmcs.username AS mmcname,
  9. mud.band_name AS mmc_name,
  10. t.id AS task_id,
  11. t.name AS task,
  12. t.stage_id AS stage,
  13. t.role_id,
  14. tl.id AS task_log_id,
  15. mr.role,
  16. u.id AS user_id,
  17. u.username AS username,
  18. COALESCE(cud.full_name, bud.band_name) AS user_name,
  19. DATE_FORMAT(tl.completed_on, '%d-%m-%Y %T') AS completed_on,
  20. tl.url AS url,
  21. mud.manager_id AS rm_id
  22. FROM users AS mmcs
  23. INNER JOIN banduserdetails AS mud ON mud.user_id = mmcs.id
  24. LEFT JOIN tasks AS t ON 1
  25. LEFT JOIN task_log AS tl ON tl.task_id = t.id AND tl.mmc_id = mmcs.id
  26. LEFT JOIN mmc_roles AS mr ON mr.id = t.role_id
  27. LEFT JOIN users AS u ON u.id = tl.user_id
  28. LEFT JOIN communityuserdetails AS cud ON cud.user_id = u.id
  29. LEFT JOIN banduserdetails AS bud ON bud.user_id = u.id
  30. WHERE mmcs.user_type = 'mmc'
  31. AND $rmCondition
  32. AND $stageCondition
  33. AND $mmcCondition
  34. ORDER BY mmcs.id, t.stage_id, t.role_id, t.task_order
  35. ";
  36. $pdo = new PDO(.....);
  37. $stmt = $pdo->prepare($sql);
  38. $rmId and $stmt->bindValue('rmId', $rmId); // (1)
  39. $stage and $stmt->bindValue('stageId', $stage); // (2)
  40. $mmcName and $stmt->bindValue('mmcName', $mmcName); // (3)
  41. $stmt->execute();
  42. return $stmt->fetchAll();
  43. }
  44.  
  45. db_exec ("SELECT ...");
  46.  
  47. $SQL = "SELECT ...";
  48. $result = db_exec ($SQL);
  49.  
  50. $weblog = new Weblog(); // create an empty object to work with.
  51. $weblog->Author = 'SchizoDuckie'; // mapped internally to strAuthor.
  52. $weblog->Title = 'A test weblog';
  53. $weblog->Story = 'This is a test weblog!';
  54. $weblog->Posted = date("Y-m-d H:i:s");
  55. $weblog->Save(); // Checks for any changed values and inserts or updates into DB.
  56. echo ($weblog->ID) // outputs: 1
  57.  
  58. $reply = new Reply();
  59. $reply->Author = 'Some random guy';
  60. $reply->Reply = 'w000t';
  61. $reply->Posted = date("Y-m-d H:i:s");
  62. $reply->IP = '127.0.0.1';
  63. $reply->Connect($weblog); // auto-saves $reply and connects it to $weblog->ID
  64.  
  65. $weblog = new Weblog(1); //Fetches the row with primary key 1 from table weblogs and hooks it's values into $weblog;
  66.  
  67. echo("<h1>{$weblog->Title}</h1>
  68. <h3>Posted by {$weblog->Author} @ {$weblog->Posted}</h3>
  69. <div class='weblogpost'>{$weblog->Story}</div>");
  70.  
  71. // now fetch the connected posts. this is the real magic:
  72. $replies = $weblog->Find("Reply"); // fetches a pre-filled array of Reply objects.
  73. if ($replies != false)
  74. {
  75. foreach($replies as $reply)
  76. {
  77. echo("<div class='weblogreply'><h4>By {$reply->Author} @ {$reply->Posted}</h4> {$reply->Reply}</div>");
  78. }
  79. }
  80.  
  81. class Weblog extends dbObject
  82. {
  83. function __construct($ID=false)
  84. {
  85. $this->__setupDatabase('blogs', // database table
  86. array('ID_Blog' => 'ID', // database field => mapped object property
  87. 'strPost' => 'Story', // as you can see, database field strPost is mapped to $this->Story
  88. 'datPosted' => 'Posted',
  89. 'strPoster' => 'Author',
  90. 'strTitle' => 'Title',
  91. 'ipAddress' => 'IpAddress',
  92. 'ID_Blog', // primary table key
  93. $ID); // value of primary key to init with (can be false for new empty object / row)
  94. $this->addRelation('Reaction'); // define a 1:many relation to Reaction
  95.  
  96. }
  97. }
  98.  
  99. $sql = "SELECT tbl1.col1, tbl1.col2, tbl2.col1, tbl2.col2"
  100. . " FROM Table1 tbl1"
  101. . " INNER JOIN Table2 tbl2 ON tbl1.id = tbl2.other_id"
  102. . " WHERE tbl2.id = ?"
  103. . " ORDER BY tbl2.col1, tbl2.col2"
  104. . " LIMIT 10, 0";
  105.  
  106. $sql = (string) View::factory('sql/myfile');
  107.  
  108. $sql = file_get_contents("myfile.sql");
  109.  
  110. $query = "SELECT "
  111. . " foo, "
  112. . " bar "
  113. . "FROM "
  114. . " mytable "
  115. . "WHERE "
  116. . " id = $userid";
Add Comment
Please, Sign In to add comment