|
This macro allows the developer to execute a SELECT command at a time and have access to the data returned through the dataset parameter, in array format: {dataset[line_number][field_name]}. To run commands that return a large amount of data, we recommend using sc_select macro for better performance.
Unlike the macro sc_lookup, where we inform the index {dataset[0][0]}, in the macro sc_lookup_field we must inform the name of the column that we want to retrieve the data {dataset[0][field_name]}
The way of assembling the return array, where we must inform the column name, makes this macro incompatible with the following databases: MSSQL Server, Access and DB2. In these cases, we recommend using the sc_lookup macro.
See below for details on the macro parameters.
| Parameter |
Description |
Examples |
dataset |
This parameter is mandatory and must be informed without spaces or special characters and does not accept variables.
It defines the name of the variable that will receive the return of the SELECT command executed by the macro.
In case of error in the execution of the SQL command, this parameter will return false and the error message will be available in the variable "dataset_erro".
Below is a simple example of validation and error message display.
if ( {meus_dados} === false ) {
echo "Access error. Message = " . {meus_dados_erro};
}
|
sc_lookup_field("my_dataset", "select field_01, field_02, field_03 from tb_name");
When executing the command print_r({my_dataset}); we will get the following return:
Array (
[0] => Array (
[field_01] => 1 [field_02] => OFFICE EQUIPMENT
)
[1] => Array (
[field_01] => 2 [field_02] => FIXED TELEPHONY ) )
In the example above, the command returned two record lines and in each line two columns (field_01 and field_02).
The recovery of the value should take place as follows.
{my_dataset[line_number][colunm_name]}
|
SQL Command |
This parameter is mandatory and can be defined by informing the command directly in the macro protected with quotes (double or single) or using a variable (local or global).
|
// Command directly in the Macro sc_lookup_field("retorno_select", "select field_01, field_02, field_03 from tb_name");
// Sample with variable $comando_select = "select field_01, field_02, field_03 from tb_name" sc_lookup_field("retorno_select", $comando_select);
|
Connection |
The "connection" parameter is optional, and must be informed if the command is executed in a database different from the one specified for the application.
Furthermore, this parameter does not accept the use of variables, being necessary to type the name of the connection for the macro to run correctly.
|
// Sample of using the connection parameter
sc_lookup_field("retorno_select", "select field_01, field_02, field_03 from tb_name", "nome_conexao");
|
Examples
Example 1: Access multiple lines
sc_lookup_field(my_dataset, "select field_01, field_02, field_03 from tb_name");
//To access the first line (dataset), we must inform: {field_01} = {my_dataset[0]['field_01']}; {field_02} = {my_dataset[0]['field_02']}; {field_03} = {my_dataset[0]['field_03']};
/To access the second line (dataset), we must inform: {field_01} = {my_dataset[1]['field_01']}; {field_02} = {my_dataset[1]['field_02']}; {field_03} = {my_dataset[1]['field_03']};
Example 2: With data validation:
$comando_sql = "select field_01, field_02, field_03 from tb_name";
sc_lookup_field(my_dataset, $comando_sql);
if ( {my_dataset} === false ) {
echo "Access error. Message = " . {my_dataset_error};
} elseif ( empty({my_dataset}) ) {
echo "select command did not return data ";
} else {
{clienteid} = {my_dataset[0]['field_01']}; {nomecliente} = {my_dataset[0]['field_02']}; {limitecred} = {my_dataset[0]['field_03']};
}
Example 3: Using the connection parameter
$comando_sql = "select field_01, field_02 from tb_name where field_03 = '{field_03} ' and field_02 = [var_global]"
sc_lookup_field(my_dataset, $comando_sql, "conn_name");
Example 4: Using local and global variables in the macro command
$comando_sql = "select field_01, field_02 from tb_name where field_03 = '{field_03} ' and field_02 = [var_global]"
sc_lookup_field(my_dataset, $comando_sql);
|