Concatenate 2 PHP variables into SQL statement

Asked

Viewed 541 times

1

I am developing a PHP system with Oracle and to "optimize" an update, I would need to concatenate a variable (with values from 1 to 3) in the name of the variable that I will pass in the instruction. The variables come from a form, and I need to pass one by one, without using array.

Below is a passage that is today:

$update .="CODIGO = '$produto_1' AND ";

The above example would need to pass something like this, where the $passo would have fixed values (today from 1 to 3):

$update .="CODIGO = '$produto_.$passo' AND ";

In that case I should pass as variable too $produto_1, afterward $produto_2......

Using the point to concatenate didn’t work. Any hints?

4 answers

4

I used Extract to generate the variable test passo_X, but the applied logic would be for.

$arrayPassos = array(
    'passo_1' => 'teste1',
    'passo_2' => 'teste2',
    'passo_3' => 'teste3',
    'passo_4' => 'teste4',
);
extract($arrayPassos); // criada variaves passo_X

$out = '';
for ($i=1; $i <= count($arrayPassos); $i++){
    $out .= ${"passo_{$i}"};
}

Note

  • in such cases it is best to use array, and not numbered variables.
  • Wow it’s true, I’ve rarely used it, but it works perfectly.

  • @Guilhermenascimento da para usar para chamada de funções também :D

  • Is practically the eval then :/

  • @Guilhermenascimento not exactly, would be almost a $$nameVar

3


This gets very complex, use arrays, for this reason they exist because if not you will have to use eval, which would be something like (recommend that don’t use eval, is only to exemplify):

$var1 = eval('return $produto_' . $passo);

$update .="CODIGO = '$var1' AND ";

Then do with array, which would be something like:

$produto = array();

$produto[1] = ...;
$produto[2] = ...;

$update .= "CODIGO = '" . $produto[$passo] . "' AND ";

You quoted form, so I can assume that refers to HTML, you can do so too:

<form method="POST" action="pagina.php">
    <input type="text" name="passo[]" value="a">
    <input type="text" name="passo[]" value="b">
    <input type="text" name="passo[]" value="c">
    <button>Enviar</button>
</form>

And in PHP you will get it like this:

<?php
print_r($_POST['passo']);

The exit will be:

Array
(
    [0] => a
    [1] => b
    [2] => c
)
  • 1

    Thank you William, using the Val solved the problem. Thank you.

  • 1

    @Diego might be better to use the array as I mentioned, or the http://answall.com/a/151986/3635response, which for you would be something like ${"produto_{$passo}"}

  • 1

    I will do some more tests, if it gets too bad I will pass to array. Thanks for the help.

  • @Diego I corrected the example, actually I wanted to say so $update .= "CODIGO = '" . $produto[$passo] . "' AND ";, I think it gets easier :)

  • 1

    When a solution leads to a eval, it’s time to think about refactoring everything.

  • @Wallacemaxters but I told you not to use and what’s right is to switch to array (or is meaning this for Diego?)

  • 1

    @Guilhermenascimento It’s all right. So, you showed the refactoring in your answer (the arrays option). Besides being more organized, it avoids problems of legibility and security. In fact, I could not miss the joke :D

Show 2 more comments

2

Using the point to concatenate didn’t work. Any hints?

It did not work as expected because the attempt at concatenation is being made between quotation marks, the . is being interpreted as any character, and not as concatenation operator.

Follow another alternative:

$update .= "CODIGO = '{$produto}_{$passo}' AND ";

Editing: It is also possible reference the variable in a loop:

$produto_1 = "PROD1";
$produto_2 = "PROD2";
$produto_3 = "PROD3";

$limite = 3;
$update = "";

for ($passo = 1; $passo <= $limite; $passo++) {
    $update .= "CODIGO = '" . ${"produto_$passo"} . "' AND ";
}

echo $update;
// CODIGO = 'PROD1' AND CODIGO = 'PROD2' AND CODIGO = 'PROD3' AND

See DEMO

  • 2

    Actually he wants to generate the variable $producto_1, $producto_2. And not the variables $product and $step, see: http://ideone.com/pYZfTJ, this will generate something like 1_2, but what he wants is that $produto . '_' . $passo turn this $produto_1 and to obtain the value of $produto_1 = 1; for example. As I mentioned, the best way is the arrays :)

  • In this example, it recognizes $product as a variable, not $producto_1, since 1 is the value that is stored in $step....

  • I’m not sure, but I think he has a loop that iterates with the $passo

  • 1

    @stderr a sim, was a typo, thanks :) ... yes I think this is just what AP needed, from his example of ideone.

1

You can mount the concatenation with the function sprintf() also, the %s are exchanged for the relevant variables (values) in the order they appear.

$passo = $_POST['passo']; 
$update .= sprinf("CODIGO = '%s_%s'", $produto, $passo);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.