Class 'Silex\Provider\PDO' not found. How to solve? - mysql

I'm using Silex as well as Doctrine. It's worked great for me for everything until I needed to bindValue for a LIMIT value. PDO's default behavior is to insert quotes around the number, which is obviously not workable. So, the solution is to set the data_type parameter. Unfortunately, it throws an error.
My Code
$start_num = 3;
$stmt = $app['db']->prepare('SELECT * FROM myTable LIMIT ?,10');
$stmt->bindValue(1, $start_num, PDO::PARAM_INT);
The Error
Fatal error: Class 'Silex\Provider\PDO' not found in ...
Most answers I've found regarding this issue say that it's a telltale sign of PDO not being compiled/enabled, however I've been using Doctrine (which relies on PDO?) successfully for a while with no problem.
Is this an issue with Doctrine? Is there something I'm doing wrong with my code?

This is a namespacing issue, if this code is in a class under the Silex\Provider namespace
Try
\PDO::PARAM_INT

Related

Doctrine get result issue

I got DQL like this:
$sql =$qb->select('c')
->from('Cusomter','c')
->where('c.login = :login')
->setParameter('login',$login);
$rs = $sql->getQuery()->getResult(Query::HYDRATE_OBJECT);
But I got the following error
Fatal error: Class 'customer\Query' not found in
MyNameSpace\customer.php
Do i need to use any namespace?
Thanks.
you dont need to actually tell getResult to hydrate_object, its default behaviour, and therefore called from the method itself.
try:
$rs = $sql->getQuery()->getResult();
if you want to know the right namespace for the constant though its:
Doctrine\ORM\AbstractQuery::HYDRATE_OBJECT
Try:
use Doctrine\ORM\Query;
or
\Doctrine\ORM\Query::HYDRATE_ARRAY

Codeigniter - Stop escape in function "from" of Query Builder

I have a problem with the way of escape of Query Builder in Codeigniter 3.0.
For example, this code
echo $this->db->select('ROUND(3.456, 1) AS T1')->get_compiled_select();
Return:
SELECT ROUND(3.456, `1)` AS `T1`
The function put backticks after a coma, but this is solved by setting FALSE the second parameter. But the function "from" put always backticks:
echo $this->nm->db->from('(SELECT ROUND(3.456, 1)) AS T1')->get_compiled_select()
Return:
SELECT * FROM (SELECT ROUND(3.456, `1))` AS `T1`
I'm using Codeigniter 3.0. The problem exists since Codeigniter 2.2. I need use the Query Builder beacuse it's very easy to use, but its escape method is troublesome. How stop the escaping in the function from?
Thanks.
I found a solution, but I'm not sure use it. In "database.php" file should be added a new element for the config array of database connection.
$db['default']['_protect_identifiers'] = FALSE;
That code disable the escape mode. But is it advisable? What is the risk of disabling the escaping system?
Thanks.
or you can disable on current query and enable it again rather on whole app. Before query, insert
$this->db->_protect_identifiers = FALSE;
and after query, set to TRUE to enable it again.

MySQL Query Error Validation

I running a Mysql Query to select some data, Sometimes i get a error called
mysql_fetch_assoc() expects parameter 1 to be resource, boolean given
when i executed this following code,
$result = $this->db->execute($sql);
for ($i = 0; $data[$i + 1] = mysql_fetch_assoc($result); $i++);
array_pop($data);
how do i optimize this coding to prevent any errors ?
is there anything wrong with it ? should i ignore this error ?
That means that the query is buggy, whyever, most likely because you construct it using components from sources which you do not really check enough. A buggy statement throws an error (since no result can be computed). That error is returned as false instead of a mysql result ressource. Since you do not check if the query succeeded but blindly try to retrieve details from the result, you get this second error.
So there are four things you have to invest into:
you should always check if a query succeeded at all:
enclose your query into a conditional: if (FALSE!==($result=$this->db->execute($sql))) and only retrieve from the result ressource if that condition resolves to true.
make sure you really (really!) check all input data you use to construct your query. Checking here also means to encode and escape it correctly, also see point 4. for this.
in cases like this it is important to analyze what exactly it is that is going wrong. There is little sense in guessing what might be going wrong. So in addition to checking if the query succeeded at all (1.) you should also take a look at the error message mysql throws if this is not the case. Use the method mysql_error() for this. It is well documented just as every other function too.
you should rework your code and migrate from phps old, long deprecated mysql extension to either mysqli or PDO. Both are php extensions that offer more security against constructing buggy statements. Read about "prepared statements" and "parameter binding" for this.

Entity Framework : Set MySQL custom environment variables

I have an issue with Entity Framework 5.0. I'm working with Silverlight 5 and MySQL 5.6 too.
I need to set an environment MySQL variable before each connexion to the MySQL server.
E.g
SET #my_var = 'test';
Under Mysql I don't have any issues.
The following raises an EntityFrameworkException (syntax error near '#').
this.ObjectContext.CreateQuery<object>(" SET #my_var = 'test' ");
OR
this.ObjectContext.CreateQuery<object>(" CALL set_my_var('test') ");
This last method raises a MySQLException saying that a DataReader is already open and need to be closed.
this.ObjectContext.ExecuteStoreQuery<object>(" CALL set_my_var('test') ", null);
I also tried to set a MySQL system environment (no '#') with the same result every time.
Any help will be much appreciated !
Thank you.
I tried so many things that I misspelled my variable in my code.
So the following finaly worked : ctx.ExecuteStoreCommand("SET #my_var = 'test'");
I decided to leave the instruction in the method Initialize of my domain service. This method is inherited of the LinqToEntitiesDomainService class.
But you need to set Allow User Variables=True in your MySQL connection string
(ref : Is it possible to use a MySql User Defined Variable in a .NET MySqlCommand?)
You simply need to use a recent version of the MySQL Connector because older versions use the '#' mark to define SQL parameters so it could conflict with custom variables. Now it uses the '?' mark : http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlcommand.html
My library was already up to date (6.6.5).
Thank you for the help !
Since your statement is not a query (i.e. does not return any result) you should use ExecuteStoreCommand. Something like this should work:
ctx.ExecuteStoreCommand("SET #my_var = 'test'")

Codeigniter order_by with CONVERT

I have to order a selection by CONVERT(`codExtern`, SIGNED) with codeIgniter.
If I use it like this:
$this->db->order_by(" CONVERT(`codExtern`, SIGNED) ");
the codeigniter puts the SIGNED word between `-s, like:
CONVERT(codExtern, `INTEGER` )
How can I make it work?
Unfortunately you can't disable identifier protection with parameters like in the select() method. CI will call CI_DB_driver::_protect_identifiers on the input if it has a , in it.
Currently you can workaround this if you set the supposedly "private" property $_protect_identifiers to false on your $this->db before calling the order_by method so when it runs it will skip this, and then flip it back (it helps with problematic column/table names for example). This is probably not a really good idea, in future CI versions this property might became really private and your code will break.
Unfortunately the database library cannot be extended, but if you are not afraid of modifying the files under system you can create an exception in the order_by() method just like the "order by random()" got one.