How do you insert a PHP constant into a SQL query?

前端 未结 4 2273
野性不改
野性不改 2020-12-18 05:34

I have a PHP file with my database configuration settings defined as constants, for example:



        
4条回答
  •  误落风尘
    2020-12-18 05:52

    You'll have to use string concatenation (of any sort).

    $query = "SELECT users FROM " . DB_TABLE_1;
    

    constants will not interpolate into a string as variables can.

    One hackish alternative is to use a variable function:

    $const = 'constant';
    $query = "SELECT users FROM {$const('DB_TABLE_1')}";
    

    which'll execute the constant() function and return the constant's value, but that's generally not a good idea, if only for legibility's sake.

提交回复
热议问题