mysql.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. require_once 'connectionConstants.php';
  3. class DominionConnection
  4. {
  5. private $pdo;
  6. public function __construct()
  7. {
  8. try
  9. {
  10. $this->pdo = new PDO(conString, usernane, password);
  11. }
  12. catch (PDOException $pdoe)
  13. {
  14. echo $pdoe->getMessage();
  15. $this->pdo = false;
  16. return false;
  17. }
  18. }
  19. private function exec($query)
  20. {//Generic function to run a query and return an array of results
  21. $result = $this->pdo->query($query);
  22. $resultArray = array();
  23. if ($result != false)
  24. {
  25. foreach ($result as $row)
  26. {
  27. $resultArray[] = $row;
  28. }
  29. }
  30. return $resultArray;
  31. }
  32. public function getRandom($sets="", $where="", $cardCount=10)
  33. {//Get a number of random cards from the database
  34. $array = $this->exec(
  35. "SELECT cards.id FROM
  36. (
  37. (SELECT id, setId FROM kingdomCards k".(strlen($where)>0?" WHERE $where":"").") AS cards
  38. JOIN
  39. (SELECT id, name FROM sets s ".(strlen($sets)>0?" WHERE name IN($sets)":"").") AS sets
  40. ON cards.setId = sets.id)
  41. ORDER BY RAND() LIMIT $cardCount;"
  42. );
  43. for ($i = 0; $i < count($array); $i++)
  44. {$array[$i] = $array[$i][0];}
  45. return $array;
  46. }
  47. public function getCards($where, $kingdom = true, $sets = "")
  48. {//Get card information from the database
  49. return $this->exec(
  50. "SELECT cards.id, cards.name AS cardName, cards.setId, cards.cost, (cards.type&b'10000'=b'10000') AS isVictory, sets.name AS setName FROM
  51. (
  52. (SELECT k.id, k.name, k.setId, k.cost, k.type FROM ".($kingdom?"kingdomCards":"nonKingdom")." k".(strlen($where)>0?" WHERE $where":"").") AS cards
  53. JOIN
  54. (SELECT id, name FROM sets s".(strlen($sets)>0?" WHERE name IN($sets)":"").") AS sets
  55. ON cards.setId = sets.id);"
  56. );
  57. }
  58. public function getIds($names, $kingdom = true)
  59. {//Get card information from the database
  60. $array = $this->exec(
  61. "SELECT id FROM ".($kingdom?"kingdomCards":"nonKingdom")." WHERE name in('".implode("', '", $names)."');"
  62. );
  63. for ($i = 0; $i < count($array); $i++)
  64. {$array[$i] = $array[$i][0];}
  65. return $array;
  66. }
  67. public function getRequired($cards)
  68. {//Get the required cards from the database
  69. $where = "id IN(".implode(", ", $cards).")";
  70. $array = $this->exec(
  71. "SELECT tmpRequired.id FROM
  72. (
  73. (
  74. (SELECT DISTINCT n.id, n.setId FROM nonKingdom n) AS tmpRequired
  75. INNER JOIN
  76. (
  77. (SELECT k.requires1 AS required FROM kingdomCards k WHERE $where AND k.requires1 IS NOT NULL)
  78. UNION
  79. (SELECT k.requires2 AS required FROM kingdomCards k WHERE $where AND k.requires2 IS NOT NULL)
  80. ) AS tmpCards
  81. ON tmpCards.required = tmpRequired.id
  82. )
  83. JOIN
  84. (SELECT id, name FROM sets s) AS tmpSets
  85. ON tmpRequired.setId = tmpSets.id) GROUP BY tmpRequired.id;"
  86. );
  87. for ($i = 0; $i < count($array); $i++)
  88. {$array[$i] = $array[$i][0];}
  89. return $array;
  90. }
  91. }
  92. ?>