Mysqli SELECT ? (bind_param)

前端 未结 2 1173
遥遥无期
遥遥无期 2021-01-24 17:47

I am trying to query the data in a column dependent on the variable $garment. The query works until I try to bind the parameter $garment . Any idea what I\'m doing wrong?

<
相关标签:
2条回答
  • 2021-01-24 18:05

    That happens because with prepared statements you only can build values (not identifiers). That's it

    SELECT ? 
    

    becomes

    SELECT 'somevalue'
    

    The first code is the correct one but to be safe you must ensure that the $garment variable value is whitelisted.

    0 讨论(0)
  • 2021-01-24 18:21

    You can't use markers for the column name. So you'll have to specify the actual column name w/o a marker.

    From the official php docs

    The markers are legal only in certain places in SQL statements. For example, they are allowed in the VALUES() list of an INSERT statement (to specify column values for a row), or in a comparison with a column in a WHERE clause to specify a comparison value.

    However, they are not allowed for identifiers (such as table or column names), in the select list that names the columns to be returned by a SELECT statement), or to specify both operands of a binary operator such as the = equal sign. The latter restriction is necessary because it would be impossible to determine the parameter type. In general, parameters are legal only in Data Manipulation Languange (DML) statements, and not in Data Defination Language (DDL) statements.

    0 讨论(0)
提交回复
热议问题