Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dimitri's avatar

Running raw db query with mysql's own prepare and execute statements

Hi all,

can anyone point me to right direction. I have some raw mysql query that counts table rows from all databases (I have also other queries in similar syntax that fetches colum values). I have problem with the last portion of query (prepare and execute), if I use raw and statement it return error.

Here is raw query in question:

Select
  concat(
    'select * from (',
    group_concat(
      concat('select ',
        quote(db), ' db, ',
        quote(tablename), ' tablename, '
        'count(1) "rowcount" ',
        'from ', db, '.', tablename)
      separator ' union ')
    , ') t order by 3 desc')
into @sql
from (
  select
    table_schema db,
    table_name tablename
  from information_schema.tables
  where table_schema not in
    ('performance_schema', 'mysql', 'information_schema')
) t;

prepare s from @sql; execute s; deallocate prepare s;
0 likes
0 replies

Please or to participate in this conversation.