Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exporting results of SQL query causes "bad parameters" error when a database is not selected first #14773

Closed
jormaster3k opened this issue Dec 7, 2018 · 14 comments
Assignees
Labels
Bug A problem or regression with an existing feature
Projects
Milestone

Comments

@jormaster3k
Copy link

jormaster3k commented Dec 7, 2018

Describe the bug

Exporting results of SQL query causes "bad parameters" error when a database is not selected first

To Reproduce

Steps to reproduce the behavior:

  1. Login to PHPMyAdmin
  2. Click on "SQL" tab
  3. Enter a valid query, e.g. "select 1 from dual;"
  4. Click Go
  5. Click Export
  6. Use default options and click "Go"
  7. Receive error "Bad parameters!"

Expected behavior

Expect to generate a file containing query results

Screenshots

image

Server configuration

  • Operating system: Ubuntu 14.04
  • Web server: Apache 2.4
  • Database version: 5.7.23-0ubuntu0.16.04.1-log
  • PHP version: 5.5.9-1ubuntu4.25
  • phpMyAdmin version: 4.8.3

Client configuration

  • Browser: Chrome
  • Operating system: Windows 10

Additional context

The "bad parameters" message is generated on line 292 in export.php:

phpmyadmin/export.php

Lines 274 to 293 in 2fc8bb1

// Generate error url and check for needed variables
if ($export_type == 'server') {
$err_url = 'server_export.php' . Url::getCommon();
} elseif ($export_type == 'database' && strlen($db) > 0) {
$err_url = 'db_export.php' . Url::getCommon(['db' => $db]);
// Check if we have something to export
if (isset($table_select)) {
$tables = $table_select;
} else {
$tables = [];
}
} elseif ($export_type == 'table' && strlen($db) > 0 && strlen($table) > 0) {
$err_url = 'tbl_export.php' . Url::getCommon(
[
'db' => $db, 'table' => $table
]
);
} else {
Core::fatalError(__('Bad parameters!'));
}

The problem appears to be the fact that since we ran the query without selecting a database yet, the $db parameter is not present in the POST request, triggering the fatal error.

@jormaster3k jormaster3k changed the title Exporting a SQL query does not work when a database is not selected first Exporting a SQL query causes "bad parameters" error when a database is not selected first Dec 7, 2018
@jormaster3k jormaster3k changed the title Exporting a SQL query causes "bad parameters" error when a database is not selected first Exporting results of SQL query causes "bad parameters" error when a database is not selected first Dec 7, 2018
@Gaurav-Punjabi
Copy link
Contributor

So what should we do to fix this issue?
Should we display a warning to the user that no DB is selected?

@williamdes
Copy link
Member

williamdes commented Dec 9, 2018

Successfully reproduced on demo servers (root, no password)

@Gaurav-Punjabi The data should export as expected ;)
No need to select a database (IMO)

@williamdes williamdes added Bug A problem or regression with an existing feature good first issue labels Dec 9, 2018
@Thedijje
Copy link

So what should we do to fix this issue?
Should we display a warning to the user that no DB is selected?

@Gaurav-Punjabi We should debug if the database is selected by the export query, as per my experience, I tried selecting database many times but on the error, I kept finding out that database gets unselect.

@computerManiac
Copy link

This piece of code makes sure that a table is always selected when we export,

if (empty($_url_params['table']) && ! empty($_url_params['db'])) {
$_url_params['table'] = $GLOBALS['dbi']->fetchValue("SHOW TABLES");
/* No result (probably no database selected) */
if ($_url_params['table'] === false) {
unset($_url_params['table']);
}
}

But since we don't have any database selected it fails. Should we explicitly set the database and table to a string 'none' so that export doesn't fail ?

@nmilo
Copy link
Contributor

nmilo commented Jan 19, 2019

Couldn't reproduce this on latest version, should this be closed?

@bahl24
Copy link
Contributor

bahl24 commented Jan 21, 2019

@williamdes I agree with @nmilo

@svaningelgem
Copy link

I could reproduce this on the current 4.8 version:

Reproduction steps:

  1. go to the url & log on
  2. click on the SQL tab on top (don't select anything else)
  3. enter the query: "select * from acid.authors;" (hopefully it still exists when you check it ;-))
  4. run it
  5. it will show the table with the entries
  6. click on the "export" tab on top.
  7. here it says "exporting from table authors" >> So it does actually know what database and everything the query is supposed to be run in.
  8. doesn't matter which type of export, so I just clicked the button.
  9. "Bad parameters"

@williamdes williamdes added this to To be sorted in issues May 2, 2019
@williamdes williamdes moved this from To be sorted to Reproduced in issues May 4, 2019
@williamdes williamdes self-assigned this Nov 30, 2019
@tlshifat
Copy link

tlshifat commented Dec 3, 2019

Successfully reproduced on demo servers (root, no password)

* [`5.0.0-dev`](https://demo.phpmyadmin.net/master-config/)

* [`4.8.x`](https://demo.phpmyadmin.net/QA_4_8/)

@Gaurav-Punjabi The data should export as expected ;)
No need to select a database (IMO)

if database is not selected then we have to take database name from sql_query . right?

@tlshifat
Copy link

tlshifat commented Dec 3, 2019

@williamdes if database is not selected then we have to take database name from sql_query . right?

@williamdes
Copy link
Member

@shifatbuet Would make sense, but what if the query uses a join ?
or uses multiple tables in the FROM clause ?

@tlshifat
Copy link

tlshifat commented Dec 4, 2019

@williamdes I was thinking about this :

  • explode sql query(select * from a join a b c d e f) to array using space.
  • find the position of 'FROM' or 'from' ex: 3
  • get number 4 postions value.
  • if contains dot (.) explode it again otherwise it is the db.
  • if doesn't contain dot (.) finally exploded array[0] is db.

@williamdes
Copy link
Member

@shifatbuet You should not do that, we have https://github.com/phpmyadmin/sql-parser to do the work :)

@tlshifat
Copy link

tlshifat commented Dec 4, 2019

@williamdes okay. may be i have to do it in URL . like CommonParams.set('db', data.params.db);.

I am really confused , how to do it from php code and also refresh so that url shows db= something. Can you help me a little bit?

@williamdes
Copy link
Member

@shifatbuet I think we need a different approach: disable the db verification
Is it possible: maybe it is so hard to do it in the code base of export page that we can not do it.

beacause the query of this issue is select 1 from dual; obviously has no real database

@williamdes williamdes removed their assignment Dec 24, 2019
@williamdes williamdes self-assigned this Apr 23, 2021
@williamdes williamdes added this to the 5.1.1 milestone Apr 23, 2021
williamdes added a commit that referenced this issue Apr 23, 2021
Signed-off-by: William Desportes <williamdes@wdes.fr>
issues automation moved this from Reproduced to Closed Apr 23, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A problem or regression with an existing feature
Projects
issues
  
Closed
Development

No branches or pull requests

9 participants