[Tutorial] 2moons Code Database

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • [Tutorial] 2moons Code Database

      Hello. I thought i may explain how to work with the Database in 2moons for the newbies. Somehow i may want to give something back after those "dying years of 2moons". May someone will find this useful.

      Source Code

      1. So how to perform an Insert in the Database.
      2. $sql = "INSERT INTO TABLE NAME (Inside here you write the Database column names you want to set, columns with no default values must have a value otherwise an error will be thrown) VALUES (Inside here you only set your param link OR static values for the values. the third value in in the first brackets must be also the third value in those brackets)";
      3. database::get()->insert($sql, array(Inside here you define the params from the second brackets));
      4. Working but unuseful example:
      5. $randValue = mt_rand(1,500);
      6. $sql = "INSERT INTO uni1_aks (name, target, ankunft) VALUES (:name, :param2, :otherParam)";
      7. database::get()->insert($sql, array(
      8. ':name' => 'ACS_'.$randValue,
      9. ':param2' => $randValue,
      10. ':otherParam' => TIMESTAMP,
      11. ));
      12. // You do not need to insert the id as it's ai (auto increment) and its value will be increased by one by default
      Display All







      The next one is Update:

      Source Code

      1. In this case we are going to update the acs.
      2. $sql = "UPDATE TABLE SET COLUMN_NAME = :NewParam", COLUMN_NAME = :newParam2 WHERE COLUMN_NAME = :variable";
      3. database::get()->update($sql, array(
      4. ':NewParam' => any Variable or static value,
      5. ':newParam2' => same here
      6. ));
      7. Working example:
      8. $newRandomValue = mt_rand(500,150000); // You can also use only rand()
      9. $sql = "UPDATE uni1_aks set name = :newName, target = :newTarget, ankunft = :otherAnkufnt WHERE id = 5";
      10. database::get()->update($sql, array(
      11. ':newName' => 'ACS_'.$newRandomValue,
      12. ':newTarget' => 1,
      13. ':otherAnkunft' => TIMESTAMP + 60*60
      14. ));
      Display All


      The next one is delete:

      Source Code

      1. In this example we are going to delete an acs entry.
      2. $sql = "DELETE FROM TABLE WHERE COLUMN_NAME = :yourValue";
      3. database::get()->delete($sql, [
      4. ':yourValue' => any Variable or static content.
      5. ]);
      6. To delete everything inside the table you do:
      7. $sql = "DELETE FROM TABLE";
      8. database::get()->delete($sql);
      9. Working example to delete everything:
      10. $sql = "Delete FROM uni1_acs";
      11. database::delete($sql);
      12. Working example for only one row:
      13. $numberFour = 4;
      14. $sql = "DELETE FROM uni1_aks WHERE id = :value OR id = :static or id = 3";
      15. database::get()->delete($sql, [
      16. ':value' => 2,
      17. ':static' => $numberFour
      18. ]);
      Display All


      How to select and create a list of a table in php with mysql?


      Source Code

      1. In this example ill show you how to create a list of results from sql.
      2. first you define an array.
      3. $array_name = array();
      4. $sql = "SELECT * FROM uni1_aks WHERE id > 5";
      5. $query_result = database::get()->select($sql);
      6. foreach($query_result as $result){
      7. $array_name[$result['id']] = array(
      8. 'id' => $result['id'],
      9. 'ankunft' => $result['ankunft],
      10. );
      11. //If you do not want to use specific columns and you need all. you do not need to use a foreach loop. In case you do used specific columns you can submit the values to smarty (2moons template engine) trough that.
      12. $this->assign(array(
      13. 'template_variable' => $array_name,
      14. ));
      15. }
      16. In the template you can use {foreach} as also, an example can be found at smarty api (just google smarty template php)
      Display All


      How to select only one row of a table:

      Source Code

      1. In this example we are selecting one row of a table.
      2. $sql = "SELECT * FROM uni1_acs WHERE id = 3";
      3. $result = database::get()->select($sql);
      4. Submit the $result to $this->assign(); and use the result in the template like this: {$result.target}




      And the last one is a bit more advanced and is "how to connect 2 tables?"

      Source Code

      1. In this example we connect the acs and planets table to get some values out of the acs table
      2. $sql = "SELECT acsData.*, p.username, u.ally_id FROM uni1_aks acsData LEFT JOIN %%PLANETS%% p ON acsData.target = p.id WHERE acsData.id = :randomId";
      3. $result = database::get()->selectSingle($sql, [
      4. ':randomId' => mt_rand(1,12),
      5. ]);




      What can be used to shorten code?
      - use a variable at the start of the function
      $db = database::get();

      In that case you would use $result = $db->select($sql); for example.

      - As also you can use the db table to shorten table names

      So instead of uni1_vars_requriements as table you write %%VARS_REQUIRE%%.

      - Place static content inside the second brackets at insert to avoid more code.

      So instead of "(name, surname, country) VALUES (:name, :surname, "England"); So in that case it is always england and you do not define it at: database::get()->insert($sql, [':name' => $name, ':surname' => $surname]);


      There are some more methods but those are enough to code in a good standard.




      I hope this Tutorial is helping someone who is learning and is helpless. The Code is not tested but it should be fine. May someone wanna add some more options below :)
    • nice!!! very nice!!! happy to see :love:
      nochmal auf deutsch, damit man nicht selbst mit google translate rumfrickeln muss^^


      XenQen wrote:

      Hallo. Ich dachte, ich könnte erklären, wie man mit der Datenbank in 2moons für die Anfänger arbeitet. Irgendwie möchte ich nach diesen "sterbenden Jahren von 2moons" etwas zurückgeben. Möge jemand das nützlich finden.

      Source Code

      1. So, wie man einen Insert in der Datenbank durchführt.
      2. $sql = "INSERT INTO TABLE NAME (Darin schreiben Sie die Datenbankspaltennamen, die Sie setzen möchten, Spalten ohne Standardwerte müssen einen Wert haben, andernfalls wird ein Fehler ausgelöst. VALUES (Hier geben Sie nur Ihre Parameterverknüpfung ODER statische Werte für die Werte an. Der dritte Wert in in den ersten Klammern muss auch der dritte Wert in diesen Klammern stehen)";
      3. database::get()->insert($sql, array(Hier definieren Sie die Parameter aus den zweiten Klammern));
      4. Working but unuseful example:
      5. $randValue = mt_rand(1,500);
      6. $sql = "INSERT INTO uni1_aks (name, target, ankunft) VALUES (:name, :param2, :otherParam)";
      7. database::get()->insert($sql, array(
      8. ':name' => 'ACS_'.$randValue,
      9. ':param2' => $randValue,
      10. ':otherParam' => TIMESTAMP,
      11. ));
      12. // Sie müssen die ID nicht als ai (auto increment) einfügen und ihr Wert wird standardmäßig um eins erhöht
      Display All






      Der nächste ist Update:

      Source Code

      1. In diesem Fall werden wir die AKS aktualisieren.
      2. $sql = "UPDATE TABLE SET COLUMN_NAME = :NewParam", COLUMN_NAME = :newParam2 WHERE COLUMN_NAME = :variable";
      3. database::get()->update($sql, array(
      4. ':NewParam' => any Variable or static value,
      5. ':newParam2' => same here
      6. ));
      7. Working example:
      8. $newRandomValue = mt_rand(500,150000); // You can also use only rand()
      9. $sql = "UPDATE uni1_aks set name = :newName, target = :newTarget, ankunft = :otherAnkufnt WHERE id = 5";
      10. database::get()->update($sql, array(
      11. ':newName' => 'ACS_'.$newRandomValue,
      12. ':newTarget' => 1,
      13. ':otherAnkunft' => TIMESTAMP + 60*60
      14. ));
      Display All

      Der nächste ist löschen:

      Source Code

      1. In diesem Beispiel löschen wir einen AKS-Eintrag.
      2. $sql = "DELETE FROM TABLE WHERE COLUMN_NAME = :yourValue";
      3. database::get()->delete($sql, [
      4. ':yourValue' => eine Variable or statischer Inhalt.
      5. ]);
      6. Um alles in der Tabelle zu löschen, tun Sie Folgendes:
      7. $sql = "DELETE FROM TABLE";
      8. database::get()->delete($sql);
      9. Arbeitsbeispiel um alles zu löschen:
      10. $sql = "Delete FROM uni1_acs";
      11. database::delete($sql);
      12. Arbeitsbeispiel für nur eine Zeile:
      13. $numberFour = 4;
      14. $sql = "DELETE FROM uni1_aks WHERE id = :value OR id = :static or id = 3";
      15. database::get()->delete($sql, [
      16. ':value' => 2,
      17. ':static' => $numberFour
      18. ]);
      Display All

      Wie man eine Liste einer Tabelle in PHP mit mysql auswählt und anlegt?

      Source Code

      1. In diesem Beispiel zeigen wir Ihnen, wie Sie eine Liste der Ergebnisse von SQL erstellen.
      2. Zuerst definieren Sie ein Array.
      3. $array_name = array();
      4. $sql = "SELECT * FROM uni1_aks WHERE id > 5";
      5. $query_result = database::get()->select($sql);
      6. foreach($query_result as $result){
      7. $array_name[$result['id']] = array(
      8. 'id' => $result['id'],
      9. 'ankunft' => $result['ankunft],
      10. );
      11. // Wenn Sie keine spezifischen Spalten verwenden möchten und Sie alle benötigen. Sie müssen keine foreach-Schleife verwenden. Falls Sie bestimmte Spalten verwendet haben, können Sie die Werte an smarty (2moons template engine) übergeben.
      12. $this->assign(array(
      13. 'template_variable' => $array_name,
      14. ));
      15. }
      16. In der Vorlage können Sie auch {foreach} verwenden, ein Beispiel finden Sie unter smarty api (nur google smarty template php)
      Display All

      So wählen Sie nur eine Zeile einer Tabelle aus:

      Source Code

      1. In diesem Beispiel wählen wir eine Zeile einer Tabelle aus.
      2. $sql = "SELECT * FROM uni1_acs WHERE id = 3";
      3. $result = database::get()->select($sql);
      4. Übergeben Sie das $result an die templates $this->assign(); und verwenden das Ergebnis im template wie folgt:{$result.target}



      Und der letzte ist ein bisschen fortgeschrittener und ist "wie zwei Tabellen verbinden?"

      Source Code

      1. In diesem Beispiel verbinden wir die Tabelle acs und planets, um einige Werte aus der Tabelle acs zu erhalten
      2. $sql = "SELECT acsData.*, p.username, u.ally_id FROM uni1_aks acsData LEFT JOIN %%PLANETS%% p ON acsData.target = p.id WHERE acsData.id = :randomId";
      3. $result = database::get()->selectSingle($sql, [
      4. ':randomId' => mt_rand(1,12),
      5. ]);



      Was kann verwendet werden, um Code zu verkürzen?
      - use a variable at the start of the function
      $db = database::get();

      In diesem Fall würden Sie zB. $result = $db->select($sql); verwenden



      - Sie können auch die dbTabelle verwenden, um Tabellennamen zu verkürzen
      Anstelle von uni1_vars_requrements als Tabelle schreibst du %%VARS_REQUIRE%%.

      - Platzieren Sie statischen Inhalt in den zweiten Klammern des INSERT, um mehr Code zu vermeiden.

      anstatt "(name, surname, country) VALUES (:name, :surname, "England"); Also in diesem Fall ist es immer England und du definierst es nicht bei: database::get()->insert($sql, [':name' => $name, ':surname' => $surname]);



      Es gibt einige weitere Methoden, aber diese sind genug, um einen guten Standard zu programmieren.

      Ich hoffe, dass dieses Tutorial jemandem hilft, der lernt und hilflos ist. Der Code wird nicht getestet, aber es sollte in Ordnung sein. Darf jemand noch einige Optionen hinzufügen :)