User Tools

Site Tools


en:dev:all:examples:sql-1

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:dev:all:examples:sql-1 [27.06.2015 19:51] – [Verschiedene Beispiele für mehr oder weniger komplexe SQL-Abfragen] translated mrbasemanen:dev:all:examples:sql-1 [27.06.2015 20:36] (current) – finished translation and removed fixme mrbaseman
Line 1: Line 1:
-FIXME **This page is not fully translated, yet. Please help completing the translation.**\\ //(remove this paragraph once the translation is finished)// 
  
 ====== Proper use of SQL queries ====== ====== Proper use of SQL queries ======
Line 7: Line 6:
 **Attention:** The following examples are __**not**__ intended for //**copy&paste**// programming, but only exemplary of thinking ideas that are intended to show how a problem can be solved! **Attention:** The following examples are __**not**__ intended for //**copy&paste**// programming, but only exemplary of thinking ideas that are intended to show how a problem can be solved!
  
-==== Abruf eines einzelnen Users ==== +==== Retrieval of a single user ==== 
-Es wird einfach anhand der `user_id` ein einzelner Datensatz in der Tabelle `users` gesucht+Based on the `user_id` simply a single record is searched in the table `users`.
  
-[color=red]falsch[/color]+[color=red]false[/color]
 <code=PHP> <code=PHP>
 $query = $database->query("SELECT * FROM '.TABLE_PREFIX.'users WHERE user_id = $user_id LIMIT 1"); $query = $database->query("SELECT * FROM '.TABLE_PREFIX.'users WHERE user_id = $user_id LIMIT 1");
Line 21: Line 20:
 } }
 </code> </code>
-Dieser Code enthält gleich mehrere Problemstellen und überflüssigen Code:\\ +This code contains a number of problem areas and superfluous code:\\ 
-  Die Methode query() ist nicht abgesichertBei einem Fehler im Statement oder der Datenbank bricht das Script mit der sehr aussagekräftigen Meldung "//Error in class database line xxx//" komplett ab+   The query() method is not securedIf an error occurs in the statement or the database, the script breaks with the very meaningful message "//Error in class Database line xxx//" completely
-  * LIMIT 1  dieses Statement ist überflüssig, da eine user_id als Primärschlüssel grundsätzlich nur einmal vorhanden sein kann+   * LIMIT 1 - this statement is unnecessary because a user_id can be the primary key in principle only once
-  Das SQL-Statement entspricht nicht den SQL-Strikt Regeln und ist zusätzlich noch innerhalb der Argumentenklammer von ->query() definiert+   The SQL statement does not correspond to the SQL Strict rules and is additionally defined within the argument bracket of ->query(). 
-  * WHILE - diese Schleife ist überflüssigda ja maximal nur ein Datensatz geliefert wird+   * WHILE - this loop is superfluoussince a maximum of only one data set will be delivered
-  Die Abfrage auf numRows() ist überflüssig, weil das nachfolgende fetchRow() den ersten Datensatz des Result-Objektes oder **null**, falls kein Datensatz gefunden wurde, zurückgibt.+   The query on numRows() is unnecessary because the subsequent fetchRow() returns the first record of the result object, or null if no record was found.
  
-Nach den neuen Standards von WB korrigiertergibt sich dadurch folgendes [color=green]richtige[/color] Codefragment:+Corrected applying the new standards of WB, this results in the following [color=green]correct[/color] code fragment:
 <code=PHP Snippet.php> <code=PHP Snippet.php>
 $sql = 'SELECT * FROM `'.$oDb->TablePrefix.'users` ' $sql = 'SELECT * FROM `'.$oDb->TablePrefix.'users` '
Line 43: Line 42:
 </code> </code>
  
- +==== Retrieve a page and to the name of the associated user ==== 
-==== Eine Seite abrufen und dazu den Namen des zugehörenden Users ==== +//(For simplicity, only the SQL statements are displayed.)//\\ 
-//(zur Vereinfachung werden nur die SQL-Statements dargestellt.)//\\ +To achieve this, data from two tables are requiredthe data from 'wb_pages' and the user name from the table 'wb_users'This problem is elegantly solved without any additional PHP code with pure **SQL**. To do this, only the two tables need to be correlated by the fields 'modified_by' and 'user_id' linked by a //JOIN//.\\ 
-Hierzu werden Daten aus zwei Tabellen benötigtdie Daten aus 'wb_pages' sowie der Benutzername aus der Tabelle 'wb_users'. +It is worthwhile in any casebe read in SQL to grasp the basics.
-Diese Aufgabe ist ohne jeden zusätzlichen PHP-Code rein mit **SQL** elegant lösbarDazu müssen nur die beiden Tabellen über die Felder 'modified_by' und 'user_id' durch einen //JOIN// miteinander verknüpft werden.\\ +
-Es lohnt sich auf jeden Fallsich in SQL einzulesen um die Grundlagen zu begreifen.+
 <code=SQL Snippet.php> <code=SQL Snippet.php>
 SELECT `wb_users`.`display_name`, `wb_pages`.*  SELECT `wb_users`.`display_name`, `wb_pages`.* 
Line 56: Line 53:
 </code> </code>
  
-FIXME //... wird mit Auftauchen weiterer Beispiele kontinuierlich fortgesetzt!//+FIXME //...will be continued with emergence of other examples!//
  
en/dev/all/examples/sql-1.1435434712.txt.gz · Last modified: 27.06.2015 19:51 by mrbaseman