-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Export as CSV - No database selected #16988
Comments
Hi @nunoperalta |
Hey. This is a production database. I edited the screenshot above to remove private information. |
I read somewhere that you need to make sure there is a name for your database, or else it will be confused. Now, seeing as you stated that you have contents in the database already (not showing it due to privacy), that seems to be in order. |
phpMyAdmin should not prevent you from using database names with un-usual characters, if you have examples let us know ;)
I would opt for this one first, we have some logic that sometimes transforms queries If anyone can confirm reproducting on demo servers ( |
There was one instance of this where I had the name |
This is indeed very interesting, |
I said above I did try putting that at the beginning of the query, but that line seems to be removed on the Export.
My database name is "db_something". Only "_" is special character.
Ok, so the SQL query is like this: select * from (
SELECT t.tid,
case when main.level = 3 then main.parent_fid else main.fid end as new_fid_level2,
case when main.level = 3 then parent.parent_fid else main.parent_fid end as new_fid_level1
FROM `some_table` t
straight_JOIN some_othertable main using (fid)
LEFT JOIN some_othertable parent on main.parent_fid = parent.fid
) t
where new_fid_level2 is not null; (only changed the table names - everything else is untouched) |
can confirm we're experiencing the same type of issue (no database selected error) when exporting complex query results. |
here is an reproducable example (on the 5.1.1 demo PMA works the same way): structure and data: create database test_16988;
use test_16988;
CREATE TABLE t1 (
id INT PRIMARY KEY
);
CREATE TABLE t2 (
id INT PRIMARY KEY
);
INSERT INTO t1 VALUES (1),(2),(3);
INSERT INTO t2 VALUES (2),(3),(4); Query to try and export the results of: SELECT * FROM (SELECT id FROM t1 UNION ALL SELECT id FROM t2 ) as results; Expected result: a csv file, actual result: <div class="alert alert-danger" role="alert"><h1>Error</h1><p><strong>SQL query:</strong> <a href="#" class="copyQueryBtn" data-text="SELECT * FROM
(SELECT id
FROM t1
UNION ALL
SELECT id
FROM t2
) as results">Copy</a>
<a href="./url.php?url=https%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F8.0%2Fen%2Fselect.html" target="mysql_doc"><img src="themes/dot.gif" title="Documentation" alt="Documentation" class="icon ic_b_help"></a><a href="index.php?route=/table/sql&sql_query=SELECT+%2A+FROM%0D%0A++++++++%28SELECT+id%0D%0AFROM+t1%0D%0AUNION+ALL%0D%0ASELECT+id%0D%0AFROM+t2%0D%0A%29+as+results&show_query=1&db=test_16988&table=t1"><span class="nowrap"><img src="themes/dot.gif" title="Edit" alt="Edit" class="icon ic_b_edit"> Edit</span></a> </p>
<p>
<code class="sql"><pre>
SELECT * FROM
(SELECT id
FROM t1
UNION ALL
SELECT id
FROM t2
) as results
</pre></code>
</p>
<p>
<strong>MySQL said: </strong><a href="./url.php?url=https%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F8.0%2Fen%2Fserver-error-reference.html" target="mysql_doc"><img src="themes/dot.gif" title="Documentation" alt="Documentation" class="icon ic_b_help"></a>
</p>
<code>#1046 - No database selected</code><br></div> |
Hi @iifawzi |
Hi, I've investigated more on this, the issues is caused because for complex queries our analyzer doesn't know exactly what is the table name, which's leading to mark the complex queries as phpmyadmin/libraries/classes/Display/Results.php Lines 4710 to 4732 in 63faf7e
$analyzed_sql_results['select_tables'] will always be an empty array in case of complex queries - mentioned also above -which's lead the query to be exported using exportRawQuery that doesn't know anything about the database.phpmyadmin/libraries/classes/Plugins/Export/ExportCsv.php Lines 366 to 369 in e355487
The issue affects all of the plugins not only csv. Those are my findings, what do you think @williamdes This could solve the issue: |
Hey, dont know if relevant, but on previous versions that same query used to work fine for exporting. |
Interesting, definitely could help! either I'm missing something, or there's a bad commit that messed this up. |
The version that we previously used is 4.9.0.1, in that version the same query worked fine, when doing exports. Let me know if there is any other information i can provide to make identifying and resolving the issue easier. |
@iifawzi @williamdes any news on this issue? |
Hi @duronrulez If anyone can workout something that will be cool, else I will take care of this when I can spend more hours on the project |
Impacted too, I think this issue is pretty urgent to be honest. All exports with subqueries are broken. |
Hi @iifawzi |
Working on it |
I can still confirm the findings above, the issue is that we don't know the table name, and this is also mentioned here. phpmyadmin/libraries/classes/Display/Results.php Lines 4710 to 4732 in 63faf7e
so, what's happening is that, we're marking almost every complex query (where we don't know exactly the name of the table) as a it was all introduced at #15121. Possible solutions:
I've tested it, all tests are passing, and I've also tested it manually and it worked as expected, the only difference is that for the real raw queries, such as and imo, it's not an issue, it wouldn't affect the functionality, and everything will be exported correctly.
I've investigated more into the docs/code, and found that we can use the for the real raw queries, the table name output of this method, is empty, and for any complex query it seems to be able to detect a table name, so we can do something like this: $queryResult = $this->dbi->tryQuery($urlParams['sql_query']);
$queryFieldsMeta = $this->dbi->getFieldsMeta($queryResult)[0];
if (! $analyzedSqlResults['select_tables'] && $queryFieldsMeta->table == '') {
$urlParams['raw_query'] = 'true';
} Although i'm not really sure of this approach, but it worked like a charm! the manual tests got exported and worked as expected. the only issue of this approach is that when I attempted to run the unit tests, some of them failed with a strange error to me. cc: @williamdes |
Hi there, I've observed a similar issue, I'm not sure it is related though. If you use a table called "user" (I know that's not the best idea, but the product in question just names the table like this) and run a query involving this table without quoting the name like this:
It works nicely within PMA, but when using the export function, it runs into the same error as described above. Essentially, it complains about no database selected. Note that the following query works:
|
For the people in need of a temporary solution: What worked for me is to create a view of the query and export it from there. |
thanks! solved for me! |
Jumping back almost 10 months later, still no fix and the milestone keeps being pushed away, while this looks to me as a VERY important bug. Exporting queries is a basic feature of PMA, and the fix proposed by @iifawzi sounds rather simple, could we have an update on what is blocking this so maybe we could help and contribute ? |
Hi |
Using #16988 (comment) When I export as text (to browser, not as a file) it outputs this, it's another bug for this topic: -- phpMyAdmin SQL Dump
-- version 5.2.1-dev
-- https://www.phpmyadmin.net/
--
-- Hôte : mariadb108.williamdes.local
-- Généré le : mar. 13 déc. 2022 à 21:13
-- Version du serveur : 10.8.6-MariaDB-1:10.8.6+maria~ubu2204
-- Version de PHP : 8.1.13
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";
SELECT * FROM (SELECT id FROM t1 UNION ALL SELECT id FROM t2 ) as results COMMIT; Export as JSON gives some broken JSON containing the "no database selected error" |
Hi everybody, I made a fix for the queries marked as "raw". There should not be any database not selected issue anymore. The (phpMyAdmin 5.2+snapshot) also available as a non official docker image is updated with the fix, please try it and give feedback here 🙏🏻 |
Thank you very much @williamdes !! |
Hi @waza-ari /cc @iifawzi I think it may be fixed with your PR about using keywords as table names on the parser |
I've tested it already without that PR, and the Parser is not complaining, but I will check that |
Describe the bug
I'm getting the problem described in #16668 right now...
I'm definitely with a database (and table) selected, I ran a SELECT query there, and I'm using the "Export" function below the results. Then I select "CSV" as the option, and the "export.csv" file has an HTML output with the query SQL there, and an error at the end saying:
To Reproduce
Steps to reproduce the behavior:
(I edited the contents of the image)
Server configuration
Client configuration
I also tried to put "USE mydb;" at the beginning of the query, but that line seems to be removed on the Export.
The text was updated successfully, but these errors were encountered: