From PHP, doing a DROP TABLE and a CREATE TABLE is successful, but doing a
LOAD FILE keeps failing, with the error "Access denied for user
'db_user'@'loca lhost' (using password: YES)". The DROP/CREATE code and the
LOAD FILE code are in separate .php files which I am running from my
browser, but both commands are executed with mysql_query(). Don't know what
I'm doing wrong, so I would appreciate any help with this.
Both files run the following code first, which works fine:
=============== =============== ====
// -----------------------------------------------------------------------
// Connect to MySQL server
// -----------------------------------------------------------------------
$link = mysql_connect($ db_host, $db_user, $db_password);
if (! $link) {
die("Could not connect to {$db_user}/{$db_password} on host
{$db_host}:\n" .
mysql_error() . end_body_html() );
exit(1);
}
else {
echo "Connected to MySQL on host {$db_host}.<p>\ n";
}
// -----------------------------------------------------------------------
// Drop and create PHC_DB database
// -----------------------------------------------------------------------
$drop_db_query = "DROP DATABASE IF EXISTS {$db_name}";
if (run_mysql_quer y($drop_db_quer y));
$create_db_quer y = "CREATE DATABASE IF NOT EXISTS {$db_name}";
if (run_mysql_quer y($create_db_qu ery));
// -----------------------------------------------------------------------
// Select database
// -----------------------------------------------------------------------
$db_selected = mysql_select_db ($db_name, $link);
if (! $db_selected) {
die("Could not select {$db_name} database." .
mysql_error() . end_body_html() );
exit(1);
}
else {
echo "Selected {$db_name} database.<p>\n" ;
}
=============== =============== ====
Here is the section of DROP/CREATE code, which also works fine:
=============== =============== ====
// -----------------------------------------------------------------------
// Drop and create REF_USER_LEVELS table
// -----------------------------------------------------------------------
$drop_table_que ry = "DROP TABLE IF EXISTS ref_user_levels ";
run_mysql_query ($drop_table_qu ery);
$create_table_q uery = <<<EOT
CREATE TABLE ref_user_levels (
user_level VARCHAR(10) NOT NULL,
actv_ind ENUM (
'A', -- active
'I' -- inactive
) NOT NULL,
max_counties INT(5),
description VARCHAR(100)
);
EOT;
run_mysql_query ($create_table_ query);
=============== =============== ====
Now here is the LOAD FILE code, which fails with
"Error: Access denied for user 'db_user'@'loca lhost' (using password: YES)
LOAD DATA INFILE '/mysubdir/ref_user_levels .txt' INTO TABLE
ref_user_levels ",
where db_user is the value of $db_user, and localhost is the value of
$db_host, as with the DROP/CREATE above.
$dat_file_path is based on DOCUMENT_ROOT, e.g., /mysubdir/
=============== =============== ====
// -----------------------------------------------------------------------
// Populate REF_USER_LEVELS table
// -----------------------------------------------------------------------
$populate_table _query = "LOAD DATA INFILE
'{$dat_file_pat h}ref_user_leve ls.txt' INTO TABLE ref_user_levels ";
run_mysql_query ($populate_tabl e_query);
=============== =============== ====
Here are the support functions called:
=============== =============== ====
function end_body_html() {
echo "\n</body>\n</html>";
return;
}
function run_mysql_query ($query) {
if (mysql_query($q uery)) {
echo "<span style='font-weight:bold;col or:green;'>Succ ess:</span>
<pre>" . $query . "</pre>\n<p>\n";
} else {
echo "<span style='font-weight:bold;col or:red;'>Error: </span> " .
mysql_error() . "<br>\n<pre>$qu ery</pre><br>\n" . end_body_html() ;
exit(1);
}
}
=============== =============== ====
Thanks in advance!
LOAD FILE keeps failing, with the error "Access denied for user
'db_user'@'loca lhost' (using password: YES)". The DROP/CREATE code and the
LOAD FILE code are in separate .php files which I am running from my
browser, but both commands are executed with mysql_query(). Don't know what
I'm doing wrong, so I would appreciate any help with this.
Both files run the following code first, which works fine:
=============== =============== ====
// -----------------------------------------------------------------------
// Connect to MySQL server
// -----------------------------------------------------------------------
$link = mysql_connect($ db_host, $db_user, $db_password);
if (! $link) {
die("Could not connect to {$db_user}/{$db_password} on host
{$db_host}:\n" .
mysql_error() . end_body_html() );
exit(1);
}
else {
echo "Connected to MySQL on host {$db_host}.<p>\ n";
}
// -----------------------------------------------------------------------
// Drop and create PHC_DB database
// -----------------------------------------------------------------------
$drop_db_query = "DROP DATABASE IF EXISTS {$db_name}";
if (run_mysql_quer y($drop_db_quer y));
$create_db_quer y = "CREATE DATABASE IF NOT EXISTS {$db_name}";
if (run_mysql_quer y($create_db_qu ery));
// -----------------------------------------------------------------------
// Select database
// -----------------------------------------------------------------------
$db_selected = mysql_select_db ($db_name, $link);
if (! $db_selected) {
die("Could not select {$db_name} database." .
mysql_error() . end_body_html() );
exit(1);
}
else {
echo "Selected {$db_name} database.<p>\n" ;
}
=============== =============== ====
Here is the section of DROP/CREATE code, which also works fine:
=============== =============== ====
// -----------------------------------------------------------------------
// Drop and create REF_USER_LEVELS table
// -----------------------------------------------------------------------
$drop_table_que ry = "DROP TABLE IF EXISTS ref_user_levels ";
run_mysql_query ($drop_table_qu ery);
$create_table_q uery = <<<EOT
CREATE TABLE ref_user_levels (
user_level VARCHAR(10) NOT NULL,
actv_ind ENUM (
'A', -- active
'I' -- inactive
) NOT NULL,
max_counties INT(5),
description VARCHAR(100)
);
EOT;
run_mysql_query ($create_table_ query);
=============== =============== ====
Now here is the LOAD FILE code, which fails with
"Error: Access denied for user 'db_user'@'loca lhost' (using password: YES)
LOAD DATA INFILE '/mysubdir/ref_user_levels .txt' INTO TABLE
ref_user_levels ",
where db_user is the value of $db_user, and localhost is the value of
$db_host, as with the DROP/CREATE above.
$dat_file_path is based on DOCUMENT_ROOT, e.g., /mysubdir/
=============== =============== ====
// -----------------------------------------------------------------------
// Populate REF_USER_LEVELS table
// -----------------------------------------------------------------------
$populate_table _query = "LOAD DATA INFILE
'{$dat_file_pat h}ref_user_leve ls.txt' INTO TABLE ref_user_levels ";
run_mysql_query ($populate_tabl e_query);
=============== =============== ====
Here are the support functions called:
=============== =============== ====
function end_body_html() {
echo "\n</body>\n</html>";
return;
}
function run_mysql_query ($query) {
if (mysql_query($q uery)) {
echo "<span style='font-weight:bold;col or:green;'>Succ ess:</span>
<pre>" . $query . "</pre>\n<p>\n";
} else {
echo "<span style='font-weight:bold;col or:red;'>Error: </span> " .
mysql_error() . "<br>\n<pre>$qu ery</pre><br>\n" . end_body_html() ;
exit(1);
}
}
=============== =============== ====
Thanks in advance!
Comment