PHP PDOException - 参数号无效:列/参数从1开始[重复]

My code is:

.....
.....

$sql = 'SELECT '.$return_fields.' FROM '.$table.' WHERE '.$search_field.'=:'.$search_field;
$stmt = $conn->prepare($sql);
$stmt->bindParam($search_field, $search_val);
$stmt->execute();

....
....

where $search_field = 'reg_user_linked', $search_val = 'aa@gmail.com'.

This error occurs when execute the statement and I couldn't figure out why:

Invalid parameter number: Columns/Parameters are 1-based

Can anyone help?


After hours of trying, I found out that this error only occurs when I was in debugging mode. If I ran the code and print the result, it works..... Does anyone know why this happens? (My IDE is NetBeans 7.2, debug tool is xdebug)...

</div>

When binding the parameter you need to specify the : in the parameter name:

$stmt->bindParam(':' . $search_field, $search_val);

You are getting an error because this is missing and the code falls back to expecting an integer value to indicate the parameter position (as if you were using ?-style parameters).

Note this description of the first parameter for PDOStatement::bindParam() from the documentation.

parameter

Parameter identifier. For a prepared statement using named placeholders, this will be a parameter name of the form :name. For a prepared statement using question mark placeholders, this will be the 1-indexed position of the parameter.