开发者

Putting more than one variable into a sql statement

开发者 https://www.devze.com 2023-01-02 15:16 出处:网络
I have constructed the following - `$format_$game_$name_$season` 4 variabl开发者_如何学Goes with underscores between. Should this work, or will it act as one variable?

I have constructed the following -

`$format_$game_$name_$season`

4 variabl开发者_如何学Goes with underscores between. Should this work, or will it act as one variable?

THankyou


A quick test indicates that, for reasons I can not fathom, your interpolation will not work as you expect it to.

<?php
    $xa = 'x';
    $xb = 'y';
    $xc = 'z';
    echo "$xa_$xb_$xc";
?>

The output of the above script in PHP 5.3 is z, since it reads $xa_ (which is empty), $xb_ (which is empty), and $xc (which is z).

If you use braces for interpolation, however, you should get the desired output.

<?php
    $xa = 'x';
    $xb = 'y';
    $xc = 'z';
    echo "${xa}_${xb}_${xc}";
?>

The output of the above script in PHP 5.3 is x_y_z, as expected.


That wont work, but not because it'll act as one variable.

First, you need to use double quotes. Backticks wont interpret at all and variables wont be expanded in a single quoted string. Further reading: String Parsing.

Furthermore, when double quoting your string it'll be interpreted as $format_ . $game_ . $name_ . $season. That is, PHP will be thinking the three first variables end with an underscore. You'll have to do either $format . '_' . $game . '_' . $name . '_' . $season or "{$format}_{$game}_{$name}_{$season}".


Useful Information

With backtick (``) delimiters, your variable will be blank.

With single quotes ('), it'll be interpreted literally.

With double quotes ("), you should get the variable interpolation (in most cases expect the case described in your question). You can use braces too for readability and some situations where it won't work without them, i.e {$var['key']}.


Should this work, or will it act as one variable?

It will not, no.

1) You should use PDO for database access (I'm required to say this!)

With that out of the way:

2) Backticks are execution operators

3) Variables can be encased in braces: "{$format}_{$game}_{$name}_{$season}"

4) Some databases use the underscore as a special character so it may need to be escaped

0

精彩评论

暂无评论...
验证码 换一张
取 消