PDO does not recognize column with special character - php

I'm trying to create an application with SimpleMVC (PHP Framework) and in the database there is a column with the name "contraseña".
When trying to make any query in this column, the PDO returns this error:
SQLSTATE [HY093]: Invalid parameter number: parameter was not defined
There is how to make the PDO recognize special characters?
Controller:
$servidor = array(
'cuenta' => $usuario,
'contraseña' => $senha,
'ipRegistro' => $ip,
'apodo' => $apelido
);
$this->_model->insert_server($servidor);
Model:
public function insert_server($data) {
$this->_db->insert("cuentas",$data);
}
Link to the SimpleMVC Framework: PHP Framework - SimpleMVC

Wrap the column bame in backticks, like so:
$servidor = array(
'cuenta' => $usuario,
'`contraseña`' => $senha,
'ipRegistro' => $ip,
'apodo' => $apelido
);
Might be something worth mentioning to the SimpleMVC developers.

Related

Yii force lowercase column names in Oracle

I found it and write here if it will be useful for someone. By default Yii framework uses PDO and pdo serves oracle column names in uppercase. To force returning them in lowercase U have to create custom connection and set PDO attribute, like:
class COraConnection extends CDbConnection
{
protected function initConnection($pdo)
{
parent::initConnection($pdo);
$pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
...
}
}
I checked, that U can set it directly on your db, without creating custom CDbConnection.
Say your oracle connection name in main.php is
'dbora' => array(
'class' => 'CDbConnection',
'connectionString' => 'oci:dbname=192.168.0.1:1521/shop;charset=CL8MSWIN1251',
'username' => 'dbuser',
'password' => 'dbpwd',
),
Just write it where U need it:
$db = Yii::app()->dbora;
$db->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);

Problems connecting to db using Zend db as standalone

I am migrating from ZF1 Zend_db to ZF2. I have problems connecting to the db and making a simple query. Appreciate if someone can guide me along. Below is my code.
This is how i connect to db
use Zend\Db\Adapter\Adapter;
$dbo = new Zend\Db\Adapter\Adapter(array(
'driver' => 'pdo_mysql',
'database' => DB_PREFIX.DB_NAME,
'username' => DB_USER,
'password' => DB_PW
));
Zend_Registry::set('db', $dbo);
This isan example how i use it.
$this->dbo = Zend_Registry::get('db');
function createData($message,$tags,$userid,$imgsrc){
$data= array(
'message' => $message,
'tags' => $tags,
'imgsrc' => $imgsrc,
'createdtimestamp'=>new Zend_Db_Expr('NOW()'),
'userid' => $userid);
$this->dbo->insert('mydata', $data);
return $this->dbo->lastInsertId();
}
I have an error. That is $dbo does not have select(),insert() methods etc. I could did it in ZF1 zend db.
Example of error message i received:
Fatal error: Call to undefined method
Zend\Db\Adapter\Adapter::select() in
You seem to think that ZF2 and ZF1 are (and should be) class-compatible. The problem is, they're not - at least in regards to Zend\Db\Adapter. That one in ZF2 is much more concise: while it has query method, its main responsibility is abstracting the DB connection itself.
I suppose what you're looking for is located in Zend\Db\Sql area. Assuming you have your DB connection in $dbo, you can create an Sql object based on it:
use Zend\Db\Sql as Sql;
$sql = new Sql\Sql($dbo);
... then create another object (of Zend\Db\Sql\Insert class):
$insert = $sql->insert('mydata');
$insert->values([
'message' => $message,
'tags' => $tags,
'imgsrc' => $imgsrc,
'createdtimestamp'=>new Sql\Expression('NOW()'),
'userid' => $userid
]);
... then use this object in query:
$insertString = $sql->getSqlStringForSqlObject($insert);
$results = $dbo->query($insertString, Adapter::QUERY_MODE_EXECUTE);

PHP 5.3.2 alternative to using $this inside an anonymous function?

I am using Laravel 4 and PHP to build a new application. It works fine on my dev server running PHP 5.4.x however my boss insist that it has to run version 5.3.2
I have spent the whole day fixing everything to work with 5.3.2 and almost have everything, so I thought, until I ran into an issue with the code below.
My problems start at this line...
DB::transaction(function($clock_in_webcam_image) use ($clock_in_webcam_image)
I believe this type of code might not work with this version of PHP? If that is the case, what are my options to run this same code or have it doing the same action?
Would appreciate any help with this. Very unfortunate that my boss told me straight out that no he will not allow us to update to a newer PHP so I am stuck in a hard spot right now
// Create a new time card record when a User Clocks In
public function createTimeCard($clock_in_webcam_image) {
// Create both Timecard and timecard record tables in a Transaction
DB::transaction(
function ($clock_in_webcam_image) use ($clock_in_webcam_image) {
$timeCard = DB::table('timeclock_timecard')->insertGetId(
array(
'user_id' => $this->user->user_id,
'clock_in_datetime' => $this->dateTime->format($this->dateFormat),
'clock_in_timestamp' => $this->dateTime->getTimestamp(),
'clock_in_webcam_image' => $clock_in_webcam_image
)
);
$timeCardPunchEntry = DB::table('timeclock_punch_entry')
->insertGetId(
array(
'timecard_id' => $timeCard,
'user_id' => $this->user->user_id,
'created_at_datetime' => $this->dateTime->format($this->dateFormat),
'created_at_timestamp' => $this->dateTime->getTimestamp(),
'clock_type' => 'clock_in',
'webcam_image' => $clock_in_webcam_image
)
);
return $timeCard;
}
);
}
UPDATE
In response to bansi's comment...is this what you mean to do...
DB::transaction(function() use($myModel){
$myModel->updateTable1();
$myModel->updateTable2();
})
Before PHP 5.4.0, you could not use $this inside an anonymous function. There is a simple workaround though where you can use the use construct to pass variables into the functions scope. Also, you are using the use construct incorrectly as $clock_in_webcam_image is not defined in the parent scope.
$user = $this->user;
$dateTime = $this->dateTime;
DB::transaction(function($clock_in_webcam_image) use ($user, $dateTime) {
// snip
array(
'user_id' => $user->user_id,
'clock_in_datetime' => $dateTime->format($this->dateFormat),
'clock_in_timestamp' => $dateTime->getTimestamp(),
'clock_in_webcam_image' => $clock_in_webcam_image
)
// snip
});
try this. please check you don't have another insertTimecard defined.
// Create a new time card record when a User Clocks In
public function createTimeCard($clock_in_webcam_image)
{
// Create both Timecard and timecard record tables in a Transaction
DB::transaction($this->insertTimecard($clock_in_webcam_image));
}
private function insertTimecard($clock_in_webcam_image)
{
$timeCard = DB::table('timeclock_timecard')->insertGetId(
array(
'user_id' => $this->user->user_id,
'clock_in_datetime' => $this->dateTime->format($this->dateFormat),
'clock_in_timestamp' => $this->dateTime->getTimestamp(),
'clock_in_webcam_image' => $clock_in_webcam_image
)
);
$timeCardPunchEntry = DB::table('timeclock_punch_entry')->insertGetId(
array(
'timecard_id' => $timeCard,
'user_id' => $this->user->user_id,
'created_at_datetime' => $this->dateTime->format($this->dateFormat),
'created_at_timestamp' => $this->dateTime->getTimestamp(),
'clock_type' => 'clock_in',
'webcam_image' => $clock_in_webcam_image
)
);
return $timeCard;
}

Zend Framework 2 query to Oracle in incorrect format

I guess that the way Zend use to build sql statement don't work with Oracle.
I'm using Oracle in my local Windows PC.
My PHP works correct with Oracle already with oci8 extension, I configured following the link below:
http://www.oracle.com/technetwork/articles/technote-php-instant-084410.html
I'm trying to connect ZF2 with Oracle. I'm using the tutorial code at Zend website:
http://zf2.readthedocs.org/en/latest/index.html#userguide
This code works perfectly with mySQL. I'm changing the database configuration to use Oracle instead.
Below is my config:
global.php
< ?php
return array(
'db' => array(
'driver' => 'Oci8',
'host' => 'localhost/orcl',
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter'
=> 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);
?>
local.php (this work because I created a user 'test' with privileges on Oracle with, I tested this user with PHP and SQL script)
< ?php
return array(
'db' => array(
'username' => 'test',
'password' => 'test',
),
);
After configuring, I try to browse the url, the error appear say "table or view does not exist". This is Oracle error, it means Zend connected to Oracle but something wrong in SQL statement.
After some debug, I see the sql statement is:
SELECT "album".* FROM "album"
It' error because Oracle don't want to receive double quote.
I tried some hard-code in file /Zend/Db/Adapter/Driver/Oci8/Statement.php, function setSql line 112, change to:
public function setSql($sql)
{
$this->sql = $sql;
$this->sql = "SELECT album.* FROM album";
return $this;
}
(remove double quote on query)
It's work!!!
I think there're some other configuration so make Zend work correctly.
Please help me! Thank you
Same problem was here: ZF2 IBM
Solution: quote_identifiers == false:
'db' => array(
'driver' => $dbParams['driver'],
'connection_string' => $dbParams['database'],
'username' => $dbParams['username'],
'password' => $dbParams['password'],
'character_set' => $dbParams['character_set'],
'platform_options' => array('quote_identifiers' => false)
),

Execute params with doctrine using named parameters

I'm encountering a strange problem with doctrine and named parameters.
Here is a query which actually works perfectly with this set of parameters (dynamic in my code):
$params = array( ':id_editeur' => 1,
':nom_editeur' => 'Test');
public function updateById($params)
{
Doctrine_Query::create()
->update('Editeur e')
->set('e.nom_editeur', ':nom_editeur')
->where('e.id_editeur = :id_editeur')
->execute($params);
}
Now i have another function
public function findAll($params)
{
$query = Doctrine_Query::create()
->from('Editeur e')
->orderBy(':orderby')
->limit(':limit')
->offset(':offset');
return $query->execute($params);
}
With these parameters:
$params = array( ':orderby' => ('e.id_editeur ASC'),
':limit' => (10),
':offset' => (20));
And even if it's the same mechanism i get the following error
Invalid parameter number: number of
bound variables does not match number
of tokens
Any idea of the reason? By the way, it works if I fill the orderby, limit and offset directly in the function in the classical way.
The params var cannot contain ":" character...
Try replacing:
$params = array( ':id_editeur' => 1,
':nom_editeur' => 'Test');
to:
$params = array( 'id_editeur' => 1,
'nom_editeur' => 'Test');
Try remove the parenthesis in params array.
$params = array( ':orderby' => 'e.id_editeur ASC',
':limit' => '10',
':offset' => '20');

Categories