Question

I have a strange issue with PHP PDO and MariaDB. If I make a View with the user "root" (default user):

-- Create the first table
CREATE TABLE `tableA` (
  `id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `name` VARCHAR(10) NOT NULL
);
INSERT INTO `tableA` (`name`) VALUES ('a');

-- Create the second table
CREATE TABLE `tableB` (
  `id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `id_A` TINYINT UNSIGNED NOT NULL,
  `data` VARCHAR(10) NOT NULL
);
INSERT INTO `tableB` (`id_A`, `data`) VALUES ('1', '...');

-- The view...
CREATE OR REPLACE VIEW `table_view` AS SELECT a.id, a.name, b.data FROM tableA a, tableB b WHERE a.id=b.id_a;

and then I change the "root" name:

RENAME USER 'root'@'localhost' TO 'uroot'@'localhost';
RENAME USER 'root'@'127.0.0.1' TO 'uroot'@'127.0.0.1';
RENAME USER 'root'@'::1' TO 'uroot'@'::1';

then I have a bad login error when I try to SELECT the view:

Error: SQLSTATE[HY000]: General error: 1449 The user specified as a definer ('root'@'localhost') does not exist.

I can access to tableA and tableB but I can't access to table_view. Why ? To correct the error I have to recreate the view:

CREATE OR REPLACE VIEW `table_view` AS SELECT a.id, a.name, b.data FROM tableA a, tableB b WHERE a.id=b.id_a;

Full example:

<?php
define('DB_USER',       'uroot');
define('DB_PASSWRD',    'root');
define('DB_DBNAME',     'test');

// SQL Script.
/*
-- Create the first table
CREATE TABLE `tableA` (
  `id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `name` VARCHAR(10) NOT NULL
);
INSERT INTO `tableA` (`name`) VALUES ('a');

-- Create the second table
CREATE TABLE `tableB` (
  `id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `id_A` TINYINT UNSIGNED NOT NULL,
  `data` VARCHAR(10) NOT NULL
);
INSERT INTO `tableB` (`id_A`, `data`) VALUES ('1', '...');

-- The view...
CREATE OR REPLACE VIEW `table_view` AS SELECT a.id, a.name, b.data FROM tableA a, tableB b WHERE a.id=b.id_a;

-- Rename "root"
RENAME USER 'root'@'localhost' TO 'uroot'@'localhost';
RENAME USER 'root'@'127.0.0.1' TO 'uroot'@'127.0.0.1';
RENAME USER 'root'@'::1' TO 'uroot'@'::1';

-- Now you can execute the PHP script
*/

$_db = null;

// Connection
try {
    $_db = new PDO('mysql:host=localhost;dbname='.DB_DBNAME, DB_USER, DB_PASSWRD, array(PDO::ATTR_PERSISTENT => false));
    $_db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

}catch( PDOException $e ){
    echo 'Error : ',$e->getMessage(),'<br />',"\n"
    ,'N° : ',$e->getCode(),"\n";
    exit();
}


// Make a SQL Query
$sql = 'SELECT * FROM table_view;';
try {
    $res = $_db->prepare($sql);
    $res->execute();
    $res->setFetchMode(PDO::FETCH_ASSOC);

    echo '<pre>';
    while( ($row=$res->fetch()) !== false )
    {
        var_export($row);
    }
    echo '</pre>';

}catch( PDOException $e ){
    echo 'Error: ',$e->getMessage(),'<br />',"\n"
    ,'N°: ',$e->getCode(),"\n"
    ,'REQ SQL: ',$sql,'<br />',"\n";
    exit;
}
?>

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top