2

I've got a stored procedure in MySQL.

I want to export the result to CSV (or whatever), but for some unknown reason, the export button won't show.

I can export the procedure, but not the result generated by the procedure.

From the Routine tab: routinetab

I tried from the SQL tab and calling from there my procedure but still no export button: sqltab

When I do it with any other query, the button shows up.

So I tried a workaround to trick PhpMyAdmin and manipulate the URL for a view creation from the query result into an export one, by changing it with "tbl_export.php" like it would normally redirect if the button was there.

The export page loads, but when I try to export it, I got the Bad parameter error: bad_param.

Somehow I've archived it with the 4.7 version with this workaround and I could download a JSON file but only once, I can't reproduce it anymore, not even running the the last version 5.1

I really don't know why this export button is not available for procedure, it's just a query result like any other.

Thanks in advance,

EDIT

If possible without using a OUTPUT FILE.

Tangogow
  • 23
  • 4

1 Answers1

1

This is not a proper solution, but rather a workaround:

You can copy the outer HTML of any export button from a regular query result using the element inspector from your browser and paste it into the result of a CALL command to export the results. Some modifications are necessary.

You should copy the anchor tag with href="tbl_export.php" from a regular query export button. Notice that the <a> tag has a data-post attribute, which contains url-encoded form data. Among all the fields of the data-post string, we are interested in the sql_query and table fields. Namely, we want to replace the sql_query and assert that the table field is not blank.

When executing a CALL command in PhpMyAdmin you shoud open the element inspector at the fieldset at the bottom of the page, paste the copied outer HTML as the last child of the fieldset and edit the sql_query value to match the value of the sql_query in the neighbouring Create View button.

The following Greasemonkey script does all of the above automatically and creates a "Forced Export" button when possible:

// ==UserScript==
// @name      Force export button in phpMyAdmin
// @namespace https://<your phpmyadmin host>/*
// @include   https://<your phpmyadmin host>/*
// @version   1
// @grant     none
// ==/UserScript==

setInterval(function() {
  // See https://stackoverflow.com/a/35385518/5254685
  /**
   * @param {String} HTML representing a single element
   * @return {Element}
   */
  function htmlToElement(html) {
      var template = document.createElement('template');
      html = html.trim(); // Never return a text node of whitespace as the result
      template.innerHTML = html;
      return template.content.firstChild;
  }

  let create_view_a = document.querySelector('span>a.create_view');
 
  if (create_view_a == null) {
    return;
  }
  
  if (document.querySelector('a[href="tbl_export.php"]') != null) {
    return;
  }

  let data_post = create_view_a.getAttribute('data-post');

  let fieldset = create_view_a.parentElement.parentElement;
  
  let form_data = Object.fromEntries(
    data_post.split('&').map(v => v.split('=', 2))
    );


  if (form_data['table']) {
    let forced_export_btn = htmlToElement(`
      <a href="tbl_export.php" data-post="${data_post}">
        <span class="nowrap"><img src="themes/dot.gif" title="Export" alt="Export" class="icon ic_b_tblexport">&nbsp;Forced Export</span>
      </a>
    `);
    fieldset.appendChild(forced_export_btn);
  }
}, 2500);
FranciscoDA
  • 21
  • 1
  • 2