yii2 - SQLSTATE[42000]: Syntax error or access violation: 1064 and table prefix - pdo

When i'm write:
public static function tableName()
{
return '{{%сurrencies}}';
}
I have error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '{%сurrencies}}' at line 1
The SQL being executed was: SELECT COUNT(*) FROM {{%сurrencies}}
When i'm write:
return 'currencies';
All is fine. What's wrong?

If you need to add any table prefix just add {tableName}.
By setting this property, any token like '{{tableName}}' will be replaced by 'prefixTableName'.
Here is the Documentation for table prefix
(http://www.yiiframework.com/doc/api/1.1/CDbConnection#tablePrefix-detail)
You can set your table prefix in config file as below:
'db'=>array(
'connectionString' => 'xxxxx',
'username' => 'xxxxx',
'password' => 'xxxxx',
'tablePrefix' => 'tbl_', // Table Prefix Here
),
Also can you please eloborate What exactly you want to do, So that I can be more specific in terms of my answer.

Related

Appending json object in database

Im trying to append new object into json array field
UPDATE phone_messages SET messages = JSON_ARRAY_APPEND(messages, '$', CAST('?' AS JSON)) WHERE charId = ? AND number = ? this is SQL I'm using but its giving me error (You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'JSON)).

Problems regarding usage of Bodyguard library in Phoenix

We are trying to create a sample project using the Bodyguard library in Phoenix. We are just going to set some rules for users, as shown on the GitHub page, https://github.com/schrockwell/bodyguard. However, we are facing errors regarding the conn variable in lib\bg_web\controllers\post_controller.ex. We have uploaded our project in https://github.com/yashdani/bg. Please help us to identify the errors. Also, tell us what else we need to code in the project. We are really stuck, and we could not find any more information from the online documentation.
This is one of the errors:-
C:\Users\yashd\bg>mix phx.server
Compiling 18 files (.ex)
warning: variable "conn" does not exist and is being expanded to "conn()", please use parentheses to remove the ambiguity or change the variable name
lib/bg_web/controllers/post_controller.ex:6
== Compilation error in file lib/bg_web/controllers/post_controller.ex ==
** (CompileError) lib/bg_web/controllers/post_controller.ex:6: undefined function conn/0
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) lib/kernel/parallel_compiler.ex:198: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6
You're missing the keyword do, instead of:
def update(conn, %{"id" => id, "post" => post_params})
should be:
def update(conn, %{"id" => id, "post" => post_params}) do

Is it possible to retrieve the namespace of a raised error?

When I raise an error from within an XQuery query, for instance with:
error( fn:QName( 'http://example.com', 'XMPL0001' ), 'Conflict' )
... the following is returned by BaseX (be it when communicating with the server, or from within the GUI)
Stopped at ., 1/7:
[XMPL0001] Conflict
Is it somehow possible to retrieve the namespace of the error (in this case http://example.com) as well?
I'm using a customized PHP client and I would like to use this information to prevent possible (future) conflicts with my custom error codes and parse the errors to throw either a standard BaseX\Exception or a custom SomeNamespace\Exception, depending on the namespace of the error.
I could, of course, simply use another error code pattern than the typical ABCD1234 XQuery pattern, to prevent possible (future) error code conflicts, but the possible use of a namespace appeals to me more, because I can then define an uniform Exception interface, such as:
interface ExceptionInterface
{
public function getCategory(); // the 4 alpha character part
public function getCode(); // the 4 digit part
}
I'm currently using BaseX 7.7.2, by the way.
Yes, you can retrieve information about the error using a few variables in the error namespace, which are in scope of the try-catch statement, like so:
declare namespace err = "http://www.w3.org/2005/xqt-errors";
try {
error( fn:QName( 'http://example.com', 'XMPL0001' ), 'Conflict' )
}
catch * {
namespace-uri-from-QName($err:code)
}
This assumes that you are using XQuery 3.0.

PDO update column based on its current value not working

I have this PDO statement:
parent::$db->custom('UPDATE users_credits SET availabe = availabe - :reward_credits, used = used + :reward_credits WHERE user_id = :user_id', array(
'reward_credits' => $reward_credits,
'user_id' => $user_id
));
For some reasons it simply does not work. I tried the very same query on the DB manually and it works.
What's wrong with PDO and how do I achieve the very same result I would achieve normally?
Thanks for any suggestion
First of all. There is nothing wrong with PDO and never has been.
It is some your own custom code to blame.
Simple checklist to solve any PDO related problem
Make sure you can see all the PHP errors.
Configure PDO to throw exceptions in SQL errors, by calling this after connect
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Debug your code.

Share sqlite2 database with Yii webapp

Hi I've got a sqlite2 database that is being used by a PHP webapp and I want a Yii webapp to access it. At the moment I copied the db file to a local server and I've changed config/main.php to:
'db'=>array(
'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/thedatabase.db',
),
When I run the Gii model generator I get:
CDbException
CDbCommand failed to execute the SQL statement: CDbCommand failed to prepare the SQL statement: SQLSTATE[HY000]: General error: 26 file is encrypted or is not a database. The SQL statement executed was: SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name<>'sqlite_sequence'
BTW I am able to convert the database to SQL using
Exporting sqlite2 database to SQL
Though I want the database to stay as an sqlite2 database so that the Yii app can access up to date user info.
After some thought it looks like it would be simpler to just use some code from the old PHP project in the Yii project...
$db = sqlite_open($databasefilename, 0666, $sqliteerror);
$query = sqlite_query($db, "SELECT password FROM user WHERE username='$username'");
$row = sqlite_fetch_array($query);
if ($row) {
return $row[password];
}
Edit:
When using that code I get the following error:
undefined function sqlite_open()
I'm using PHP Version 5.4.7...
http://au1.php.net/manual/en/function.sqlite-open.php
Here it says:
PHP 5 < 5.4.0
I got it working with the sqlite2 database by using 'sqlite2:' instead of 'sqlite:'
'db2'=>array(
'class'=>'CDbConnection',
'connectionString' => 'sqlite2:'.dirname(__FILE__).'/../../../thedatabase.db',
),
I can do queries like this:
Yii::app()->db2->createCommand($sql)->queryAll());
To stop Error 500 Object configuration must be an array containing a "class" element 'class'=>'CDbConnection' is required in config/main.php. I think that info is already included in the first database connection somewhere.