Why $results = DB::select works while DB::table is not? - php

I have this mysql driver
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', '{database}'),
'username' => env('DB_USERNAME', '{user}'),
'password' => env('DB_PASSWORD', '{pass}'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
and in my database.php
I have 'default' => env('DB_CONNECTION', 'mysql'),
So When I run in the controller
$results = DB::table('adventureworks2017.sales.store')->get()-first();
It gives me 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 '.`store`' at line 1 (SQL: select * from `adventureworks2017`.`sales`.`store`)
while
$results = DB::select('SELECT * FROM adventureworks2017.`sales.store` limit 5'); it workes
Any reason why query builder is not working wile passing statement works?

You can try passing a raw expression for the table name so the query builder doesn't try to decipher your dot notation into database and table, I suppose.
DB::table(DB::raw('sales.store'))->first();
Or even
DB::table(DB::raw('`sales.store`'))->first();

Related

"Access denied for user, using password: yes" Laravel 8 Heroku

Here is the config/database.php file that I modified using this tutorial. It says that I need to attach the $DATABASE_URL=parse_url("mysql://####"); code block above the said php page. (the tutorial is using postgreSQL, while my system needs to use MySQL).
// attached $DATABASE_URL=parse_url("mysql://####") on the topmost part of the page
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => $DATABASE_URL['host'], // edited this line
'port' => $DATABASE_URL['port'], // edited this line
'database' => ltrim($DATABASE_URL['path'], '/'), // edited this line
'username' => $DATABASE_URL['user'], // edited this line
'password' => $DATABASE_URL['pass'], // edited this line
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
below is the original mysql code
// 'mysql' => [
// 'driver' => 'mysql',
// 'url' => env('DATABASE_URL'),
// 'host' => env('DB_HOST', '127.0.0.1'),
// 'port' => env('DB_PORT', '3306'),
// 'database' => env('DB_DATABASE', 'forge'),
// 'username' => env('DB_USERNAME', 'forge'),
// 'password' => env('DB_PASSWORD', ''),
// 'unix_socket' => env('DB_SOCKET', ''),
// 'charset' => 'utf8mb4',
// 'collation' => 'utf8mb4_unicode_ci',
// 'prefix' => '',
// 'prefix_indexes' => true,
// 'strict' => true,
// 'engine' => null,
// 'options' => extension_loaded('pdo_mysql') ? array_filter([
// PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
// ]) : [],
// ],
error ouput when running the system with the said problem
SQLSTATE[HY000] [1045] Access denied for user '{{redacted username}}'#'{{redacted ip address}}' (using password: YES) (SQL: select * from video_links where home_video = true and video_links.deleted_at is null limit 1)
Another problem I encounter while running the migration command in the Heroku CLI is this:
PHP Warning: Undefined array key "port" in /app/config/database.php on line 51
It seems like the $DATABASE_URL=parse_url("mysql://####") is not fetching the port value in its array.
How should I approach this?
You can leave config/database.php unchanged.
And add an ENV var DATABASE_URL:
In the settings or via CLI:
heroku config:set DATABASE_URL=YOUR_URL

Not able to connect Laravel app to database which is hosted on Azzure over ssl

I am struggling in connecting Laravel app with MYSQL database. I can connect to database over MySQL Workbench and also with PDO connection custom string but with Laravel I cannot. Not sure what is wrong.
Custom connection string: It works file is inside laravel app. I put this just for checking
$conn = mysqli_init();
mysqli_ssl_set($conn,NULL,NULL, "BaltimoreCyberTrustRoot.crt.pem", NULL, NULL) ;
mysqli_real_connect($conn, 'sever.host', 'server-username', 'server-password', 'database', 3306, MYSQLI_CLIENT_SSL);
if (mysqli_connect_errno($conn)) {
die('Failed to connect to MySQL: '.mysqli_connect_error());
}
else{
echo "Connected";
}
Then I added in .env file following, beside defining username password host and db name.
BaltimoreCyberTrustRoot.crt.pem file is on same path as .env file
MYSQL_SSL_KEY=BaltimoreCyberTrustRoot.crt.pem
MYSQL_SSL_CERT=BaltimoreCyberTrustRoot.crt.pem
MYSQL_SSL_CA=BaltimoreCyberTrustRoot.crt.pem
MYSQL_SSL_CIPHER=BaltimoreCyberTrustRoot.crt.pem
MYSQL_SSL=true
In config/database.php looks like this
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
'options' => (env('MYSQL_SSL')) ? [
PDO::MYSQL_ATTR_SSL_KEY => env('MYSQL_SSL_KEY'), // /path/to/key.pem
PDO::MYSQL_ATTR_SSL_CERT => env('MYSQL_SSL_CERT'), // /path/to/cert.pem
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_SSL_CA'), // /path/to/ca.pem
PDO::MYSQL_ATTR_SSL_CIPHER => env('MYSQL_SSL_CIPHER')
] : [],
Not sure what is wrong, any idea?
I get these errors:
"error":"SQLSTATE[HY000] [2002] An attempt was made to access a socket in a way forbidden by its access permissions.
[{"file":"D:\home\site\wwwroot\vendor\laravel\framework\src\Illuminate\Database\Connection.php","line":624,"function":"runQueryCallback","class":"Illuminate\Database\Connection","type":"->","args":["select * from backend_token limit 1",[],{}]},
It helps to know which version of Laravel you are on to be absolutely sure, but on Laravel 8 my database config has:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
Don't change your database.php config but in your .env add MYSQL_ATTR_SSL_CA=/path/to/BaltimoreCyberTrustRoot.crt.pem
FYI sometimes if you are using private IP addresses then edit your hosts file
10.10.10.10 hostname.domain.com
Where of course the ip number matches the ip of the host.
If that doesn't work, add the cipher line from you config PDO::MYSQL_ATTR_SSL_CIPHER => env('MYSQL_SSL_CIPHER'),

local.ERROR: PDOException: SQLSTATE[HY000]: General error: 1615 Prepared statement needs to be re-prepared in happen in every morning

My laravel project is getting error every morning, and after some time it becomes okay. I don't understand what is the issue.
I googled it and see some similar issues. But it does not resolve my issue.
db version is 5.1.1
Enable the ATTR_EMULATE_PREPARES of PDO (disabled by default at Laravel) for resolve the problem. Just insert the option config:
'options' => [
\PDO::ATTR_EMULATE_PREPARES => true
]
For example
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'options' => [
\PDO::ATTR_EMULATE_PREPARES => true
]
],

Laravel groupBy not working , version 5.4.24

Here is my query, it works on all older versions.
$data = DB::table('applicant')
->groupBy('AppAffID')
->get();
Error i get is :
SQLSTATE[42000]: Syntax error or access violation: 1055 'comloant_loantreeAPI.applicant.AppID' isn't in GROUP BY (SQL: select * from `applicant` group by `AppAffID`)
I have tried the database change :
I added 'strict' => false, to database config.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
What is it that you want to achieve with the groupBy? If you want to sort on the AppAffID you should use the orderBy instead done like so:
$data = DB::table('applicant')
->orderBy('AppAffID', 'desc') //can also use 'asc' for ascending
->get();

Group by not working after upgrading to laravel 5.3

This is the code that was working on laravel 5.2
$menus = CmsMenuItem::groupBy('menu_id')->get();
but now it throws error
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1
of SELECT list is not in GROUP BY clause and contains nonaggregated
column 'convertifier_cms.cms_menu_items.id' which is not functionally
dependent on columns in GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by (SQL: select * from 'cms_menu_items' group
by 'menu_id')
I have also tried
`strict => false`
in database.php but no effect
Try this for database config.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
and use query like this way
$menus =DB::table('cms_menu_item')
->select('*')
->groupBy('menu_id')
->get();
As per this PR just try this in your database config
'strict' => false,
If not there is some known issue is going on.
Please refer these links PR & Issue
Please Go to your config/database.php folder. In mysql configuration array, change strict => true to strict => false, and everything will work nicely.

Categories