- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试在 PHP(特别是 Laravel)中创建一个 MySQL View ,但我遇到了一个奇怪的错误:
[PDOException]
SQLSTATE[42000]: SSyntax error or access violation: 1142 ANY command denied to user 'user'@'localhost' for table '/tmp/#sql_475_0'
直接在 MySQL 中运行创建语句可以正常工作。如果我删除 View 的连接,那么一切正常。用户具有完全权限 (GRANT ALL)。广泛的谷歌搜索没有返回任何类似的东西。
我的代码在下面,稍微简化了,在运行第 4 条语句创建 jobs_view 时产生了错误。
DB::statement("
CREATE VIEW quote_response_count AS (
SELECT job_id, COUNT(quotes.id) as total FROM quotes
INNER JOIN quote_requests on quote_requests.quote_id = quotes.id
INNER JOIN quote_responses on quote_responses.quote_request_id = quote_requests.id
GROUP BY job_id
);
");
DB::statement("
CREATE VIEW customer_paid AS (
SELECT job_id, SUM(amount) as total FROM transactions
WHERE category = 'customer payment' AND is_verified = 1
GROUP BY job_id, category
);
");
DB::statement("
CREATE VIEW company_paid AS (
SELECT job_id, SUM(amount) as total FROM transactions
WHERE category = 'company payment' AND is_verified = 1
GROUP BY job_id, category
);
");
DB::statement("
CREATE VIEW jobs_view AS (
SELECT
jobs.*,
IFNULL(customer_paid.total, 0) AS customer_paid,
IFNULL(company_paid.total, 0) AS company_paid,
IFNULL(quote_response_count.total, 0) AS responses_received,
price - IFNULL(customer_paid.total, 0) AS customer_owes,
cost - IFNULL(customer_paid.total, 0) AS owes_company,
(
deposit > 0 AND IFNULL(customer_paid.total, 0) >= deposit
) AS deposit_paid
FROM jobs
LEFT OUTER JOIN quote_response_count AS quote_response_count ON quote_response_count.job_id = jobs.id
LEFT OUTER JOIN customer_paid AS customer_paid ON customer_paid.job_id = jobs.id
LEFT OUTER JOIN company_paid AS company_paid ON company_paid.job_id = jobs.id
);
");
PHP 应用程序中 SHOW GRANTS 的输出如下:
[Grants for user@localhost] => GRANT USAGE ON *.* TO 'user'@'localhost' IDENTIFIED BY PASSWORD '****************************'
[Grants for user@localhost] => GRANT ALL PRIVILEGES ON `dbname`.* TO 'user'@'localhost'
下面非常简化的示例也产生相同的结果:
DB::statement("
CREATE TABLE table1 (
id int(11) NOT NULL AUTO_INCREMENT,
foo varchar(45) DEFAULT NULL,
PRIMARY KEY (id)
);
");
DB::statement("
CREATE VIEW view1 AS (
SELECT id, foo FROM table1
);
");
DB::statement("
CREATE VIEW view2 AS (
SELECT table1.id, view1.foo FROM table1
INNER JOIN view1 ON view1.id = table1.id
);
");
如果只是从 view1 选择,而不是加入,也会出现同样的错误。
我遇到此问题的系统是运行 PHP 5.5.23 和 MySQL 5.5.41 的 Ubuntu 12.04 服务器。
最佳答案
Eureka !对于面临此问题的任何其他人,问题是由于 Laravel 设置了以下 PDO 连接选项而发生的:
PDO::ATTR_EMULATE_PREPARES => false
我的解决方案不是为我的整个应用程序启用 Emulate Prepares,而是克隆我的数据库配置,覆盖 PDO 选项,然后在创建我的 View 时使用该连接:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'user',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'mysql-emulate-prepares' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'user',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'options' => array(
PDO::ATTR_EMULATE_PREPARES => true,
),
),
$rand = rand(10000, 99999);
DB::statement("
CREATE TABLE table".$rand." (
id int(11) NOT NULL AUTO_INCREMENT,
foo varchar(45) DEFAULT NULL,
PRIMARY KEY (id)
);
");
DB::statement("
CREATE VIEW view".$rand." AS (
SELECT id, foo FROM table1
);
");
DB::connection('mysql-emulate-prepares')->statement("
CREATE VIEW view".($rand+2)." AS (
SELECT table".$rand.".id, view".$rand.".foo FROM table".$rand."
INNER JOIN view".$rand." ON view".$rand.".id = table".$rand.".id
);
");
Ryan Vincent 的巨大功劳帮助我调试这个。
关于php - PDOException 语法错误或访问冲突 1142,当创建引用其他 View 的 View 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29460447/
我正在使用 symfony,当我在控制台中输入以下内容时: php app/console doctrine:schema:create 我有以下错误 [Doctrine\DBAL\Exception
我在 PHP 中收到以下错误: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2003]
这是我的代码: $db = new PDO('mysql:host=192.168.1.1;dbname=mydb'); 是否可以更改异常消息“无法连接到‘192.168.1.1’上的 MySQL 服
我在 PHP 中收到以下错误: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2003]
我安装了 Laravel 并从 git 服务器克隆了一个存储库以在临时服务器上使用。安装所有必需的工具后,我想迁移数据库。我现在得到的错误是: [2016-05-04 16:54:51] local.
如果我无法连接到数据库(例如 xamp 关闭或数据库连接关闭等),我会 try catch 错误。我尝试使用 PDO::errorCode() 但没有产生结果。 在我的php中,我有一个connect
当我运行以下代码时,出现此错误: Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: numb
我正在使用 Articles 类从我的数据库中提取关于 ..articles 的数据。我可以使用实例来插入数据、修改它,但我似乎无法从不同的页面集使用它。文件夹结构如下所示/cms/conf.php(
这个问题在这里已经有了答案: PHP Fatal error: Uncaught PDOException: could not find driver (4 个答案) PDOException “
这段代码工作正常,但我想处理 exception如果有任何问题,所以我故意在 query 中犯了一个语法错误但没有任何反应。下面是代码 try { $sql = "INSERT INTO jo
我不知道如何在下面的代码中捕获PDOException,请告诉我在下面的代码中哪里抛出异常? 我有(目录): - folder -1) b.php -2) c.php - au
我有一段简单的代码,在 PHP 中使用 PDO: $conn = new PDO('mysql:host=localhost;dbname=someDatabase', $username, $pas
我目前正在通过终端在 Laravel 中进行迁移,并且在尝试使用 php artisan migrate 时出现这两个错误;我将在下面打印错误: Exception trace: 1 PDOE
try { self::$dbinstance = new PDO( "mysql:host=$c[host];dbname=$c[dbname]", $c['user'],
我目前正在完善我的网站,一切正常但我一直收到错误。它不会破坏任何东西,但看起来很烦人。我一直在尝试很多东西,但我需要一些新的眼光来看待这个问题:/ 错误 PHP Fatal error: Cannot
获取POST传递过来的信息,将字符串中的所有空格去掉,然后启动一个新的pdo实例,连接mysql,将POST传递过来的信息插入到表中。 $title = trim($_POST["title"]);
我可以将迁移迁移到数据库,但是当我访问公用文件夹时,我收到一个 PDOException : SQLSTATE[28000] [1045] 用户“root”@“localhost”访问被拒绝(使用密码
我在 CentOS 7 上使用 PHP 5.6 运行 MariaDB。这是我的连接字符串: try { $connStr = 'mysql:host=server;dbname=mysql';
try { self::$dbinstance = new PDO( "mysql:host=$c[host];dbname=$c[dbname]", $c['user'],
所以我正在尝试为这样的 Laravel 应用程序编写单元测试: protected function setUp() { parent::setUp(); $this->disable
我是一名优秀的程序员,十分优秀!