ClarkeRubber

databaseTools.php

Dec 30th, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 20.89 KB | None | 0 0
  1. <?php
  2.  
  3. //-----GAME FUCNTIONS
  4. function findGame($gameInfo, $con = NULL){
  5.     if($con == NULL){
  6.         $con = $GLOBALS['con'];
  7.     }
  8.     //Takes in information about a game and outputs a complete record if it exists
  9.     //If more than one game exists, return a list of matching games
  10.     //If the game does not exist=FALSE
  11.  
  12.     $first = TRUE;
  13.     $query = 'SELECT * FROM Games WHERE ';
  14.  
  15.     $columns = array('id', 'White', 'Black', 'Result', 'Opening', 'Event', 'Location', 'Round', 'Date', 'PGN');
  16.  
  17.     for($x = 0; $x < count($columns); $x++){
  18.         $key = $columns[$x];
  19.  
  20.         if( isset( $gameInfo[ $key ] ) && !empty( $gameInfo[ $key ] ) ){
  21.  
  22.             if($first){
  23.                 $query .= "$key = '".mysqli_real_escape_string( $con, $gameInfo[ $key ] )."'\n";
  24.             }else{
  25.                 $query .= "AND $key ='".mysqli_real_escape_string( $con, $gameInfo[ $key ] )."'\n";
  26.             }
  27.  
  28.             $first = FALSE;
  29.         }
  30.     }
  31.  
  32.     $result = mysqli_query($con, $query) or die( mysqli_error( $con ) );
  33.  
  34.     $output = array();
  35.  
  36.     if($row = mysqli_fetch_array($result)){
  37.         $output = $row;
  38.     }
  39.  
  40.     $output = ( empty( $output ) ? FALSE : $output );
  41.  
  42.     return $output;
  43. }
  44. function createGame($gameInfo, $con = NULL){
  45.     if($con == NULL){
  46.         $con = $GLOBALS['con'];
  47.     }
  48.     //Takes in information about a game and outputs a complete record if the game was created successully
  49.     //if the game failed to create, return FALSE or DIE
  50.  
  51.     /*Minimum information to create game:
  52.         -White
  53.         -Black
  54.         -Result
  55.         -PGN
  56.     */
  57.  
  58.     $White      = ( empty( $gameInfo['White'    ] ) ? ''        : mysqli_real_escape_string( $con, $gameInfo['White'        ] ) );
  59.     $Black      = ( empty( $gameInfo['Black'    ] ) ? ''        : mysqli_real_escape_string( $con, $gameInfo['Black'        ] ) );
  60.     $Result     = ( empty( $gameInfo['Result'   ] ) && ! isset($gameInfo['Result']) ? ''        : mysqli_real_escape_string( $con, $gameInfo['Result'       ] ) );
  61.     $Opening    = ( empty( $gameInfo['Opening'  ] ) ? 'NULL'    : mysqli_real_escape_string( $con, $gameInfo['Opening'      ] ) );
  62.     $Event      = ( empty( $gameInfo['Event'    ] ) ? 'NULL'    : mysqli_real_escape_string( $con, $gameInfo['Event'        ] ) );
  63.     $Location   = ( empty( $gameInfo['Location' ] ) ? 'NULL'    : mysqli_real_escape_string( $con, $gameInfo['Location'     ] ) );
  64.     $Round      = ( empty( $gameInfo['Round'    ] ) ? 'NULL'    : mysqli_real_escape_string( $con, $gameInfo['Round'        ] ) );
  65.     $Date       = ( empty( $gameInfo['Date'     ] ) ? 'NULL'    : mysqli_real_escape_string( $con, $gameInfo['Date'         ] ) );
  66.     $PGN        = ( empty( $gameInfo['PGN'      ] ) ? ''        : mysqli_real_escape_string( $con, $gameInfo['PGN'          ] ) );
  67.  
  68.     $output = FALSE;
  69.  
  70.     if($White != '' && $Black != '' && $Result != '' && $PGN != ''){
  71.         $result = mysqli_query($con, "INSERT INTO Games (White, Black, Result, Opening, Event, Location, Round, Date, PGN)
  72.             VALUES ('$White', '$Black', '$Result', '$Opening', '$Event', '$Location', '$Round', '$Date', '$PGN')") or die( mysqli_error( $con ) );
  73.  
  74.         $id = mysqli_insert_id($con) or die( mysqli_error( $con ) );
  75.  
  76.         $result = mysqli_query($con, "SELECT * FROM Games WHERE id = '$id'") or die( mysqli_error( $con ) );
  77.  
  78.         $output = mysqli_fetch_array($result) or die( mysqli_error( $con ) );
  79.     }
  80.  
  81.     return $output;
  82. }
  83. function updateGame($gameInfo, $con = NULL){
  84.     if($con == NULL){
  85.         $con = $GLOBALS['con'];
  86.     }
  87.     //Game must be identified by it's unique ID
  88.     //ELSE return the updated entry
  89.  
  90.     $columns = array('White', 'Black', 'Result', 'Opening', 'Event', 'Location', 'Round', 'Date', 'PGN');
  91.  
  92.     $id = ( empty( $gameInfo['id'] ) ? 'ESCAPE' : mysqli_real_escape_string( $con, $gameInfo['id'] ) );
  93.  
  94.     if($id == 'ESCAPE'){
  95.         return FALSE;
  96.     }
  97.  
  98.     for($x = 0; $x < count($columns); $x++){
  99.         $column = $columns[ $x ];
  100.         $query = ( empty( $gameInfo[ $columns[ $x ] ] ) ? '' : mysqli_real_escape_string( $con, $gameInfo[ $columns[ $x ] ] ) );
  101.  
  102.         if($query != ''){
  103.             mysqli_query($con, "UPDATE Games SET $column = '$query' WHERE id = '$id'") or die( mysqli_error( $con ) );
  104.         }
  105.     }
  106.  
  107.     $result = mysqli_query($con, "SELECT * FROM Games WHERE id = '$id'") or die( mysqli_error( $con ) );
  108.  
  109.     $output = mysqli_fetch_array($result) or die( mysqli_error( $con ) );
  110.  
  111.     return $output;
  112. }
  113.  
  114. //-----PLAYER FUNCTIONS
  115. function findPlayer($playerInfo, $con = NULL){
  116.     if($con == NULL){
  117.         $con = $GLOBALS['con'];
  118.     }
  119.     //Takes in information about a game and outputs a complete record if it exists
  120.     //If the game does not exist=FALSE
  121.  
  122.     $first = TRUE;
  123.     $query = 'SELECT * FROM Players WHERE ';
  124.  
  125.     $columns = array('id', 'Name', 'ELO', 'FirstGame', 'LastGame', 'Country', 'GamesPlayed');
  126.  
  127.     for($x = 0; $x < count($columns); $x++){
  128.         $key = $columns[$x];
  129.  
  130.         if( isset( $playerInfo[ $key ] ) && !empty( $playerInfo[ $key ] ) ){
  131.  
  132.             if($first){
  133.                 $query .= "$key = '".mysqli_real_escape_string( $con, $playerInfo[ $key ] )."'\n";
  134.             }else{
  135.                 $query .= "AND $key ='".mysqli_real_escape_string( $con, $playerInfo[ $key ] )."'\n";
  136.             }
  137.  
  138.             $first = FALSE;
  139.         }
  140.     }
  141.  
  142.     $result = mysqli_query($con, $query) or die( mysqli_error( $con ) );
  143.  
  144.     $output = array();
  145.  
  146.     if($row = mysqli_fetch_array($result)){
  147.         $output = $row;
  148.     }
  149.  
  150.     $output = ( empty( $output ) ? FALSE : $output );
  151.  
  152.     return $output;
  153. }
  154. function createPlayer($playerInfo, $con = NULL){
  155.     if($con == NULL){
  156.         $con = $GLOBALS['con'];
  157.     }
  158.     //Takes in information about a player and outputs a complete record if the player was created successfully
  159.     //If the player failed to create, return FALSE or DIE
  160.    
  161.  
  162.     $Name       = ( empty( $playerInfo['Name'       ] ) ? ''        : mysqli_real_escape_string( $con, $playerInfo['Name'           ] ) );
  163.     $ELO        = ( empty( $playerInfo['ELO'        ] ) ? '0'       : mysqli_real_escape_string( $con, $playerInfo['ELO'            ] ) );
  164.     $FirstGame  = ( empty( $playerInfo['FirstGame'  ] ) ? '50000101': mysqli_real_escape_string( $con, $playerInfo['FirstGame'      ] ) );
  165.     $LastGame   = ( empty( $playerInfo['LastGame'   ] ) ? '00000101': mysqli_real_escape_string( $con, $playerInfo['LastGame'       ] ) );
  166.     $Country    = ( empty( $playerInfo['Country'    ] ) ? ''        : mysqli_real_escape_string( $con, $playerInfo['Country'        ] ) );
  167.     $GamesPlayed= ( empty( $playerInfo['GamesPlayed'] ) ? '0'       : mysqli_real_escape_string( $con, $playerInfo['GamesPlayed'    ] ) );
  168.  
  169.     $output = FALSE;
  170.  
  171.     if($Name != ''){
  172.         $result = mysqli_query($con, "INSERT INTO Players (Name, ELO, FirstGame, LastGame, Country, GamesPlayed)
  173.             VALUES ('$Name', '$ELO', '$FirstGame', '$LastGame', '$Country', '$GamesPlayed')") or die( mysqli_error( $con ) );
  174.  
  175.         $id = mysqli_insert_id($con);
  176.  
  177.         $result = mysqli_query($con, "SELECT * FROM Players WHERE id = '$id'") or die( mysqli_error( $con ) );
  178.  
  179.         $output = mysqli_fetch_array($result) or die( mysqli_error( $con ) );
  180.     }
  181.  
  182.     return $output;
  183. }
  184. function updatePlayer($playerInfo, $con = NULL){
  185.     if($con == NULL){
  186.         $con = $GLOBALS['con'];
  187.     }
  188.     //Game must be identified by it's unique ID
  189.     //ELSE return the updated entry
  190.  
  191.     $columns = array('id', 'Name', 'ELO', 'FirstGame', 'LastGame', 'Country', 'GamesPlayed');
  192.  
  193.     $id = ( empty( $playerInfo['id'] ) ? 'ESCAPE' : mysqli_real_escape_string( $con, $playerInfo['id'] ) );
  194.  
  195.     if($id == 'ESCAPE'){
  196.         return FALSE;
  197.     }
  198.  
  199.     for($x = 0; $x < count($columns); $x++){
  200.         $column = $columns[ $x ];
  201.         $query = ( empty( $playerInfo[ $columns[ $x ] ] ) ? '' : mysqli_real_escape_string( $con, $playerInfo[ $columns[ $x ] ] ) );
  202.  
  203.         if($query != ''){
  204.             mysqli_query($con, "UPDATE Players SET $column = '$query' WHERE id = '$id'") or die( mysqli_error( $con ) );
  205.         }
  206.     }
  207.  
  208.     $result = mysqli_query($con, "SELECT * FROM Players WHERE id = '$id'") or die( mysqli_error( $con ) );
  209.  
  210.     $output = mysqli_fetch_array($result) or die( mysqli_error( $con ) );
  211.  
  212.     return $output;
  213. }
  214.  
  215. //-----POSITION FUNCTIONS
  216. function findPosition($positionInfo, $con = NULL){
  217.     if($con == NULL){
  218.         $con = $GLOBALS['con'];
  219.     }
  220.     $first = TRUE;
  221.     $query = 'SELECT * FROM Positions WHERE ';
  222.  
  223.     $columns = array('id', 'FEN', 'Side', 'Evaluated', 'Eval', 'EvalTime', 'TimesPlayed', 'WhiteWinRate', 'BlackWinRate', 'DrawRate', 'CoordX', 'CoordY', 'CoordZ');
  224.  
  225.     for($x = 0; $x < count($columns); $x++){
  226.         $key = $columns[$x];
  227.  
  228.         if( isset( $positionInfo[ $key ] ) && !empty( $positionInfo[ $key ] ) ){
  229.  
  230.             if($first){
  231.                 $query .= "$key = '".mysqli_real_escape_string( $con, $positionInfo[ $key ] )."'\n";
  232.             }else{
  233.                 $query .= "AND $key ='".mysqli_real_escape_string( $con, $positionInfo[ $key ] )."'\n";
  234.             }
  235.  
  236.             $first = FALSE;
  237.         }
  238.     }
  239.  
  240.     $result = mysqli_query($con, $query);
  241.  
  242.     $output = array();
  243.  
  244.     if($row = mysqli_fetch_array($result)){
  245.         $output = $row;
  246.     }
  247.  
  248.     $output = ( empty( $output ) ? FALSE : $output );
  249.  
  250.     return $output;
  251. }
  252. function createPosition($positionInfo, $con = NULL){
  253.     if($con == NULL){
  254.         $con = $GLOBALS['con'];
  255.     }
  256.     /*Minimum information required to create a position:
  257.         -FEN
  258.         -Side
  259.     */
  260.     $FEN            = ( empty( $positionInfo['FEN'          ] ) ? ''        : mysqli_real_escape_string( $con, $positionInfo['FEN'          ] ) );
  261.     $Side           = ( empty( $positionInfo['Side'] ) && !isset($positionInfo['Side'] ) ? '' : mysqli_real_escape_string( $con, $positionInfo['Side'] ) );
  262.     $Evaluated      = ( empty( $positionInfo['Evaluated'    ] ) ? '0'       : mysqli_real_escape_string( $con, $positionInfo['Evaluated'    ] ) );
  263.     $Eval           = ( empty( $positionInfo['Eval'         ] ) ? '0'       : mysqli_real_escape_string( $con, $positionInfo['Eval'         ] ) );
  264.     $EvalTime       = ( empty( $positionInfo['EvalTime'     ] ) ? '0'       : mysqli_real_escape_string( $con, $positionInfo['EvalTime'     ] ) );
  265.     $TimesPlayed    = ( empty( $positionInfo['TimesPlayed'  ] ) ? '1'       : mysqli_real_escape_string( $con, $positionInfo['TimesPlayed'  ] ) );
  266.     $WhiteWinRate   = ( empty( $positionInfo['WhiteWinRate' ] ) ? '0'       : mysqli_real_escape_string( $con, $positionInfo['WhiteWinRate' ] ) );
  267.     $BlackWinRate   = ( empty( $positionInfo['BlackWinRate' ] ) ? '0'       : mysqli_real_escape_string( $con, $positionInfo['BlackWinRate' ] ) );
  268.     $DrawRate       = ( empty( $positionInfo['DrawRate'     ] ) ? '0'       : mysqli_real_escape_string( $con, $positionInfo['DrawRate'     ] ) );
  269.     $CoordX         = ( empty( $positionInfo['CoordX'       ] ) ? '0'       : mysqli_real_escape_string( $con, $positionInfo['CoordX'       ] ) );
  270.     $CoordY         = ( empty( $positionInfo['CoordY'       ] ) ? '0'       : mysqli_real_escape_string( $con, $positionInfo['CoordY'       ] ) );
  271.     $CoordZ         = ( empty( $positionInfo['CoordZ'       ] ) ? '0'       : mysqli_real_escape_string( $con, $positionInfo['CoordZ'       ] ) );
  272.  
  273.     $output = FALSE;
  274.  
  275.     if($FEN != '' && $Side != ''){
  276.         $result = mysqli_query($con, "INSERT INTO Positions (FEN, Side, Evaluated, Eval, EvalTime, TimesPlayed, WhiteWinRate, BlackWinRate, DrawRate, CoordX, CoordY, CoordZ)
  277.             VALUES ('$FEN', '$Side', '$Evaluated', '$Eval', '$EvalTime', '$TimesPlayed', '$WhiteWinRate', '$BlackWinRate', '$DrawRate', '$CoordX', '$CoordY', '$CoordZ')") or die( mysqli_error( $con ) );
  278.  
  279.         $id = mysqli_insert_id($con) or die( mysqli_error( $con ) );
  280.  
  281.         $result = mysqli_query($con, "SELECT * FROM Positions WHERE id = '$id'") or die( mysqli_error( $con ) );
  282.  
  283.         $output = mysqli_fetch_array($result) or die( mysqli_error( $con ) );
  284.     }
  285.  
  286.     return $output;
  287. }
  288. function updatePosition($positionInfo, $con = NULL){
  289.     if($con == NULL){
  290.         $con = $GLOBALS['con'];
  291.     }
  292.     $columns = array('FEN', 'Side', 'Evaluated', 'Eval', 'EvalTime', 'TimesPlayed', 'WhiteWinRate', 'BlackWinRate', 'DrawRate', 'CoordX', 'CoordY', 'CoordZ');
  293.  
  294.     $id = ( empty( $positionInfo['id'] ) ? 'ESCAPE' : mysqli_real_escape_string( $con, $positionInfo['id'] ) );
  295.  
  296.     if($id == 'ESCAPE'){
  297.         return FALSE;
  298.     }
  299.  
  300.     for($x = 0; $x < count($columns); $x++){
  301.         $column = $columns[ $x ];
  302.         $query = ( empty( $positionInfo[ $columns[ $x ] ] ) ? '' : mysqli_real_escape_string( $con, $positionInfo[ $columns[ $x ] ] ) );
  303.  
  304.         if($query != ''){
  305.             mysqli_query($con, "UPDATE Positions SET $column = '$query' WHERE id = '$id'") or die( mysqli_error( $con ) );
  306.         }
  307.     }
  308.  
  309.     $result = mysqli_query($con, "SELECT * FROM Positions WHERE id = '$id'") or die( mysqli_error( $con ) );
  310.  
  311.     $output = mysqli_fetch_array($result) or die( mysqli_error( $con ) );
  312.  
  313.     return $output;
  314. }
  315.  
  316. //-----POSITON RELATION FUNCTIONS
  317. function findPositionRelation($relationInfo, $con = NULL){
  318.     if($con == NULL){
  319.         $con = $GLOBALS['con'];
  320.     }
  321.     $first = TRUE;
  322.     $query = 'SELECT * FROM PositionsRelations WHERE ';
  323.  
  324.     $columns = array('id', 'PlayedMove', 'ParentID', 'ChildID', 'TimesPlayed');
  325.  
  326.     for($x = 0; $x < count($columns); $x++){
  327.         $key = $columns[$x];
  328.  
  329.         if( isset( $relationInfo[ $key ] ) && !empty( $relationInfo[ $key ] ) ){
  330.  
  331.             if($first){
  332.                 $query .= "$key = '".mysqli_real_escape_string( $con, $relationInfo[ $key ] )."'\n";
  333.             }else{
  334.                 $query .= "AND $key ='".mysqli_real_escape_string( $con, $relationInfo[ $key ] )."'\n";
  335.             }
  336.  
  337.             $first = FALSE;
  338.         }
  339.     }
  340.  
  341.     $result = mysqli_query($con, $query);
  342.  
  343.     $output = array();
  344.  
  345.     if($row = mysqli_fetch_array($result)){
  346.         $output = $row;
  347.     }
  348.  
  349.     $output = ( empty( $output ) ? FALSE : $output );
  350.  
  351.     return $output;
  352. }
  353. function createPositionRelation($relationInfo, $con = NULL){
  354.     if($con == NULL){
  355.         $con = $GLOBALS['con'];
  356.     }
  357.     /*Minimum information required to create a position relation:
  358.         -PlayedMove
  359.         -ParentID
  360.         -ChildID
  361.     */
  362.     $PlayedMove     = ( empty( $relationInfo['PlayedMove'   ] ) ? ''    : mysqli_real_escape_string( $con, $relationInfo['PlayedMove'   ] ) );
  363.     $ParentID       = ( empty( $relationInfo['ParentID'     ] ) ? ''    : mysqli_real_escape_string( $con, $relationInfo['ParentID' ] ) );
  364.     $ChildID        = ( empty( $relationInfo['ChildID'      ] ) ? ''    : mysqli_real_escape_string( $con, $relationInfo['ChildID'  ] ) );
  365.     $TimesPlayed    = ( empty( $relationInfo['TimesPlayed'  ] ) ? '1'   : mysqli_real_escape_string( $con, $relationInfo['TimesPlayed'] ) );
  366.  
  367.     $output = FALSE;
  368.  
  369.     if($PlayedMove != '' && $ParentID != '' && $ChildID != ''){
  370.         $result = mysqli_query($con, "INSERT INTO PositionsRelations (PlayedMove, ParentID, ChildID, TimesPlayed)
  371.             VALUES ('$PlayedMove', '$ParentID', '$ChildID', '$TimesPlayed')") or die( mysqli_error( $con ) );
  372.  
  373.         $id = mysqli_insert_id($con) or die( mysqli_error( $con ) );
  374.  
  375.         $result = mysqli_query($con, "SELECT * FROM PositionsRelations WHERE id = '$id'") or die( mysqli_error( $con ) );
  376.  
  377.         $output = mysqli_fetch_array($result) or die( mysqli_error( $con ) );
  378.     }
  379.  
  380.     return $output;
  381. }
  382.  
  383. function updatePositionRelation($relationInfo, $con = NULL){
  384.     if($con == NULL){
  385.         $con = $GLOBALS['con'];
  386.     }
  387.     $columns = array('PlayedMove', 'ParentID', 'ChildID', 'TimesPlayed');
  388.  
  389.     $id = ( empty( $relationInfo['id'] ) ? 'ESCAPE' : mysqli_real_escape_string( $con, $relationInfo['id'] ) );
  390.  
  391.     if($id == 'ESCAPE'){
  392.         return FALSE;
  393.     }
  394.  
  395.     for($x = 0; $x < count($columns); $x++){
  396.         $column = $columns[ $x ];
  397.         $query = ( empty( $relationInfo[ $columns[ $x ] ] ) ? '' : mysqli_real_escape_string( $con, $relationInfo[ $columns[ $x ] ] ) );
  398.  
  399.         if($query != ''){
  400.             mysqli_query($con, "UPDATE PositionsRelations SET $column = '$query' WHERE id = '$id'") or die( mysqli_error( $con ) );
  401.         }
  402.     }
  403.  
  404.     $result = mysqli_query($con, "SELECT * FROM PositionsRelations WHERE id = '$id'") or die( mysqli_error( $con ) );
  405.  
  406.     $output = mysqli_fetch_array($result) or die( mysqli_error( $con ) );
  407.  
  408.     return $output;
  409. }
  410.  
  411. //-----GAME RELATION FUNCTIONS
  412. function findGameRelation($relationInfo, $con = NULL){
  413.     if($con == NULL){
  414.         $con = $GLOBALS['con'];
  415.     }
  416.     $first = TRUE;
  417.     $query = 'SELECT * FROM GamePositions WHERE ';
  418.  
  419.     $columns = array('id', 'PlayedMove', 'GameID', 'PositionID');
  420.  
  421.     for($x = 0; $x < count($columns); $x++){
  422.         $key = $columns[$x];
  423.  
  424.         if( isset( $relationInfo[ $key ] ) && !empty( $relationInfo[ $key ] ) ){
  425.  
  426.             if($first){
  427.                 $query .= "$key = '".mysqli_real_escape_string( $con, $relationInfo[ $key ] )."'\n";
  428.             }else{
  429.                 $query .= "AND $key ='".mysqli_real_escape_string( $con, $relationInfo[ $key ] )."'\n";
  430.             }
  431.  
  432.             $first = FALSE;
  433.         }
  434.     }
  435.  
  436.     $result = mysqli_query($con, $query);
  437.  
  438.     $output = array();
  439.  
  440.     if($row = mysqli_fetch_array($result)){
  441.         $output = $row;
  442.     }
  443.  
  444.     $output = ( empty( $output ) ? FALSE : $output );
  445.  
  446.     return $output;
  447. }
  448. function createGameRelation($relationInfo, $con = NULL){
  449.     if($con == NULL){
  450.         $con = $GLOBALS['con'];
  451.     }
  452.     /*Minimum information required to create a position relation:
  453.         -PlayedMove
  454.         -GameID
  455.         -ChildID
  456.     */
  457.     $PlayedMove     = ( empty( $relationInfo['PlayedMove'   ] ) ? ''    : mysqli_real_escape_string( $con, $relationInfo['PlayedMove'   ] ) );
  458.     $GameID         = ( empty( $relationInfo['GameID'       ] ) ? ''    : mysqli_real_escape_string( $con, $relationInfo['GameID'       ] ) );
  459.     $PositionID     = ( empty( $relationInfo['PositionID'   ] ) ? ''    : mysqli_real_escape_string( $con, $relationInfo['PositionID'   ] ) );
  460.  
  461.     $output = FALSE;
  462.  
  463.     if($PlayedMove != '' && $GameID != '' && $PositionID != ''){
  464.         $result = mysqli_query($con, "INSERT INTO GamesPositions (PlayedMove, GameID, PositionID)
  465.             VALUES ('$PlayedMove', '$GameID', '$PositionID')") or die( mysqli_error( $con ) );
  466.  
  467.         $id = mysqli_insert_id($con) or die( mysqli_error( $con ) );
  468.  
  469.         $result = mysqli_query($con, "SELECT * FROM GamesPositions WHERE id = '$id'") or die( mysqli_error( $con ) );
  470.  
  471.         $output = mysqli_fetch_array($result) or die( mysqli_error( $con ) );
  472.     }
  473.  
  474.     return $output;
  475. }
  476. function updateGameRelation($relationInfo, $con = NULL){
  477.     if($con == NULL){
  478.         $con = $GLOBALS['con'];
  479.     }
  480.     $columns = array('PlayedMove', 'GameID', 'PositionID');
  481.  
  482.     $id = ( empty( $relationInfo['id'] ) ? 'ESCAPE' : mysqli_real_escape_string( $con, $relationInfo['id'] ) );
  483.  
  484.     if($id == 'ESCAPE'){
  485.         return FALSE;
  486.     }
  487.  
  488.     for($x = 0; $x < count($columns); $x++){
  489.         $column = $columns[ $x ];
  490.         $query = ( empty( $relationInfo[ $columns[ $x ] ] ) ? '' : mysqli_real_escape_string( $con, $relationInfo[ $columns[ $x ] ] ) );
  491.  
  492.         if($query != ''){
  493.             mysqli_query($con, "UPDATE GamesPositions SET $column = '$query' WHERE id = '$id'") or die( mysqli_error( $con ) );
  494.         }
  495.     }
  496.  
  497.     $result = mysqli_query($con, "SELECT * FROM GamesPositions WHERE id = '$id'") or die( mysqli_error( $con ) );
  498.  
  499.     $output = mysqli_fetch_array($result) or die( mysqli_error( $con ) );
  500.  
  501.     return $output;
  502. }
  503.  
  504. //-----OPENING FUNCTIONS
  505. function findOpening($openingInfo, $con = NULL){
  506.     if($con == NULL){
  507.         $con = $GLOBALS['con'];
  508.     }
  509.     $first = TRUE;
  510.     $query = 'SELECT * FROM Openings WHERE ';
  511.  
  512.     $columns = array('id', 'ECOCode', 'Name', 'PGN', 'TimesPlayed');
  513.  
  514.     for($x = 0; $x < count($columns); $x++){
  515.         $key = $columns[$x];
  516.  
  517.         if( isset( $openingInfo[ $key ] ) && !empty( $openingInfo[ $key ] ) ){
  518.  
  519.             if($first){
  520.                 $query .= "$key = '".mysqli_real_escape_string( $con, $openingInfo[ $key ] )."'\n";
  521.             }else{
  522.                 $query .= "AND $key ='".mysqli_real_escape_string( $con, $openingInfo[ $key ] )."'\n";
  523.             }
  524.  
  525.             $first = FALSE;
  526.         }
  527.     }
  528.  
  529.     $result = mysqli_query($con, $query);
  530.  
  531.     $output = array();
  532.  
  533.     if($row = mysqli_fetch_array($result)){
  534.         $output = $row;
  535.     }
  536.  
  537.     $output = ( empty( $output ) ? FALSE : $output );
  538.  
  539.     return $output;
  540. }
  541. function createOpening($openingInfo, $con = NULL){
  542.     if($con == NULL){
  543.         $con = $GLOBALS['con'];
  544.     }
  545.     /*Minimum information required to create an opening:
  546.         -ECOCode
  547.         -Name
  548.         -PGN
  549.         -TimesPlayed
  550.     */
  551.  
  552.     $ECOCode        = ( empty( $openingInfo['ECOCode'       ] ) ? ''    : mysqli_real_escape_string( $con, $openingInfo['ECOCode'   ] ) );
  553.     $Name           = ( empty( $openingInfo['Name'          ] ) ? ''    : mysqli_real_escape_string( $con, $openingInfo['Name'      ] ) );
  554.     $PGN            = ( empty( $openingInfo['PGN'           ] ) ? ''    : mysqli_real_escape_string( $con, $openingInfo['PGN'       ] ) );
  555.     $TimesPlayed    = ( empty( $openingInfo['TimesPlayed'   ] ) ? '0'   : mysqli_real_escape_string( $con, $openingInfo['TimesPlayed'] ) );
  556.  
  557.     $output = FALSE;
  558.  
  559.     if($ECOCode != '' && $Name != '' && $PGN != ''){
  560.         $result = mysqli_query($con, "INSERT INTO PositionsRelations (ECOCode, Name, PGN, TimesPlayed)
  561.             VALUES ('$ECOCode', '$Name', '$PGN', '$TimesPlayed')") or die( mysqli_error( $con ) );
  562.  
  563.         $id = mysqli_insert_id($con) or die( mysqli_error( $con ) );
  564.  
  565.         $result = mysqli_query($con, "SELECT * FROM PositionsRelations WHERE id = '$id'") or die( mysqli_error( $con ) );
  566.  
  567.         $output = mysqli_fetch_array($result) or die( mysqli_error( $con ) );
  568.     }
  569.  
  570.     return $output;
  571. }
  572. function updateOpening($openingInfo, $con = NULL){
  573.     if($con == NULL){
  574.         $con = $GLOBALS['con'];
  575.     }
  576.     $columns = array('ECOCode', 'Name', 'PGN', 'TimesPlayed');
  577.  
  578.     $id = ( empty( $openingInfo['id'] ) ? 'ESCAPE' : mysqli_real_escape_string( $con, $openingInfo['id'] ) );
  579.  
  580.     if($id == 'ESCAPE'){
  581.         return FALSE;
  582.     }
  583.  
  584.     for($x = 0; $x < count($columns); $x++){
  585.         $column = $columns[ $x ];
  586.         $query = ( empty( $openingInfo[ $columns[ $x ] ] ) ? '' : mysqli_real_escape_string( $con, $openingInfo[ $columns[ $x ] ] ) );
  587.  
  588.         if($query != ''){
  589.             mysqli_query($con, "UPDATE PositionsRelations SET $column = '$query' WHERE id = '$id'") or die( mysqli_error( $con ) );
  590.         }
  591.     }
  592.  
  593.     $result = mysqli_query($con, "SELECT * FROM PositionsRelations WHERE id = '$id'") or die( mysqli_error( $con ) );
  594.  
  595.     $output = mysqli_fetch_array($result) or die( mysqli_error( $con ) );
  596.  
  597.     return $output;
  598. }
  599.  
  600. $con = mysqli_connect("127.0.0.1", "root", "<you aren't getting that!>", "Chess");
  601.  
  602. //Location, User, Passkey, Database
  603.  
  604. if (mysqli_connect_errno()){
  605.     echo "Failed to connect to MySQL: " . mysqli_connect_error();
  606. } else {
  607.     echo "Successfully Connected to mySQL server!\n";
  608. }
Advertisement
Add Comment
Please, Sign In to add comment