0
I have a select, within it I have a function :
SELECT VAL1, VAL2, ".funcao('VAL1', 'VAL2')." AS soma FROM Conta WHERE soma < 100
Table structure CONTA
--------------------
| ID | VAL1 | VAL2 |
--------------------
| 1 | 10 | 50 |
--------------------
| 2 | 50 | 80 |
--------------------
| 3 | 30 | 70 |
--------------------
If I use the following structure in the function :
function funcao($valor1, $valor2)
{
$retorno = $valor1 + $valor2;
return $retorno;
};
I can return the value of each column in a good, and still get the result!
Then I tried to work the function more and apply the value inside a string. Ex:
function funcao($valor1, $valor2)
{
$res = "Os números a ser somados são ".$valor1." e ".$valor2."";
echo $res ;
//Esse echo serve apenas de exemplo, pra mostrar o retorno da variável
};
Only that I can’t do it! When I applied the variable $valor1
or $valor2
, instead of displaying the column value, it showed the column name itself, i.e., VAL1
and VAL2
My question is
Is it possible to work with these variables in a more complex function according to the example cited, without losing the value of the column? Since when I try to do this the value is as "VAL1" instead of 10 or 50.
In simpler mode! Even if I just want to do a "echo $valor1" within the function, the result is "VAL1" instead of "10"
Try to understand that the Select part happens in Mysql, not PHP. If you set the function when generating the query, it is processed BEFORE Mysql starts working (even before it has the data to process). Mysql only works SQL code, not PHP. After the data comes back from SELECT, then you can use PHP again, but not in query (which is the last example I gave).
– Bacco
In the same paragraph that you marked in bold, you explained directly how the function will be executed, IE, first you will get the value of the function and then run the select together! And that made it clear!
– ivan veloso
In this case, 'VAL1' and 'VAL2' pro PHP have a completely different sense, are mere strings. When you do
funcao( 'VAL1', 'VAL2' )
is sending two "texts" to the function, so echo shows exactly the same. I’ll delete the comments from above, which you’ve read, so it’s not too messy in your question.– Bacco
Yes! I understood that part! That’s why I changed the question, and that’s why I accepted it! Because now I understand the process! - I thought VAL1 and VAL2 already had their result! Only from what I understood, at the time the function is executed the Select has not even happened yet! -I thought the opposite! That I was starting select, then function and that after that process I was losing values
– ivan veloso
Maybe a way is to learn the Mysql functions and test separately in the database (using Mysql Workbench, or similar tool) to train to do operations on DB directly (forgetting a little PHP). My fear is only you end up confusing more, kkkk. Maybe it’s good to give a "trained" in PHP functions and parameters before. But when you think it’s time, a summary of the Mysql functions is here: https://dev.mysql.com/doc/refman/5.6/en/functions.html
– Bacco
I even got to see an example of a question in stackoverflow in English that the guy used the proper function of Mysql. But now yes I understood why!
– ivan veloso
You can play a little with SQL Fiddle if you want to test it. I set up a test applying what I put in the answer: http://sqlfiddle.com/#! 9/14b055/1
– Bacco