{{indexmenu_n>300}} ====== DB query using the funct. "_dbQuery" ====== FIXME Better TRANSLATE Here's a small example of how the built-in function _dbQuery ** ** may be used in a script. function _dbQuery($query='', $_queryMode='ASSOC') { ....... } \\ **$_queryMode:** * INSERT, DELETE, UPDATE * ON_DUPLICATE //(INSERT ... ON DUPLICATE KEY)// * ROW, ARRAY //(SELECT Queries) // * COUNT //(Count)// * COUNT_SHOW //(send SHOW query and count results)// * ASSOC //(default)// \\ ===== Example: ===== **Adoption:** From a given table //(exemplarily "phpwcms_country")// data should be selected and displayed in a CP. \\ Of course the table can also be a self-created. The script is stored in the folder /frontend_render/ . The data are here collected from the database, selected and placed into a usable format, here . \\ With a replacement tag is the result shown in a CP. \\ \\ ===== Adjusted Table Excerpt: ===== ^country_id ^ country_iso ^ country_iso3 ^ country_isonum ^ country_continent_code ^ country_name ^ country_continent ^ | 1 | AF | AFG | 4 | AS | Afghanistan, Islamic Republic of | Asia | | 2 | AL | ALB | 8 | EU | Albania, Republic of | Asia | | 3 | DZ | DZA | 12 | AF | Algeria, People\'s Democratic Republic of | Africa | | 4 | AS | ASM | 16 | OC | American Samoa | Oceania | | 5 | AD | AND | 20 | EU | Andorra, Principality of | Europe | | 6 | ... | ... | ... | ... | ... | ... | \\ * **Replacer in CMS:** {DB-QUERY} * **Table name:** phpwcms_country * **Used fields of the table:** country_id, country_iso, country_iso3, country_isonum, country_continent_code, country_name, country_continent * **Selection by:** * A value of "continent_code" //(EU)// * **AND NOT** Several values of "country_iso3" //('ala','alb')// * **AND** First letter from "country_iso" // (a,b,c) // \\ ===== Script: ===== **File:** template/frontend_remder/rt_example_db_query01.php '.LF; $table .= ''.LF.''.LF; $table .= LF.'' .LF.'' .LF.'' .LF.'' .LF.'' .LF.'' .LF.'' .LF; $table .= ''.LF; foreach ($result as $value) { $table .= ''.LF; $table .= LF.'' .LF.'' .LF.'' .LF.'' .LF.'' .LF.'' .LF.'' .LF; $table .= ''.LF; } $table .= ''.LF.'
db-ID ISO ISO3 ISONUM C-CODE C-NAME CONTINENT
'.$value['country_id']. ''.$value['country_iso']. ''.$value['country_iso3']. ''.$value['country_isonum']. ''.$value['country_continent_code'].''.$value['country_name']. ''.$value['country_continent'].'
'.LF; } // --- Replace and insert into page $content["all"] = str_replace('{DB-QUERY}',$table, $content["all"]); } ?> \\ \\ ===== Result: ===== **TAG:** {DB-QUERY} e.g. in the CP "Plain Text". {{:deutsch:technik:aufruf-interner-funktionen:example_db_query01_1.gif|}} \\ ~~UP~~ ===== Script V1.1: ===== **With parameter transfer** e.g. **{DB-QUERY:EU}**, Results, see above. **File:** template/frontend_remder/rt_example_db_query01.php **TAG:** {DB-QUERY:[C-CODE]} '.LF; $table .= ''.LF.''.LF; $table .= LF.' db-ID ' .LF.' ISO ' .LF.' ISO3 ' .LF.' ISONUM ' .LF.' C-CODE ' .LF.' C-NAME ' .LF.' CONTINENT ' .LF; $table .= ''.LF; foreach ($result as $value) { $table .= ''.LF; $table .= LF.''.$value['country_id']. '' .LF.''.$value['country_iso']. '' .LF.''.$value['country_iso3']. '' .LF.''.$value['country_isonum']. '' .LF.''.$value['country_continent_code'].'' .LF.''.$value['country_name']. '' .LF.''.$value['country_continent'].'' .LF; $table .= ''.LF; } $table .= ''.LF.''.LF; } } if ($error_flag) $table = $error_text; // --- Replace and insert into page // $content["all"] = str_replace('{DB-QUERY:}',$table, $content["all"]); // Old version $content["all"] = preg_replace('/\{DB-QUERY\:'.$input.'\}/', $table, $content["all"]); } ?> \\ Please pay attention to the inquiry of security after ## %%// --- catch the selector %%## \\ If the data comes from a user input, this data must be considered very low!! **Links:** * [[http://php.net/manual/de/function.preg-match.php|PHP preg_match]] * [[http://www.php.net/manual/de/function.preg-replace.php|PHP preg_replace]] * [[links/php/regular-expression]] * [[http://php.net/manual/de/function.in-array.php|PHP in_array]] \\