I've just gotten this error from a get_record query:
$sql = "select * from {user_info_data} where data = ? ";
causes the error, and...
$sql = "select * from {user_info_data} where " . $DB->sql_compare_text('data') . " = ? ";
doesn't (from an example I found quickly). In this case 'data' is a text field. You can't compare text fields without wrapping them in this. I *think* it's digs into the db library depths and modifies to suit different database types. For MySQL and Postgres it appears to do nothing at all though.
That's fair enough, but I'm not creating an sql string, I'm just using $DB->get_record(), which I would assume should do the necessary for me. Should I file this as a bug?
$DB->get_record( 'some_table', array('textfield' => 'fred') )
isn't going to work either, and should be...
$DB->get_record( 'some_table', array(sql_compare_text('textfield') => 'fred'))
...which I think is a syntax error (?), but you get my point I hope.
Thanks for that Howard, I tried exactly as you suggest there and I got the same error.
What I would expect to happen in the first case above, is for the parameters to be passed to where_clause(), and where_clause() to say "that's a text field, I need to run it through sql_compare_text()" rather than one having to do so before it's passed to get_record() (after all, where_clause() would have no way of knowing this had already happened). Instead, it just seems to say "that's a text field, forget it". Unless that's the intended behaviour, in which case the error message needs to be changed to make that clear and suggest a solution.
In the meantime, I'm using get_record_select() with sql_compare_text() around the field name in the WHERE clause, which seems to work around the issue.
I don't mind that being the case, as long as they *actually* say that in the error message
I've created MDL-27629 to track this.
Apology for reviving a couple of years old post, but I am facing the same error here. And to my knowledge, I think I pin pointed the problem:
In line 3234:
'eventtype' => 'facetofacesession' is causing the error (I guess)...
Any idea to resolve this!
Thank you.
@Avas B : I am also facing the same issue now you have solved this?
If solved please give me solution
Thank you
example code for the sql_compare_text()
$compare_scale_clause = $DB->sql_compare_text('shortname') . ' =
' . $DB->sql_compare_text(':short');
compare text first and then append the variable to your query
$DB->get_records_sql("select * from {table name} where $compare_scale_clause", array('short' => $sh));
I know this is really really really old, but since I arrived here because I had the same error, I thought I'd explain why the error happens.
It's because the field name you (we) are using are MySql reserved words - in my case "code" and in the examples above "name" and "data". Usually, if you do an ordinary select command, you'll wrap those field names in backticks ``, but using the get_record(s) method doesn't add those for you.
The work around is to either use the get_records_sql and put in your own backticks, or change the field name in the table, if that's within your power.
Field names to avoid: https://dev.mysql.com/doc/refman/5.5/en/keywords.html
pip pip
Hello,
Maybe your version is correct, but it isn't applicable to my issue:
I face today the 'textconditionsnotallowed' with $DB->get_field function (though I used it many times, don't know what's wrong now...) and my DB is MSSQL, so I also checked for not using it's reserved words..
My code is:
$table='mytable';
$params = ['userid'=>2, 'mynote'=>'some note'];
$id=$DB->get_field($table,'id', $params]);
In the 'mytable', the field 'userid' is type of bigint and 'mynote' is varchar(MAX), so can't get the point of the error...
Also I tried changing single quotes to double quotes - no effect.
How to resolve this, does anybody know, please?