A
A
Alexey Sklyarov2018-02-02 21:40:48
PHP
Alexey Sklyarov, 2018-02-02 21:40:48

How can I organize such execution of a PDO query with bind_param?

There is a query to the database:

$stmts = $this->db->prepare("SELECT user_id FROM usersSettings WHERE ?=?");
$stmts->bind_param("ss", $method,$percent);

In the query, the first "?" is the name of the column to be checked. Alas, this way of writing does not display anything for me ([num_rows] => 0). If instead of the first ? write a specific column in the table, then everything works correctly, how can I set this column (which must be passed as an argument to the function)?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
OKyJIucT, 2018-02-02
@OKyJIucT

The third parameter in bind_param is the data type of the $method parameter. What do you have in this variable?
Do you want it to be like this?
Try this - remove the third parameter.

$stmts = $this->db->prepare("SELECT user_id FROM usersSettings WHERE ?=?");
$stmts->bind_param("ss", $method);

S
Shadowbearer, 2018-02-05
@Shadowbearer

PDO cannot parameterize table and field names. Those. WHERE constructs? = ? or SELECT * FROM :table won't work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question