Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sql.lib.php « libraries - github.com/phpmyadmin/phpmyadmin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e56211a78572809a7666f7f11c1dbe801552650d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
 * set of functions for the sql executor
 *
 * @package PhpMyAdmin
 */
if (!defined('PHPMYADMIN')) {
    exit;
}

/**
 * Get the database name inside a query
 *
 * @param string $sql       SQL query
 * @param array  $databases array with all databases
 *
 * @return string $db new database name
 */
function PMA_getNewDatabase($sql, $databases)
{
    $db = '';
    // loop through all the databases
    foreach ($databases as $database) {
        if ($GLOBALS['PMA_String']->strpos($sql, $database['SCHEMA_NAME']) !== false
        ) {
            $db = $database['SCHEMA_NAME'];
            break;
        }
    }
    return $db;
}

/**
 * Get the table name in a sql query
 * If there are several tables in the SQL query,
 * first table will return
 *
 * @param string $sql    SQL query
 * @param array  $tables array of names in current database
 *
 * @return string $table table name
 */
function PMA_getTableNameBySQL($sql, $tables)
{
    $table = '';

    // loop through all the tables in the database
    foreach ($tables as $tbl) {
        if ($GLOBALS['PMA_String']->strpos($sql, $tbl)) {
            $table .= ' ' . $tbl;
        }
    }

    if (count(explode(' ', trim($table))) > 1) {
        $tmp_array = explode(' ', trim($table));
        return $tmp_array[0];
    }

    return trim($table);
}


/**
 * Generate table html when SQL statement have multiple queries
 * which return displayable results
 *
 * @param object $displayResultsObject PMA_DisplayResults object
 * @param string $db                   database name
 * @param array  $sql_data             information about SQL statement
 * @param string $goto                 URL to go back in case of errors
 * @param string $pmaThemeImage        path for theme images  directory
 * @param string $printview            whether printview is enabled
 * @param string $url_query            URL query
 * @param array  $disp_mode            the display mode
 * @param string $sql_limit_to_append  limit clause
 * @param bool   $editable             whether editable or not
 *
 * @return string   $table_html   html content
 */
function PMA_getTableHtmlForMultipleQueries(
    $displayResultsObject, $db, $sql_data, $goto, $pmaThemeImage,
    $printview, $url_query, $disp_mode, $sql_limit_to_append,
    $editable
) {
    $table_html = '';

    $tables_array = $GLOBALS['dbi']->getTables($db);
    $databases_array = $GLOBALS['dbi']->getDatabasesFull();
    $multi_sql = implode(";", $sql_data['valid_sql']);
    $querytime_before = array_sum(explode(' ', microtime()));

    // Assignment for variable is not needed since the results are
    // looping using the connection
    @$GLOBALS['dbi']->tryMultiQuery($multi_sql);

    $querytime_after = array_sum(explode(' ', microtime()));
    $querytime = $querytime_after - $querytime_before;
    $sql_no = 0;

    do {
        $analyzed_sql = array();
        $is_affected = false;
        $showtable = array();

        $result = $GLOBALS['dbi']->storeResult();
        $fields_meta = ($result !== false)
            ? $GLOBALS['dbi']->getFieldsMeta($result)
            : array();
        $fields_cnt  = count($fields_meta);

        // Initialize needed params related to each query in multiquery statement
        if (isset($sql_data['valid_sql'][$sql_no])) {
            // 'Use' query can change the database
            if ($GLOBALS['PMA_String']->stripos(
                $sql_data['valid_sql'][$sql_no],
                "use "
            )) {
                $db = PMA_getNewDatabase(
                    $sql_data['valid_sql'][$sql_no],
                    $databases_array
                );
            }

            $table = PMA_getTableNameBySQL(
                $sql_data['valid_sql'][$sql_no],
                $tables_array
            );

            // for the use of the parse_analyze.inc.php
            $sql_query = $sql_data['valid_sql'][$sql_no];

            // Parse and analyze the query
            include 'libraries/parse_analyze.inc.php';

            $unlim_num_rows = PMA_Table::countRecords($db, $table, true);
            $showtable = PMA_Table::sGetStatusInfo($db, $table, null, true);
            $url_query = PMA_URL_getCommon(array('db' => $db, 'table' => $table));

            // Handle remembered sorting order, only for single table query
            if ($GLOBALS['cfg']['RememberSorting']
                && ! ($is_count || $is_export || $is_func || $is_analyse)
                && isset($analyzed_sql[0]['select_expr'])
                && (count($analyzed_sql[0]['select_expr']) == 0)
                && isset($analyzed_sql[0]['queryflags']['select_from'])
                && count($analyzed_sql[0]['table_ref']) == 1
            ) {
                PMA_handleSortOrder(
                    $db,
                    $table,
                    $analyzed_sql,
                    $sql_data['valid_sql'][$sql_no]
                );
            }

            // Do append a "LIMIT" clause?
            if (($_SESSION['tmpval']['max_rows'] != 'all')
                && ! ($is_count || $is_export || $is_func || $is_analyse)
                && isset($analyzed_sql[0]['queryflags']['select_from'])
                && ! isset($analyzed_sql[0]['queryflags']['offset'])
                && empty($analyzed_sql[0]['limit_clause'])
            ) {
                $sql_limit_to_append = ' LIMIT '
                    . $_SESSION['tmpval']['pos']
                    . ', ' . $_SESSION['tmpval']['max_rows'] . " ";
                $sql_data['valid_sql'][$sql_no] = PMA_getSqlWithLimitClause(
                    $analyzed_sql,
                    $sql_limit_to_append
                );
            }

            // Set the needed properties related to executing sql query
            $displayResultsObject->__set('db', $db);
            $displayResultsObject->__set('table', $table);
            $displayResultsObject->__set('goto', $goto);
        }

        if (! $is_affected) {
            $num_rows = ($result) ? @$GLOBALS['dbi']->numRows($result) : 0;
        } elseif (! isset($num_rows)) {
            $num_rows = @$GLOBALS['dbi']->affectedRows();
        }

        if (isset($sql_data['valid_sql'][$sql_no])) {

            $displayResultsObject->__set(
                'sql_query',
                $sql_data['valid_sql'][$sql_no]
            );
            $displayResultsObject->setProperties(
                $unlim_num_rows, $fields_meta, $is_count, $is_export, $is_func,
                $is_analyse, $num_rows, $fields_cnt, $querytime, $pmaThemeImage,
                $GLOBALS['text_dir'], $is_maint, $is_explain, $is_show,
                $showtable, $printview, $url_query, $editable
            );
        }

        if ($num_rows == 0) {
            continue;
        }

        // With multiple results, operations are limited
        $disp_mode = 'nnnn000000';
        $is_limited_display = true;

        // Collect the tables
        $table_html .= $displayResultsObject->getTable(
            $result, $disp_mode, $analyzed_sql, $is_limited_display
        );

        // Free the result to save the memory
        $GLOBALS['dbi']->freeResult($result);

        $sql_no++;

    } while ($GLOBALS['dbi']->moreResults() && $GLOBALS['dbi']->nextResult());

    return $table_html;
}

/**
 * Handle remembered sorting order, only for single table query
 *
 * @param string $db                    database name
 * @param string $table                 table name
 * @param array  &$analyzed_sql_results the analyzed query results
 * @param string &$full_sql_query       SQL query
 *
 * @return void
 */
function PMA_handleSortOrder(
    $db, $table, &$analyzed_sql_results, &$full_sql_query
) {
    $pmatable = new PMA_Table($table, $db);
    if (empty($analyzed_sql_results['analyzed_sql'][0]['order_by_clause'])) {
        $sorted_col = $pmatable->getUiProp(PMA_Table::PROP_SORTED_COLUMN);
        if ($sorted_col) {
            //remove the tablename from retrieved preference
            //to get just the column name and the sort order
            $sorted_col = str_replace(
                PMA_Util::backquote($table) . '.', '', $sorted_col
            );
            // retrieve the remembered sorting order for current table
            $sql_order_to_append = ' ORDER BY ' . $sorted_col . ' ';
            $full_sql_query
                = $analyzed_sql_results['analyzed_sql'][0]['section_before_limit']
                . $sql_order_to_append
                . $analyzed_sql_results['analyzed_sql'][0]['limit_clause']
                . ' '
                . $analyzed_sql_results['analyzed_sql'][0]['section_after_limit'];

            // update the $analyzed_sql
            $analyzed_sql_results['analyzed_sql'][0]['section_before_limit']
                .= $sql_order_to_append;
            $analyzed_sql_results['analyzed_sql'][0]['order_by_clause']
                = $sorted_col;
        }
    } else {
        // store the remembered table into session
        $pmatable->setUiProp(
            PMA_Table::PROP_SORTED_COLUMN,
            $analyzed_sql_results['analyzed_sql'][0]['order_by_clause']
        );
    }
}

/**
 * Append limit clause to SQL query
 *
 * @param array  $analyzed_sql        the analyzed query
 * @param string $sql_limit_to_append clause to append
 *
 * @return string limit clause appended SQL query
 */
function PMA_getSqlWithLimitClause($analyzed_sql,
    $sql_limit_to_append
) {
    return $analyzed_sql[0]['section_before_limit'] . "\n"
        . $sql_limit_to_append . $analyzed_sql[0]['section_after_limit'];
}


/**
 * Get column name from a drop SQL statement
 *
 * @param string $sql SQL query
 *
 * @return string $drop_column Name of the column
 */
function PMA_getColumnNameInColumnDropSql($sql)
{
    $tmpArray1 = explode('DROP', $sql);
    $str_to_check = trim($tmpArray1[1]);

    if ($GLOBALS['PMA_String']->stripos($str_to_check, 'COLUMN') !== false) {
        $tmpArray2 = explode('COLUMN', $str_to_check);
        $str_to_check = trim($tmpArray2[1]);
    }

    $tmpArray3 = explode(' ', $str_to_check);
    $str_to_check = trim($tmpArray3[0]);

    $drop_column = str_replace(';', '', trim($str_to_check));
    $drop_column = str_replace('`', '', $drop_column);

    return $drop_column;
}

/**
 * Verify whether the result set has columns from just one table
 *
 * @param array $fields_meta meta fields
 *
 * @return boolean whether the result set has columns from just one table
 */
function PMA_resultSetHasJustOneTable($fields_meta)
{
    $just_one_table = true;
    $prev_table = $fields_meta[0]->table;
    foreach ($fields_meta as $one_field_meta) {
        if (! empty($one_field_meta->table)
            && $one_field_meta->table != $prev_table
        ) {
            $just_one_table = false;
            break;
        }
    }
    return $just_one_table;
}

/**
 * Verify whether the result set contains all the columns
 * of at least one unique key
 *
 * @param string $db          database name
 * @param string $table       table name
 * @param array  $fields_meta meta fields
 *
 * @return boolean whether the result set contains a unique key
 */
function PMA_resultSetContainsUniqueKey($db, $table, $fields_meta)
{
    $resultSetColumnNames = array();
    foreach ($fields_meta as $oneMeta) {
        $resultSetColumnNames[] = $oneMeta->name;
    }
    foreach (PMA_Index::getFromTable($table, $db) as $index) {
        if ($index->isUnique()) {
            $indexColumns = $index->getColumns();
            $numberFound = 0;
            foreach ($indexColumns as $indexColumnName => $dummy) {
                if (in_array($indexColumnName, $resultSetColumnNames)) {
                    $numberFound++;
                }
            }
            if ($numberFound == count($indexColumns)) {
                return true;
            }
        }
    }
    return false;
}

/**
 * Get the HTML for relational column dropdown
 * During grid edit, if we have a relational field, returns the html for the
 * dropdown
 *
 * @param string $db         current database
 * @param string $table      current table
 * @param string $column     current column
 * @param string $curr_value current selected value
 *
 * @return string $dropdown html for the dropdown
 */
function PMA_getHtmlForRelationalColumnDropdown($db, $table, $column, $curr_value)
{
    $foreigners = PMA_getForeigners($db, $table, $column);

    $foreignData = PMA_getForeignData($foreigners, $column, false, '', '');

    if ($foreignData['disp_row'] == null) {
        //Handle the case when number of values
        //is more than $cfg['ForeignKeyMaxLimit']
        $_url_params = array(
                'db' => $db,
                'table' => $table,
                'field' => $column
        );

        $dropdown = '<span class="curr_value">'
            . htmlspecialchars($_REQUEST['curr_value'])
            . '</span>'
            . '<a href="browse_foreigners.php'
            . PMA_URL_getCommon($_url_params) . '"'
            . 'class="ajax browse_foreign" ' . '>'
            . __('Browse foreign values')
            . '</a>';
    } else {
        $dropdown = PMA_foreignDropdown(
            $foreignData['disp_row'],
            $foreignData['foreign_field'],
            $foreignData['foreign_display'],
            $curr_value,
            $GLOBALS['cfg']['ForeignKeyMaxLimit']
        );
        $dropdown = '<select>' . $dropdown . '</select>';
    }

    return $dropdown;
}

/**
 * Get the HTML for the header of the page in print view if print view is selected.
 * Otherwise returns null.
 *
 * @param string $db        current database
 * @param string $sql_query current sql query
 * @param int    $num_rows  the number of rows in result
 *
 * @return string $header html for the header
 */
function PMA_getHtmlForPrintViewHeader($db, $sql_query, $num_rows)
{
    $response = PMA_Response::getInstance();
    $header = $response->getHeader();
    if (isset($_REQUEST['printview']) && $_REQUEST['printview'] == '1') {
        PMA_Util::checkParameters(array('db', 'sql_query'));
        $header->enablePrintView();
        if ( $GLOBALS['cfg']['Server']['verbose']) {
            $hostname =  $GLOBALS['cfg']['Server']['verbose'];
        } else {
            $hostname =  $GLOBALS['cfg']['Server']['host'];
            if (! empty( $GLOBALS['cfg']['Server']['port'])) {
                $hostname .=  $GLOBALS['cfg']['Server']['port'];
            }
        }

        $versions  = "phpMyAdmin&nbsp;" . PMA_VERSION;
        $versions .= "&nbsp;/&nbsp;";
        $versions .= "MySQL&nbsp;" . PMA_MYSQL_STR_VERSION;

        $print_view_header = '';
        $print_view_header .= "<h1>" . __('SQL result') . "</h1>";
        $print_view_header .= "<p>";
        $print_view_header .= "<strong>" . __('Host:')
            . "</strong> $hostname<br />";
        $print_view_header .= "<strong>" . __('Database:') . "</strong> "
            . htmlspecialchars($db) . "<br />";
        $print_view_header .= "<strong>" . __('Generation Time:') . "</strong> "
            . PMA_Util::localisedDate() . "<br />";
        $print_view_header .= "<strong>" . __('Generated by:')
            . "</strong> $versions<br />";
        $print_view_header .= "<strong>" . __('SQL query:') . "</strong> "
            . htmlspecialchars($sql_query) . ";";
        if (isset($num_rows)) {
            $print_view_header .= "<br />";
            $print_view_header .= "<strong>" . __('Rows:') . "</strong> $num_rows";
        }
        $print_view_header .= "</p>";
    } else {
        $print_view_header = null;
    }

    return $print_view_header;
}

/**
 * Get the HTML for the profiling table and accompanying chart if profiling is set.
 * Otherwise returns null
 *
 * @param string $url_query         url query
 * @param string $db                current database
 * @param array  $profiling_results array containing the profiling info
 *
 * @return string $profiling_table html for the profiling table and chart
 */
function PMA_getHtmlForProfilingChart($url_query, $db, $profiling_results)
{
    if (isset($profiling_results)) {
        $pma_token = $_SESSION[' PMA_token '];
        $url_query = isset($url_query)
            ? $url_query
            : PMA_URL_getCommon(array('db' => $db));

        $profiling_table = '';
        $profiling_table .= '<fieldset><legend>' . __('Profiling')
            . '</legend>' . "\n";
        $profiling_table .= '<div style="float: left;">';
        $profiling_table .= '<h3>' . __('Detailed profile') . '</h3>';
        $profiling_table .= '<table id="profiletable"><thead>' . "\n";
        $profiling_table .= ' <tr>' . "\n";
        $profiling_table .= '  <th>' . __('Order')
            . '<div class="sorticon"></div></th>' . "\n";
        $profiling_table .= '  <th>' . __('State')
            . PMA_Util::showMySQLDocu('general-thread-states')
            . '<div class="sorticon"></div></th>' . "\n";
        $profiling_table .= '  <th>' . __('Time')
            . '<div class="sorticon"></div></th>' . "\n";
        $profiling_table .= ' </tr></thead><tbody>' . "\n";
        list($detailed_table, $chart_json, $profiling_stats)
            = PMA_analyzeAndGetTableHtmlForProfilingResults($profiling_results);
        $profiling_table .= $detailed_table;
        $profiling_table .= '</tbody></table>' . "\n";
        $profiling_table .= '</div>';

        $profiling_table .= '<div style="float: left; margin-left:10px;">';
        $profiling_table .= '<h3>' . __('Summary by state') . '</h3>';
        $profiling_table .= '<table id="profilesummarytable"><thead>' . "\n";
        $profiling_table .= ' <tr>' . "\n";
        $profiling_table .= '  <th>' . __('State')
            . PMA_Util::showMySQLDocu('general-thread-states')
            . '<div class="sorticon"></div></th>' . "\n";
        $profiling_table .= '  <th>' . __('Total Time')
            . '<div class="sorticon"></div></th>' . "\n";
        $profiling_table .= '  <th>' . __('% Time')
            . '<div class="sorticon"></div></th>' . "\n";
        $profiling_table .= '  <th>' . __('Calls')
            . '<div class="sorticon"></div></th>' . "\n";
        $profiling_table .= '  <th>' . __('ø Time')
            . '<div class="sorticon"></div></th>' . "\n";
        $profiling_table .= ' </tr></thead><tbody>' . "\n";
        $profiling_table .= PMA_getTableHtmlForProfilingSummaryByState(
            $profiling_stats
        );
        $profiling_table .= '</tbody></table>' . "\n";

        $profiling_table .= <<<EOT
<script type="text/javascript">
    pma_token = '$pma_token';
    url_query = '$url_query';
</script>
EOT;
        $profiling_table .= "</div>";
        $profiling_table .= "<div class='clearfloat'></div>";

        //require_once 'libraries/chart.lib.php';
        $profiling_table .= '<div id="profilingChartData" style="display:none;">';
        $profiling_table .= json_encode($chart_json);
        $profiling_table .= '</div>';
        $profiling_table .= '<div id="profilingchart" style="display:none;">';
        $profiling_table .= '</div>';
        $profiling_table .= '<script type="text/javascript">';
        $profiling_table .= "AJAX.registerOnload('sql.js', function () {";
        $profiling_table .= 'makeProfilingChart();';
        $profiling_table .= 'initProfilingTables();';
        $profiling_table .= '});';
        $profiling_table .= '</script>';
        $profiling_table .= '</fieldset>' . "\n";
    } else {
        $profiling_table = null;
    }
    return $profiling_table;
}

/**
 * Function to get HTML for detailed profiling results table, profiling stats, and
 * $chart_json for displaying the chart.
 *
 * @param array $profiling_results profiling results
 *
 * @return mixed
 */
function PMA_analyzeAndGetTableHtmlForProfilingResults(
    $profiling_results
) {
    $profiling_stats = array(
        'total_time' => 0,
        'states' => array(),
    );
    $chart_json = Array();
    $i = 1;
    $table = '';
    foreach ($profiling_results as $one_result) {
        if (isset($profiling_stats['states'][ucwords($one_result['Status'])])) {
            $states = $profiling_stats['states'];
            $states[ucwords($one_result['Status'])]['time']
                += $one_result['Duration'];
            $states[ucwords($one_result['Status'])]['calls']++;
        } else {
            $profiling_stats['states'][ucwords($one_result['Status'])] = array(
                'total_time' => $one_result['Duration'],
                'calls' => 1,
            );
        }
        $profiling_stats['total_time'] += $one_result['Duration'];

        $table .= ' <tr>' . "\n";
        $table .= '<td>' . $i++ . '</td>' . "\n";
        $table .= '<td>' . ucwords($one_result['Status'])
            . '</td>' . "\n";
        $table .= '<td class="right">'
            . (PMA_Util::formatNumber($one_result['Duration'], 3, 1))
            . 's<span style="display:none;" class="rawvalue">'
            . $one_result['Duration'] . '</span></td>' . "\n";
        if (isset($chart_json[ucwords($one_result['Status'])])) {
            $chart_json[ucwords($one_result['Status'])]
                += $one_result['Duration'];
        } else {
            $chart_json[ucwords($one_result['Status'])]
                = $one_result['Duration'];
        }
    }
    return array($table, $chart_json, $profiling_stats);
}

/**
 * Function to get HTML for summary by state table
 *
 * @param array $profiling_stats profiling stats
 *
 * @return string $table html for the table
 */
function PMA_getTableHtmlForProfilingSummaryByState($profiling_stats)
{
    $table = '';
    foreach ($profiling_stats['states'] as $name => $stats) {
        $table .= ' <tr>' . "\n";
        $table .= '<td>' . $name . '</td>' . "\n";
        $table .= '<td align="right">'
            . PMA_Util::formatNumber($stats['total_time'], 3, 1)
            . 's<span style="display:none;" class="rawvalue">'
            . $stats['total_time'] . '</span></td>' . "\n";
        $table .= '<td align="right">'
            . PMA_Util::formatNumber(
                100 * ($stats['total_time'] / $profiling_stats['total_time']),
                0, 2
            )
        . '%</td>' . "\n";
        $table .= '<td align="right">' . $stats['calls'] . '</td>'
            . "\n";
        $table .= '<td align="right">'
            . PMA_Util::formatNumber(
                $stats['total_time'] / $stats['calls'], 3, 1
            )
            . 's<span style="display:none;" class="rawvalue">'
            . number_format($stats['total_time'] / $stats['calls'], 8, '.', '')
            . '</span></td>' . "\n";
        $table .= ' </tr>' . "\n";
    }
    return $table;
}

/**
 * Get the HTML for the enum column dropdown
 * During grid edit, if we have a enum field, returns the html for the
 * dropdown
 *
 * @param string $db         current database
 * @param string $table      current table
 * @param string $column     current column
 * @param string $curr_value currently selected value
 *
 * @return string $dropdown html for the dropdown
 */
function PMA_getHtmlForEnumColumnDropdown($db, $table, $column, $curr_value)
{
    $values = PMA_getValuesForColumn($db, $table, $column);
    $dropdown = '<option value="">&nbsp;</option>';
    $dropdown .= PMA_getHtmlForOptionsList($values, array($curr_value));
    $dropdown = '<select>' . $dropdown . '</select>';
    return $dropdown;
}

/**
 * Get the HTML for the set column dropdown
 * During grid edit, if we have a set field, returns the html for the
 * dropdown
 *
 * @param string $db         current database
 * @param string $table      current table
 * @param string $column     current column
 * @param string $curr_value currently selected value
 *
 * @return string $dropdown html for the set column
 */
function PMA_getHtmlForSetColumn($db, $table, $column, $curr_value)
{
    $values = PMA_getValuesForColumn($db, $table, $column);
    $dropdown = '';

    //converts characters of $curr_value to HTML entities
    $converted_curr_value = htmlentities(
        $curr_value, ENT_COMPAT, "UTF-8"
    );

    $selected_values = explode(',', $converted_curr_value);
    $dropdown .= PMA_getHtmlForOptionsList($values, $selected_values);

    $select_size = (sizeof($values) > 10) ? 10 : sizeof($values);
    $dropdown = '<select multiple="multiple" size="' . $select_size . '">'
        . $dropdown . '</select>';

    return $dropdown;
}

/**
 * Get all the values for a enum column or set column in a table
 *
 * @param string $db     current database
 * @param string $table  current table
 * @param string $column current column
 *
 * @return array $values array containing the value list for the column
 */
function PMA_getValuesForColumn($db, $table, $column)
{
    $field_info_query = $GLOBALS['dbi']->getColumnsSql($db, $table, $column);

    $field_info_result = $GLOBALS['dbi']->fetchResult(
        $field_info_query, null, null, null, PMA_DatabaseInterface::QUERY_STORE
    );

    $values = PMA_Util::parseEnumSetValues($field_info_result[0]['Type']);

    return $values;
}

/**
 * Get HTML for options list
 *
 * @param array $values          set of values
 * @param array $selected_values currently selected values
 *
 * @return string $options HTML for options list
 */
function PMA_getHtmlForOptionsList($values, $selected_values)
{
    $options = '';
    foreach ($values as $value) {
        $options .= '<option value="' . $value . '"';
        if (in_array($value, $selected_values, true)) {
            $options .= ' selected="selected" ';
        }
        $options .= '>' . $value . '</option>';
    }
    return $options;
}

/**
 * Function to get html for bookmark support if bookmarks are enabled. Else will
 * return null
 *
 * @param string $disp_mode      display mode
 * @param bool   $cfgBookmark    configuration setting for bookmarking
 * @param string $sql_query      sql query
 * @param string $db             current database
 * @param string $table          current table
 * @param string $complete_query complete query
 * @param string $bkm_user       bookmarking user
 *
 * @return string $html
 */
function PMA_getHtmlForBookmark($disp_mode, $cfgBookmark, $sql_query, $db, $table,
    $complete_query, $bkm_user
) {
    if ($disp_mode[7] == '1'
        && (! empty($cfgBookmark) && empty($_GET['id_bookmark']))
        && ! empty($sql_query)
    ) {
        $goto = 'sql.php'
            . PMA_URL_getCommon(
                array(
                    'db' => $db,
                    'table' => $table,
                    'sql_query' => $sql_query,
                    'id_bookmark'=> 1,
                )
            );
        $bkm_sql_query = urlencode(
            isset($complete_query) ? $complete_query : $sql_query
        );
        $html = '<form action="sql.php" method="post"'
            . ' onsubmit="return ! emptyFormElements(this,'
            . '\'bkm_fields[bkm_label]\');"'
            . ' id="bookmarkQueryForm">';
        $html .= PMA_URL_getHiddenInputs();
        $html .= '<input type="hidden" name="db"'
            . ' value="' . htmlspecialchars($db) . '" />';
        $html .= '<input type="hidden" name="goto" value="' . $goto . '" />';
        $html .= '<input type="hidden" name="bkm_fields[bkm_database]"'
            . ' value="' . htmlspecialchars($db) . '" />';
        $html .= '<input type="hidden" name="bkm_fields[bkm_user]"'
            . ' value="' . $bkm_user . '" />';
        $html .= '<input type="hidden" name="bkm_fields[bkm_sql_query]"'
            . ' value="'
            . $bkm_sql_query
            . '" />';
        $html .= '<fieldset>';
        $html .= '<legend>';
        $html .= PMA_Util::getIcon(
            'b_bookmark.png', __('Bookmark this SQL query'), true
        );
        $html .= '</legend>';
        $html .= '<div class="formelement">';
        $html .= '<label for="fields_label_">' . __('Label:') . '</label>';
        $html .= '<input type="text" id="fields_label_"'
            . ' name="bkm_fields[bkm_label]" value="" />';
        $html .= '</div>';
        $html .= '<div class="formelement">';
        $html .= '<input type="checkbox" name="bkm_all_users"'
            . ' id="bkm_all_users" value="true" />';
        $html .= '<label for="bkm_all_users">'
            . __('Let every user access this bookmark')
            . '</label>';
        $html .= '</div>';
        $html .= '<div class="clearfloat"></div>';
        $html .= '</fieldset>';
        $html .= '<fieldset class="tblFooters">';
        $html .= '<input type="hidden" name="store_bkm" value="1" />';
        $html .= '<input type="submit"'
            . ' value="' . __('Bookmark this SQL query') . '" />';
        $html .= '</fieldset>';
        $html .= '</form>';

    } else {
        $html = null;
    }

    return $html;
}

/**
 * Function to check whether to remember the sorting order or not
 *
 * @param array $analyzed_sql_results the analyzed query and other variables set
 *                                    after analyzing the query
 *
 * @return boolean
 */
function PMA_isRememberSortingOrder($analyzed_sql_results)
{
    $select_from = isset(
        $analyzed_sql_results['analyzed_sql'][0]['queryflags']['select_from']
    );
    if ($GLOBALS['cfg']['RememberSorting']
        && ! ($analyzed_sql_results['is_count']
        || $analyzed_sql_results['is_export']
        || $analyzed_sql_results['is_func']
        || $analyzed_sql_results['is_analyse'])
        && isset($analyzed_sql_results['analyzed_sql'][0]['select_expr'])
        && (count($analyzed_sql_results['analyzed_sql'][0]['select_expr']) == 0)
        && $select_from
        && count($analyzed_sql_results['analyzed_sql'][0]['table_ref']) == 1
    ) {
        return true;
    } else {
        return false;
    }
}

/**
 * Function to check whether the LIMIT clause should be appended or not
 *
 * @param array $analyzed_sql_results the analyzed query and other variables set
 *                                    after analyzing the query
 *
 * @return boolean
 */
function PMA_isAppendLimitClause($analyzed_sql_results)
{
    $select_from = isset(
        $analyzed_sql_results['analyzed_sql'][0]['queryflags']['select_from']
    );
    if (($_SESSION['tmpval']['max_rows'] != 'all')
        && ! ($analyzed_sql_results['is_export']
        || $analyzed_sql_results['is_analyse'])
        && ($select_from || $analyzed_sql_results['is_subquery'])
        && ! isset($analyzed_sql_results['analyzed_sql'][0]['queryflags']['offset'])
        && empty($analyzed_sql_results['analyzed_sql'][0]['limit_clause'])
    ) {
        return true;
    } else {
        return false;
    }
}

/**
 * Function to check whether this query is for just browsing
 *
 * @param array   $analyzed_sql_results the analyzed query and other variables set
 *                                      after analyzing the query
 * @param boolean $find_real_end        whether the real end should be found
 *
 * @return boolean
 */
function PMA_isJustBrowsing($analyzed_sql_results, $find_real_end)
{
    $distinct = isset(
        $analyzed_sql_results['analyzed_sql'][0]['queryflags']['distinct']
    );

    $table_name = isset(
        $analyzed_sql_results['analyzed_sql'][0]['table_ref'][1]['table_name']
    );
    if (! $analyzed_sql_results['is_group']
        && ! isset($analyzed_sql_results['analyzed_sql'][0]['queryflags']['union'])
        && ! $distinct
        && ! $table_name
        && (empty($analyzed_sql_results['analyzed_sql'][0]['where_clause'])
        || $analyzed_sql_results['analyzed_sql'][0]['where_clause'] == '1 ')
        && empty($analyzed_sql_results['analyzed_sql'][0]['group_by_clause'])
        && ! isset($find_real_end)
        && !$analyzed_sql_results['is_subquery']
        && empty($analyzed_sql_results['analyzed_sql'][0]['having_clause'])
    ) {
        return true;
    } else {
        return false;
    }
}

/**
 * Function to check whether the related transformation information should be deleted
 *
 * @param array $analyzed_sql_results the analyzed query and other variables set
 *                                    after analyzing the query
 *
 * @return boolean
 */
function PMA_isDeleteTransformationInfo($analyzed_sql_results)
{
    if (!empty($analyzed_sql_results['analyzed_sql'][0]['querytype'])
        && (($analyzed_sql_results['analyzed_sql'][0]['querytype'] == 'ALTER')
        || ($analyzed_sql_results['analyzed_sql'][0]['querytype'] == 'DROP'))
    ) {
        return true;
    } else {
        return false;
    }
}

/**
 * Function to check whether the user has rights to drop the database
 *
 * @param array   $analyzed_sql_results  the analyzed query and other variables set
 *                                       after analyzing the query
 * @param boolean $allowUserDropDatabase whether the user is allowed to drop db
 * @param boolean $is_superuser          whether this user is a superuser
 *
 * @return boolean
 */
function PMA_hasNoRightsToDropDatabase($analyzed_sql_results,
    $allowUserDropDatabase, $is_superuser
) {
    if (! defined('PMA_CHK_DROP')
        && ! $allowUserDropDatabase
        && isset ($analyzed_sql_results['drop_database'])
        && $analyzed_sql_results['drop_database'] == 1
        && ! $is_superuser
    ) {
        return true;
    } else {
        return false;
    }
}

/**
 * Function to set a column property 
 *
 * @param PMA_Table $pmatable      PMA_Table instance
 * @param string    $request_index col_order|col_visib
 *
 * @return boolean $retval
 */
function PMA_setColumnProperty($pmatable, $request_index)
{
    $property_value = explode(',', $_REQUEST[$request_index]);
    switch($request_index) {
    case 'col_order':
        $property_to_set = PMA_Table::PROP_COLUMN_ORDER;
        break;
    case 'col_visib':
        $property_to_set = PMA_Table::PROP_COLUMN_VISIB;
        break;
    default:
        $property_to_set = '';
    }
    $retval = $pmatable->setUiProp(
        $property_to_set,
        $property_value,
        $_REQUEST['table_create_time']
    );
    if (gettype($retval) != 'boolean') {
        $response = PMA_Response::getInstance();
        $response->isSuccess(false);
        $response->addJSON('message', $retval->getString());
        exit;
    }

    return $retval;
}

/**
 * Function to check the request for setting the column order or visibility
 *
 * @param String $table the current table
 * @param String $db    the current database
 *
 * @return void
 */
function PMA_setColumnOrderOrVisibility($table, $db)
{
    $pmatable = new PMA_Table($table, $db);
    $retval = false;

    // set column order
    if (isset($_REQUEST['col_order'])) {
        $retval = PMA_setColumnProperty($pmatable, 'col_order');
    }

    // set column visibility
    if ($retval === true && isset($_REQUEST['col_visib'])) {
        $retval = PMA_setColumnProperty($pmatable, 'col_visib');
    }

    $response = PMA_Response::getInstance();
    $response->isSuccess($retval == true);
    exit;
}

/**
 * Function to add a bookmark
 *
 * @param String $pmaAbsoluteUri absolute URI
 * @param String $goto           goto page URL
 *
 * @return void
 */
function PMA_addBookmark($pmaAbsoluteUri, $goto)
{
    $result = PMA_Bookmark_save(
        $_POST['bkm_fields'],
        (isset($_POST['bkm_all_users'])
            && $_POST['bkm_all_users'] == 'true' ? true : false
        )
    );
    $response = PMA_Response::getInstance();
    if ($response->isAjax()) {
        if ($result) {
            $msg = PMA_message::success(__('Bookmark %s has been created.'));
            $msg->addParam($_POST['bkm_fields']['bkm_label']);
            $response->addJSON('message', $msg);
        } else {
            $msg = PMA_message::error(__('Bookmark not created!'));
            $response->isSuccess(false);
            $response->addJSON('message', $msg);
        }
        exit;
    } else {
        // go back to sql.php to redisplay query; do not use &amp; in this case:
        /**
         * @todo In which scenario does this happen?
         */
        PMA_sendHeaderLocation(
            $pmaAbsoluteUri . $goto
            . '&label=' . $_POST['bkm_fields']['bkm_label']
        );
    }
}

/**
 * Function to find the real end of rows
 *
 * @param String $db    the current database
 * @param String $table the current table
 *
 * @return mixed the number of rows if "retain" param is true, otherwise true
 */
function PMA_findRealEndOfRows($db, $table)
{
    $unlim_num_rows = PMA_Table::countRecords($db, $table, true);
    $_SESSION['tmpval']['pos'] = PMA_getStartPosToDisplayRow($unlim_num_rows);

    return $unlim_num_rows;
}

/**
 * Function to get values for the relational columns
 *
 * @param String $db    the current database
 * @param String $table the current table
 *
 * @return void
 */
function PMA_getRelationalValues($db, $table)
{
    $column = $_REQUEST['column'];
    if ($_SESSION['tmpval']['relational_display'] == 'D'
        && isset($_REQUEST['relation_key_or_display_column'])
        && $_REQUEST['relation_key_or_display_column']
    ) {
        $curr_value = $_REQUEST['relation_key_or_display_column'];
    } else {
        $curr_value = $_REQUEST['curr_value'];
    }
    $dropdown = PMA_getHtmlForRelationalColumnDropdown(
        $db, $table, $column, $curr_value
    );
    $response = PMA_Response::getInstance();
    $response->addJSON('dropdown', $dropdown);
    exit;
}

/**
 * Function to get values for Enum or Set Columns
 *
 * @param String $db         the current database
 * @param String $table      the current table
 * @param String $columnType whether enum or set
 *
 * @return void
 */
function PMA_getEnumOrSetValues($db, $table, $columnType)
{
    $column = $_REQUEST['column'];
    $curr_value = $_REQUEST['curr_value'];
    $response = PMA_Response::getInstance();
    if ($columnType == "enum") {
        $dropdown = PMA_getHtmlForEnumColumnDropdown(
            $db, $table, $column, $curr_value
        );
        $response->addJSON('dropdown', $dropdown);
    } else {
        $select = PMA_getHtmlForSetColumn($db, $table, $column, $curr_value);
        $response->addJSON('select', $select);
    }
    exit;
}

/**
 * Function to append the limit clause
 *
 * @param array $analyzed_sql analyzed sql query
 *
 * @return array
 */
function PMA_appendLimitClause($analyzed_sql)
{
    $sql_limit_to_append = ' LIMIT ' . $_SESSION['tmpval']['pos']
        . ', ' . $_SESSION['tmpval']['max_rows'] . " ";
    $full_sql_query = PMA_getSqlWithLimitClause(
        $analyzed_sql,
        $sql_limit_to_append
    );

    return array($sql_limit_to_append, $full_sql_query);
}

/**
 * Function to get the default sql query for browsing page
 *
 * @param String $db    the current database
 * @param String $table the current table
 *
 * @return String $sql_query the default $sql_query for browse page
 */
function PMA_getDefaultSqlQueryForBrowse($db, $table)
{
    include_once 'libraries/bookmark.lib.php';
    $book_sql_query = PMA_Bookmark_get(
        $db,
        '\'' . PMA_Util::sqlAddSlashes($table) . '\'',
        'label',
        false,
        true
    );

    if (! empty($book_sql_query)) {
        $GLOBALS['using_bookmark_message'] = PMA_message::notice(
            __('Using bookmark "%s" as default browse query.')
        );
        $GLOBALS['using_bookmark_message']->addParam($table);
        $GLOBALS['using_bookmark_message']->addMessage(
            PMA_Util::showDocu('faq', 'faq6-22')
        );
        $sql_query = $book_sql_query;
    } else {

        $defaultOrderByClause = '';

        if (isset($GLOBALS['cfg']['TablePrimaryKeyOrder'])
            && ($GLOBALS['cfg']['TablePrimaryKeyOrder'] !== 'NONE')
        ) {

            $primaryKey     = null;
            $primary        = PMA_Index::getPrimary($table, $db);

            if ($primary !== false) {

                $primarycols    = $primary->getColumns();

                foreach ($primarycols as $col) {
                    $primaryKey = $col->getName();
                    break;
                }

                if ($primaryKey != null) {
                    $defaultOrderByClause = ' ORDER BY '
                                          . PMA_Util::backquote($table) . '.'
                                          . PMA_Util::backquote($primaryKey) . ' '
                                          . $GLOBALS['cfg']['TablePrimaryKeyOrder'];
                }

            }

        }

        $sql_query = 'SELECT * FROM ' . PMA_Util::backquote($table)
            . $defaultOrderByClause;

    }
    unset($book_sql_query);

    return $sql_query;
}

/**
 * Responds an error when an error happens when executing the query
 *
 * @param boolean $is_gotofile    whether goto file or not
 * @param String  $error          error after executing the query
 * @param String  $full_sql_query full sql query
 *
 * @return void
 */
function PMA_handleQueryExecuteError($is_gotofile, $error, $full_sql_query)
{
    if ($is_gotofile) {
        $message = PMA_Message::rawError($error);
        $response = PMA_Response::getInstance();
        $response->isSuccess(false);
        $response->addJSON('message', $message);
    } else {
        PMA_Util::mysqlDie($error, $full_sql_query, '', '');
    }
    exit;
}

/**
 * Function to store the query as a bookmark
 *
 * @param String  $db                     the current database
 * @param String  $bkm_user               the bookmarking user
 * @param String  $sql_query_for_bookmark the query to be stored in bookmark
 * @param String  $bkm_label              bookmark label
 * @param boolean $bkm_replace            whether to replace existing bookmarks
 *
 * @return void
 */
function PMA_storeTheQueryAsBookmark($db, $bkm_user, $sql_query_for_bookmark,
    $bkm_label, $bkm_replace
) {
    include_once 'libraries/bookmark.lib.php';
    $bfields = array(
                 'bkm_database' => $db,
                 'bkm_user'  => $bkm_user,
                 'bkm_sql_query' => urlencode($sql_query_for_bookmark),
                 'bkm_label' => $bkm_label
    );

    // Should we replace bookmark?
    if (isset($bkm_replace)) {
        $bookmarks = PMA_Bookmark_getList($db);
        foreach ($bookmarks as $key => $val) {
            if ($val == $bkm_label) {
                PMA_Bookmark_delete($key);
            }
        }
    }

    PMA_Bookmark_save($bfields, isset($_POST['bkm_all_users']));

}

/**
 * Function to execute the SQL query and set the execution time
 *
 * @param String $full_sql_query the full sql query
 *
 * @return mixed  $result the results after running the query
 */
function PMA_executeQueryAndStoreResults($full_sql_query)
{
    // Measure query time.
    $querytime_before = array_sum(explode(' ', microtime()));

    $result = @$GLOBALS['dbi']->tryQuery(
        $full_sql_query, null, PMA_DatabaseInterface::QUERY_STORE
    );
    $querytime_after = array_sum(explode(' ', microtime()));

    $GLOBALS['querytime'] = $querytime_after - $querytime_before;

    // If a stored procedure was called, there may be more results that are
    // queued up and waiting to be flushed from the buffer. So let's do that.
    do {
        $GLOBALS['dbi']->storeResult();
        if (! $GLOBALS['dbi']->moreResults()) {
            break;
        }
    } while ($GLOBALS['dbi']->nextResult());

    return $result;
}

/**
 * Function to get the affected or changed number of rows after executing a query
 *
 * @param boolean $is_affected whether the query affected a table
 * @param mixed   $result      results of executing the query
 * @param int     $num_rows    number of rows affected or changed
 *
 * @return int    $num_rows    number of rows affected or changed
 */
function PMA_getNumberOfRowsAffectedOrChanged($is_affected, $result, $num_rows)
{
    if (! $is_affected) {
        $num_rows = ($result) ? @$GLOBALS['dbi']->numRows($result) : 0;
    } elseif (! isset($num_rows)) {
        $num_rows = @$GLOBALS['dbi']->affectedRows();
    }

    return $num_rows;
}

/**
 * Checks if the current database has changed
 * This could happen if the user sends a query like "USE `database`;"
 *
 * @param String $db the database in the query
 *
 * @return int $reload whether to reload the navigation(1) or not(0)
 */
function PMA_hasCurrentDbChanged($db)
{
    // Checks if the current database has changed
    // This could happen if the user sends a query like "USE `database`;"
    $reload = 0;
    if ($GLOBALS['PMA_String']->strlen($db)) {
        $current_db = $GLOBALS['dbi']->fetchValue('SELECT DATABASE()');
        // $current_db is false, except when a USE statement was sent
        if ($current_db != false && $db !== $current_db) {
            $reload = 1;
        }
    }

    return $reload;
}

/**
 * If a table, database or column gets dropped, clean comments.
 *
 * @param String $db             current database
 * @param String $table          current table
 * @param String $dropped_column dropped column if any
 * @param bool   $purge          whether purge set or not
 * @param array  $extra_data     extra data
 *
 * @return array $extra_data
 */
function PMA_cleanupRelations($db, $table, $dropped_column, $purge, $extra_data)
{
    include_once 'libraries/relation_cleanup.lib.php';

    /** @var PMA_String $pmaString */
    $pmaString = $GLOBALS['PMA_String'];

    if (isset($purge) && $purge == 1) {
        if ($pmaString->strlen($table) && $pmaString->strlen($db)) {
            PMA_relationsCleanupTable($db, $table);
        } elseif ($pmaString->strlen($db)) {
            PMA_relationsCleanupDatabase($db);
        }
    }

    if (isset($dropped_column)
        && !empty($dropped_column)
        && $pmaString->strlen($db)
        && $pmaString->strlen($table)
    ) {
        PMA_relationsCleanupColumn($db, $table, $dropped_column);
        // to refresh the list of indexes (Ajax mode)
        $extra_data['indexes_list'] = PMA_Index::getView($table, $db);
    }

    return $extra_data;
}

/**
 * Function to count the total number of rows for the same 'SELECT' query without
 * the 'LIMIT' clause that may have been programatically added
 *
 * @param int    $num_rows             number of rows affected/changed by the query
 * @param bool   $is_select            whether the query is SELECT or not
 * @param bool   $justBrowsing         whether just browsing or not
 * @param string $db                   the current database
 * @param string $table                the current table
 * @param array  $parsed_sql           parsed sql
 * @param array  $analyzed_sql_results the analyzed query and other variables set
 *                                     after analyzing the query
 *
 * @return int $unlim_num_rows unlimited number of rows
 */
function PMA_countQueryResults(
    $num_rows, $is_select, $justBrowsing,
    $db, $table, $parsed_sql, $analyzed_sql_results
) {
    if (!PMA_isAppendLimitClause($analyzed_sql_results)) {
        // if we did not append a limit, set this to get a correct
        // "Showing rows..." message
        // $_SESSION['tmpval']['max_rows'] = 'all';
        $unlim_num_rows         = $num_rows;
    } elseif ($is_select || $analyzed_sql_results['is_subquery']) {
        //    c o u n t    q u e r y

        // If we are "just browsing", there is only one table,
        // and no WHERE clause (or just 'WHERE 1 '),
        // we do a quick count (which uses MaxExactCount) because
        // SQL_CALC_FOUND_ROWS is not quick on large InnoDB tables

        // However, do not count again if we did it previously
        // due to $find_real_end == true
        if ($justBrowsing) {
            // Get row count (is approximate for InnoDB)
            $unlim_num_rows = PMA_Table::countRecords(
                $db,
                $table,
                false
            );
            /**
             * @todo Can we know at this point that this is InnoDB,
             *       (in this case there would be no need for getting
             *       an exact count)?
             */
            if ($unlim_num_rows < $GLOBALS['cfg']['MaxExactCount']) {
                // Get the exact count if approximate count
                // is less than MaxExactCount
                /**
                 * @todo In countRecords(), MaxExactCount is also verified,
                 *       so can we avoid checking it twice?
                 */
                $unlim_num_rows = PMA_Table::countRecords(
                    $db,
                    $table,
                    true
                );
            }

        } else {
            // add select expression after the SQL_CALC_FOUND_ROWS

            // for UNION, just adding SQL_CALC_FOUND_ROWS
            // after the first SELECT works.

            // take the left part, could be:
            // SELECT
            // (SELECT

            $analyzed_sql = $analyzed_sql_results['analyzed_sql'];

            $count_query = PMA_SQP_format(
                $parsed_sql,
                'query_only',
                0,
                $analyzed_sql[0]['position_of_first_select'] + 1
            );
            $count_query .= ' SQL_CALC_FOUND_ROWS ';
            // add everything that was after the first SELECT
            $count_query .= PMA_SQP_format(
                $parsed_sql,
                'query_only',
                $analyzed_sql[0]['position_of_first_select'] + 1
            );
            // ensure there is no semicolon at the end of the
            // count query because we'll probably add
            // a LIMIT 1 clause after it
            $count_query = rtrim($count_query);
            $count_query = rtrim($count_query, ';');

            // if using SQL_CALC_FOUND_ROWS, add a LIMIT to avoid
            // long delays. Returned count will be complete anyway.
            // (but a LIMIT would disrupt results in an UNION)

            if (! isset($analyzed_sql[0]['queryflags']['union'])) {
                $count_query .= ' LIMIT 1';
            }

            // run the count query

            $GLOBALS['dbi']->tryQuery($count_query);
            // if (mysql_error()) {
            // void.
            // I tried the case
            // (SELECT `User`, `Host`, `Db`, `Select_priv` FROM `db`)
            // UNION (SELECT `User`, `Host`, "%" AS "Db",
            // `Select_priv`
            // FROM `user`) ORDER BY `User`, `Host`, `Db`;
            // and although the generated count_query is wrong
            // the SELECT FOUND_ROWS() work! (maybe it gets the
            // count from the latest query that worked)
            //
            // another case where the count_query is wrong:
            // SELECT COUNT(*), f1 from t1 group by f1
            // and you click to sort on count(*)
            // }
            $unlim_num_rows = $GLOBALS['dbi']->fetchValue('SELECT FOUND_ROWS()');
        } // end else "just browsing"
    } else {// not $is_select
        $unlim_num_rows = 0;
    }

    return $unlim_num_rows;
}

/**
 * Function to handle all aspects relating to executing the query
 *
 * @param array   $analyzed_sql_results   analyzed sql results
 * @param String  $full_sql_query         full sql query
 * @param boolean $is_gotofile            whether to go to a file
 * @param String  $db                     current database
 * @param String  $table                  current table
 * @param boolean $find_real_end          whether to find the real end
 * @param String  $sql_query_for_bookmark sql query to be stored as bookmark
 * @param array   $extra_data             extra data
 *
 * @return mixed
 */
function PMA_executeTheQuery($analyzed_sql_results, $full_sql_query, $is_gotofile,
    $db, $table, $find_real_end, $sql_query_for_bookmark, $extra_data
) {
    // Only if we ask to see the php code
    if (isset($GLOBALS['show_as_php'])) {
        $result = null;
        $num_rows = 0;
        $unlim_num_rows = 0;
    } else { // If we don't ask to see the php code
        if (isset($_SESSION['profiling']) && PMA_Util::profilingSupported()) {
            $GLOBALS['dbi']->query('SET PROFILING=1;');
        }

        $result = PMA_executeQueryAndStoreResults($full_sql_query);

        // Displays an error message if required and stop parsing the script
        $error = $GLOBALS['dbi']->getError();
        if ($error) {
            PMA_handleQueryExecuteError($is_gotofile, $error, $full_sql_query);
        }

        // If there are no errors and bookmarklabel was given,
        // store the query as a bookmark
        if (! empty($_POST['bkm_label']) && ! empty($sql_query_for_bookmark)) {
            $cfgBookmark = PMA_Bookmark_getParams();
            PMA_storeTheQueryAsBookmark(
                $db, $cfgBookmark['user'],
                $sql_query_for_bookmark, $_POST['bkm_label'],
                isset($_POST['bkm_replace']) ? $_POST['bkm_replace'] : null
            );
        } // end store bookmarks

        // Gets the number of rows affected/returned
        // (This must be done immediately after the query because
        // mysql_affected_rows() reports about the last query done)
        $num_rows = PMA_getNumberOfRowsAffectedOrChanged(
            $analyzed_sql_results['is_affected'], $result,
            isset($num_rows) ? $num_rows : null
        );

        // Grabs the profiling results
        if (isset($_SESSION['profiling']) && PMA_Util::profilingSupported()) {
            $profiling_results = $GLOBALS['dbi']->fetchResult('SHOW PROFILE;');
        }

        $justBrowsing = PMA_isJustBrowsing(
            $analyzed_sql_results, isset($find_real_end) ? $find_real_end : null
        );

        $unlim_num_rows = PMA_countQueryResults(
            $num_rows, $analyzed_sql_results['is_select'], $justBrowsing, $db,
            $table, $analyzed_sql_results['parsed_sql'], $analyzed_sql_results
        );

        $extra_data = PMA_cleanupRelations(
            isset($db) ? $db : '', isset($table) ? $table : '',
            isset($_REQUEST['dropped_column']) ? $_REQUEST['dropped_column'] : null,
            isset($_REQUEST['purge']) ? $_REQUEST['purge'] : null,
            isset($extra_data) ? $extra_data : null
        );

        // Update Indexes list.
        if (isset($_REQUEST['index_change'])) {
            $extra_data['indexes_list'] = PMA_Index::getView($table, $db);
        }
    }

    return array($result, $num_rows, $unlim_num_rows,
        isset($profiling_results) ? $profiling_results : null, $extra_data
    );
}
/**
 * Delete related tranformatioinformationn information
 *
 * @param String $db           current database
 * @param String $table        current table
 * @param array  $analyzed_sql analyzed sql query
 *
 * @return void
 */
function PMA_deleteTransformationInfo($db, $table, $analyzed_sql)
{
    include_once 'libraries/transformations.lib.php';
    if ($analyzed_sql[0]['querytype'] == 'ALTER') {
        $posDrop = $GLOBALS['PMA_String']->stripos(
            $analyzed_sql[0]['unsorted_query'],
            'DROP'
        );
        if ($posDrop !== false) {
            $drop_column = PMA_getColumnNameInColumnDropSql(
                $analyzed_sql[0]['unsorted_query']
            );

            if ($drop_column != '') {
                PMA_clearTransformations($db, $table, $drop_column);
            }
        }

    } else if (($analyzed_sql[0]['querytype'] == 'DROP') && ($table != '')) {
        PMA_clearTransformations($db, $table);
    }
}

/**
 * Function to get the message for the no rows returned case
 *
 * @param string $message_to_show      message to show
 * @param array  $analyzed_sql_results analyzed sql results
 * @param int    $num_rows             number of rows
 *
 * @return string $message
 */
function PMA_getMessageForNoRowsReturned($message_to_show, $analyzed_sql_results,
    $num_rows
) {
    if ($analyzed_sql_results['is_delete']) {
        $message = PMA_Message::getMessageForDeletedRows($num_rows);
    } elseif ($analyzed_sql_results['is_insert']) {
        if ($analyzed_sql_results['is_replace']) {
            // For replace we get DELETED + INSERTED row count,
            // so we have to call it affected
            $message = PMA_Message::getMessageForAffectedRows($num_rows);
        } else {
            $message = PMA_Message::getMessageForInsertedRows($num_rows);
        }
        $insert_id = $GLOBALS['dbi']->insertId();
        if ($insert_id != 0) {
            // insert_id is id of FIRST record inserted in one insert,
            // so if we inserted multiple rows, we had to increment this
            $message->addMessage('[br]');
            // need to use a temporary because the Message class
            // currently supports adding parameters only to the first
            // message
            $_inserted = PMA_Message::notice(__('Inserted row id: %1$d'));
            $_inserted->addParam($insert_id + $num_rows - 1);
            $message->addMessage($_inserted);
        }
    } elseif ($analyzed_sql_results['is_affected']) {
        $message = PMA_Message::getMessageForAffectedRows($num_rows);

        // Ok, here is an explanation for the !$is_select.
        // The form generated by sql_query_form.lib.php
        // and db_sql.php has many submit buttons
        // on the same form, and some confusion arises from the
        // fact that $message_to_show is sent for every case.
        // The $message_to_show containing a success message and sent with
        // the form should not have priority over errors
    } elseif (! empty($message_to_show) && ! $analyzed_sql_results['is_select']) {
        $message = PMA_Message::rawSuccess(htmlspecialchars($message_to_show));
    } elseif (! empty($GLOBALS['show_as_php'])) {
        $message = PMA_Message::success(__('Showing as PHP code'));
    } elseif (isset($GLOBALS['show_as_php'])) {
        /* User disable showing as PHP, query is only displayed */
        $message = PMA_Message::notice(__('Showing SQL query'));
    } else {
        $message = PMA_Message::success(
            __('MySQL returned an empty result set (i.e. zero rows).')
        );
    }

    if (isset($GLOBALS['querytime'])) {
        $_querytime = PMA_Message::notice(
            '(' . __('Query took %01.4f seconds.') . ')'
        );
        $_querytime->addParam($GLOBALS['querytime']);
        $message->addMessage($_querytime);
    }

    // In case of ROLLBACK, notify the user.
    if (isset($_REQUEST['rollback_query'])) {
        $message->addMessage(__('[ROLLBACK occurred.]'));
    }

    return $message;
}

/**
 * Function to send the Ajax response when no rows returned
 *
 * @param string $message              message to be send
 * @param array  $analyzed_sql         analyzed sql
 * @param object $displayResultsObject DisplayResult instance
 * @param array  $extra_data           extra data
 *
 * @return void
 */
function PMA_sendAjaxResponseForNoResultsReturned($message, $analyzed_sql,
    $displayResultsObject, $extra_data
) {
    /**
     * @todo find a better way to make getMessage() in Header.class.php
     *       output the intended message
     */
    $GLOBALS['message'] = $message;

    if ($GLOBALS['cfg']['ShowSQL']) {
        $extra_data['sql_query'] = PMA_Util::getMessage(
            $message, $GLOBALS['sql_query'], 'success'
        );
    }
    if (isset($GLOBALS['reload']) && $GLOBALS['reload'] == 1) {
        $extra_data['reload'] = 1;
        $extra_data['db'] = $GLOBALS['db'];
    }
    $response = PMA_Response::getInstance();
    $response->isSuccess($message->isSuccess());
    // No need to manually send the message
    // The Response class will handle that automatically
    $query__type = PMA_DisplayResults::QUERY_TYPE_SELECT;
    if ($analyzed_sql[0]['querytype'] == $query__type) {
        $createViewHTML = $displayResultsObject->getCreateViewQueryResultOp(
            $analyzed_sql
        );
        $response->addHTML($createViewHTML . '<br />');
    }

    $response->addJSON(isset($extra_data) ? $extra_data : array());
    if (empty($_REQUEST['ajax_page_request'])) {
        $response->addJSON('message', $message);
        exit;
    }
}

/**
 * Function to respond back when the query returns zero rows
 * This method is called
 * 1-> When browsing an empty table
 * 2-> When executing a query on a non empty table which returns zero results
 * 3-> When executing a query on an empty table
 * 4-> When executing an INSERT, UPDATE, DELETE query from the SQL tab
 * 5-> When deleting a row from BROWSE tab
 * 6-> When searching using the SEARCH tab which returns zero results
 * 7-> When changing the structure of the table except change operation
 *
 * @param array  $analyzed_sql_results analyzed sql results
 * @param string $db                   current database
 * @param string $table                current table
 * @param string $message_to_show      message to show
 * @param int    $num_rows             number of rows
 * @param object $displayResultsObject DisplayResult instance
 * @param array  $extra_data           extra data
 *
 * @return void
 */
function PMA_sendQueryResponseForNoResultsReturned($analyzed_sql_results, $db,
    $table, $message_to_show, $num_rows, $displayResultsObject, $extra_data
) {
    if (PMA_isDeleteTransformationInfo($analyzed_sql_results)) {
        PMA_deleteTransformationInfo(
            $db, $table, $analyzed_sql_results['analyzed_sql']
        );
    }

    $message = PMA_getMessageForNoRowsReturned(
        isset($message_to_show) ? $message_to_show : null, $analyzed_sql_results,
        $num_rows
    );
    if (!isset($GLOBALS['show_as_php'])) {
        PMA_sendAjaxResponseForNoResultsReturned(
            $message, $analyzed_sql_results['analyzed_sql'],
            $displayResultsObject,
            isset($extra_data) ? $extra_data : null
        );
    }
    exit();
}

/**
 * Function to send response for ajax grid edit
 *
 * @param object $result result of the executed query
 *
 * @return void
 */
function PMA_sendResponseForGridEdit($result)
{
    $row = $GLOBALS['dbi']->fetchRow($result);
    $field_flags = $GLOBALS['dbi']->fieldFlags($result, 0);
    if (stristr($field_flags, PMA_DisplayResults::BINARY_FIELD)) {
        $row[0] = bin2hex($row[0]);
    }
    $response = PMA_Response::getInstance();
    $response->addJSON('value', $row[0]);
    exit;
}

/**
 * Function to get html for the sql query results div
 *
 * @param string $previous_update_query_html html for the previously executed query
 * @param string $profiling_chart_html       html for profiling
 * @param object $missing_unique_column_msg  message for the missing unique column
 * @param object $bookmark_created_msg       message for bookmark creation
 * @param string $table_html                 html for the table for displaying sql
 *                                           results
 * @param string $indexes_problems_html      html for displaying errors in indexes
 * @param string $bookmark_support_html      html for displaying bookmark form
 * @param string $print_button_html          html for the print button in printview
 *
 * @return string $html_output
 */
function PMA_getHtmlForSqlQueryResults($previous_update_query_html,
    $profiling_chart_html, $missing_unique_column_msg, $bookmark_created_msg,
    $table_html, $indexes_problems_html, $bookmark_support_html, $print_button_html
) {
    //begin the sqlqueryresults div here. container div
    $html_output = '<div id="sqlqueryresults" class="ajax">';
    $html_output .= isset($previous_update_query_html)
        ? $previous_update_query_html : '';
    $html_output .= isset($profiling_chart_html) ? $profiling_chart_html : '';
    $html_output .= isset($missing_unique_column_msg)
        ? $missing_unique_column_msg->getDisplay() : '';
    $html_output .= isset($bookmark_created_msg)
        ? $bookmark_created_msg->getDisplay() : '';
    $html_output .= $table_html;
    $html_output .= isset($indexes_problems_html) ? $indexes_problems_html : '';
    $html_output .= isset($bookmark_support_html) ? $bookmark_support_html : '';
    $html_output .= isset($print_button_html) ? $print_button_html : '';
    $html_output .= '</div>'; // end sqlqueryresults div

    return $html_output;
}

/**
 * Returns a message for successful creation of a bookmark or null if a bookmark
 * was not created
 *
 * @return PMA_message $bookmark_created_msg
 */
function PMA_getBookmarkCreatedMessage()
{
    if (isset($_GET['label'])) {
        $bookmark_created_msg = PMA_message::success(
            __('Bookmark %s has been created.')
        );
        $bookmark_created_msg->addParam($_GET['label']);
    } else {
        $bookmark_created_msg = null;
    }

    return $bookmark_created_msg;
}

/**
 * Function to get html for the sql query results table
 *
 * @param array  $sql_data             sql data
 * @param object $displayResultsObject instance of DisplayResult.class
 * @param string $db                   current database
 * @param string $goto                 goto page url
 * @param string $pmaThemeImage        theme image uri
 * @param string $url_query            url query
 * @param string $disp_mode            display mode
 * @param string $sql_limit_to_append  sql limit to append
 * @param bool   $editable             whether the result table is editable or not
 * @param int    $unlim_num_rows       unlimited number of rows
 * @param int    $num_rows             number of rows
 * @param bool   $showtable            whether to show table or not
 * @param object $result               result of the executed query
 * @param array  $analyzed_sql_results analyzed sql results
 *
 * @return String
 */
function PMA_getHtmlForSqlQueryResultsTable($sql_data, $displayResultsObject, $db,
    $goto, $pmaThemeImage, $url_query, $disp_mode, $sql_limit_to_append,
    $editable, $unlim_num_rows, $num_rows, $showtable, $result,
    $analyzed_sql_results
) {
    $printview = isset($_REQUEST['printview']) ? $_REQUEST['printview'] : null;
    if (! empty($sql_data) && ($sql_data['valid_queries'] > 1)
        || $analyzed_sql_results['is_procedure']
    ) {
        $_SESSION['is_multi_query'] = true;
        $table_html = PMA_getTableHtmlForMultipleQueries(
            $displayResultsObject, $db, $sql_data, $goto,
            $pmaThemeImage, $printview, $url_query,
            $disp_mode, $sql_limit_to_append, $editable
        );
    } else {
        if (isset($result) && $result) {
            $fields_meta = $GLOBALS['dbi']->getFieldsMeta($result);
            $fields_cnt  = count($fields_meta);
        }
        $_SESSION['is_multi_query'] = false;
        $displayResultsObject->setProperties(
            $unlim_num_rows, $fields_meta, $analyzed_sql_results['is_count'],
            $analyzed_sql_results['is_export'], $analyzed_sql_results['is_func'],
            $analyzed_sql_results['is_analyse'], $num_rows,
            $fields_cnt, $GLOBALS['querytime'], $pmaThemeImage, $GLOBALS['text_dir'],
            $analyzed_sql_results['is_maint'], $analyzed_sql_results['is_explain'],
            $analyzed_sql_results['is_show'], $showtable, $printview, $url_query,
            $editable
        );

        $table_html = $displayResultsObject->getTable(
            $result, $disp_mode, $analyzed_sql_results['analyzed_sql']
        );
        $GLOBALS['dbi']->freeResult($result);
    }

    return $table_html;
}

/**
 * Function to get html for the previous query if there is such. If not will return
 * null
 *
 * @param string $disp_query   display query
 * @param bool   $showSql      whether to show sql
 * @param array  $sql_data     sql data
 * @param string $disp_message display message
 *
 * @return string $previous_update_query_html
 */
function PMA_getHtmlForPreviousUpdateQuery($disp_query, $showSql, $sql_data,
    $disp_message
) {
    // previous update query (from tbl_replace)
    if (isset($disp_query) && ($showSql == true) && empty($sql_data)) {
        $previous_update_query_html = PMA_Util::getMessage(
            $disp_message, $disp_query, 'success'
        );
    } else {
        $previous_update_query_html = null;
    }

    return $previous_update_query_html;
}

/**
 * To get the message if a column index is missing. If not will return null
 *
 * @param string  $table    current table
 * @param string  $db       current database
 * @param boolean $editable whether the results table can be editable or not
 *
 * @return PMA_message $message
 */
function PMA_getMessageIfMissingColumnIndex($table, $db, $editable)
{
    if (!empty($table) && ($GLOBALS['dbi']->isSystemSchema($db) || !$editable)) {
        $missing_unique_column_msg = PMA_message::notice(
            __(
                'Current selection does not contain a unique column.'
                . ' Grid edit, checkbox, Edit, Copy and Delete features'
                . ' are not available.'
            )
        );
    } else {
        $missing_unique_column_msg = null;
    }

    return $missing_unique_column_msg;
}

/**
 * Function to get html to display problems in indexes
 *
 * @param string     $query_type     query type
 * @param array|null $selectedTables array of table names selected from the
 *                                   database structure page, for an action
 *                                   like check table, optimize table,
 *                                   analyze table or repair table
 * @param string     $db             current database
 *
 * @return string
 */
function PMA_getHtmlForIndexesProblems($query_type, $selectedTables, $db)
{
    // BEGIN INDEX CHECK See if indexes should be checked.
    if (isset($query_type)
        && $query_type == 'check_tbl'
        && isset($selectedTables)
        && is_array($selectedTables)
    ) {
        $indexes_problems_html = '';
        foreach ($selectedTables as $tbl_name) {
            $check = PMA_Index::findDuplicates($tbl_name, $db);
            if (! empty($check)) {
                $indexes_problems_html .= sprintf(
                    __('Problems with indexes of table `%s`'), $tbl_name
                );
                $indexes_problems_html .= $check;
            }
        }
    } else {
        $indexes_problems_html = null;
    }

    return $indexes_problems_html;
}

/**
 * Function to get the html for the print button in printview
 *
 * @return string $print_button_html html for the print button
 */
function PMA_getHtmlForPrintButton()
{
    // Do print the page if required
    if (isset($_REQUEST['printview']) && $_REQUEST['printview'] == '1') {
        $print_button_html = PMA_Util::getButton();
    } else {
        $print_button_html = null;
    }

    return $print_button_html;
}

/**
 * Function to display results when the executed query returns non empty results
 *
 * @param array      $result               executed query results
 * @param array      $analyzed_sql_results analysed sql results
 * @param string     $db                   current database
 * @param string     $table                current table
 * @param string     $disp_mode            display mode
 * @param string     $message              message to show
 * @param array      $sql_data             sql data
 * @param object     $displayResultsObject Instance of DisplyResults.class
 * @param string     $goto                 goto page url
 * @param string     $pmaThemeImage        uri of the theme image
 * @param string     $sql_limit_to_append  sql limit to append
 * @param int        $unlim_num_rows       unlimited number of rows
 * @param int        $num_rows             number of rows
 * @param string     $full_sql_query       full sql query
 * @param string     $disp_query           display query
 * @param string     $disp_message         display message
 * @param array      $profiling_results    profiling results
 * @param string     $query_type           query type
 * @param array|null $selectedTables       array of table names selected from
 *                                         the database structure page, for an
 *                                         action like check table, optimize
 *                                         table, analyze table or repair table
 * @param string     $sql_query            sql query
 * @param string     $complete_query       complete sql query
 *
 * @return void
 */
function PMA_sendQueryResponseForResultsReturned($result,
    $analyzed_sql_results, $db, $table, $disp_mode, $message, $sql_data,
    $displayResultsObject, $goto, $pmaThemeImage, $sql_limit_to_append,
    $unlim_num_rows, $num_rows,  $full_sql_query, $disp_query,
    $disp_message, $profiling_results, $query_type, $selectedTables, $sql_query,
    $complete_query
) {
    // If we are retrieving the full value of a truncated field or the original
    // value of a transformed field, show it here
    if (isset($_REQUEST['grid_edit']) && $_REQUEST['grid_edit'] == true) {
        PMA_sendResponseForGridEdit($result);
        // script has exited at this point
    }

    // Gets the list of fields properties
    if (isset($result) && $result) {
        $fields_meta = $GLOBALS['dbi']->getFieldsMeta($result);
    }

    // Should be initialized these parameters before parsing
    $showtable = isset($showtable) ? $showtable : null;
    $url_query = isset($url_query) ? $url_query : null;

    $response = PMA_Response::getInstance();
    $header   = $response->getHeader();
    $scripts  = $header->getScripts();

    // hide edit and delete links:
    // - for information_schema
    // - if the result set does not contain all the columns of a unique key
    //   (unless this is an updatable view)

    $sele_exp_cls = $analyzed_sql_results['analyzed_sql'][0]['select_expr_clause'];
    $updatableView
        = trim($sele_exp_cls) == '*'
        && PMA_Table::isUpdatableView($db, $table);

    $has_unique = PMA_resultSetContainsUniqueKey(
        $db, $table, $fields_meta
    );

    $just_one_table = PMA_resultSetHasJustOneTable($fields_meta);

    $editable = ($has_unique || $updatableView) && $just_one_table;

    // Displays the results in a table
    if (empty($disp_mode)) {
        // see the "PMA_setDisplayMode()" function in
        // libraries/DisplayResults.class.php
        $disp_mode = 'urdr111101';
    }
    if (!empty($table) && ($GLOBALS['dbi']->isSystemSchema($db) || !$editable)) {
        $disp_mode = 'nnnn110111';
    }
    if ( isset($_REQUEST['printview']) && $_REQUEST['printview'] == '1') {
        $disp_mode = 'nnnn000000';
    }

    if (isset($_REQUEST['table_maintenance'])) {
        $scripts->addFile('makegrid.js');
        $scripts->addFile('sql.js');
        $table_maintenance_html = '';
        if (isset($message)) {
            $message = PMA_Message::success($message);
            $table_maintenance_html = PMA_Util::getMessage(
                $message, $GLOBALS['sql_query'], 'success'
            );
        }
        $table_maintenance_html .= PMA_getHtmlForSqlQueryResultsTable(
            isset($sql_data) ? $sql_data : null, $displayResultsObject, $db, $goto,
            $pmaThemeImage, $url_query, $disp_mode, $sql_limit_to_append,
            false, $unlim_num_rows, $num_rows, $showtable, $result,
            $analyzed_sql_results
        );
        if (empty($sql_data) || ($sql_data['valid_queries'] = 1)) {
            $response->addHTML($table_maintenance_html);
            exit();
        }
    }

    if (!isset($_REQUEST['printview']) || $_REQUEST['printview'] != '1') {
        $scripts->addFile('makegrid.js');
        $scripts->addFile('sql.js');
        unset($GLOBALS['message']);
        //we don't need to buffer the output in getMessage here.
        //set a global variable and check against it in the function
        $GLOBALS['buffer_message'] = false;
    }

    $print_view_header_html = PMA_getHtmlForPrintViewHeader(
        $db, $full_sql_query, $num_rows
    );

    $previous_update_query_html = PMA_getHtmlForPreviousUpdateQuery(
        isset($disp_query) ? $disp_query : null,
        $GLOBALS['cfg']['ShowSQL'], isset($sql_data) ? $sql_data : null,
        isset($disp_message) ? $disp_message : null
    );

    $profiling_chart_html = PMA_getHtmlForProfilingChart(
        $disp_mode, $db, isset($profiling_results) ? $profiling_results : null
    );

    $missing_unique_column_msg = PMA_getMessageIfMissingColumnIndex(
        $table, $db, $editable
    );

    $bookmark_created_msg = PMA_getBookmarkCreatedMessage();

    $table_html = PMA_getHtmlForSqlQueryResultsTable(
        isset($sql_data) ? $sql_data : null, $displayResultsObject, $db, $goto,
        $pmaThemeImage, $url_query, $disp_mode, $sql_limit_to_append,
        $editable, $unlim_num_rows, $num_rows, $showtable, $result,
        $analyzed_sql_results
    );

    $indexes_problems_html = PMA_getHtmlForIndexesProblems(
        isset($query_type) ? $query_type : null,
        isset($selectedTables) ? $selectedTables : null, $db
    );

    $cfgBookmark = PMA_Bookmark_getParams();
    if ($cfgBookmark) {
        $bookmark_support_html = PMA_getHtmlForBookmark(
            $disp_mode,
            $cfgBookmark,
            $sql_query, $db, $table,
            isset($complete_query) ? $complete_query : $sql_query,
            $cfgBookmark['user']
        );
    } else {
        $bookmark_support_html = '';
    }

    $print_button_html = PMA_getHtmlForPrintButton();

    $html_output = isset($table_maintenance_html) ? $table_maintenance_html : '';

    $html_output .= isset($print_view_header_html) ? $print_view_header_html : '';

    $html_output .= PMA_getHtmlForSqlQueryResults(
        $previous_update_query_html, $profiling_chart_html,
        $missing_unique_column_msg, $bookmark_created_msg,
        $table_html, $indexes_problems_html, $bookmark_support_html,
        $print_button_html
    );

    $response->addHTML($html_output);

    exit();
}

/**
 * Function to send response for both empty results and non empty results
 *
 * @param int        $num_rows             number of rows returned by the
 *                                         executed query
 * @param int        $unlim_num_rows       unlimited number of rows
 * @param bool       $is_affected          is affected
 * @param string     $db                   current database
 * @param string     $table                current table
 * @param string     $message_to_show      message to show
 * @param array      $analyzed_sql_results analyzed Sql Results
 * @param object     $displayResultsObject Instance of DisplayResult class
 * @param array      $extra_data           extra data
 * @param array      $result               executed query results
 * @param string     $disp_mode            display mode
 * @param object     $message              message
 * @param array      $sql_data             sql data
 * @param string     $goto                 goto page url
 * @param string     $pmaThemeImage        uri of the PMA theme image
 * @param string     $sql_limit_to_append  sql limit to append
 * @param string     $full_sql_query       full sql query
 * @param string     $disp_query           display query
 * @param string     $disp_message         display message
 * @param array      $profiling_results    profiling results
 * @param string     $query_type           query type
 * @param array|null $selectedTables       array of table names selected from
 *                                         the database structure page, for an
 *                                         action like check table, optimize
 *                                         table, analyze table or repair table
 * @param string     $sql_query            sql query
 * @param string     $complete_query       complete query
 *
 * @return void
 */
function PMA_sendQueryResponse($num_rows, $unlim_num_rows, $is_affected,
    $db, $table, $message_to_show, $analyzed_sql_results, $displayResultsObject,
    $extra_data, $result, $disp_mode,$message, $sql_data,
    $goto, $pmaThemeImage, $sql_limit_to_append, $full_sql_query,
    $disp_query, $disp_message, $profiling_results, $query_type,
    $selectedTables, $sql_query, $complete_query
) {
    // No rows returned -> move back to the calling page
    if ((0 == $num_rows && 0 == $unlim_num_rows) || $is_affected) {
        PMA_sendQueryResponseForNoResultsReturned(
            $analyzed_sql_results, $db, $table,
            isset($message_to_show) ? $message_to_show : null,
            $num_rows, $displayResultsObject, $extra_data
        );

    } else {
        // At least one row is returned -> displays a table with results
        PMA_sendQueryResponseForResultsReturned(
            isset($result) ? $result : null, $analyzed_sql_results,
            $db, $table, isset($disp_mode) ? $disp_mode : null,
            isset($message) ? $message : null, isset($sql_data) ? $sql_data : null,
            $displayResultsObject, $goto, $pmaThemeImage,
            $sql_limit_to_append, $unlim_num_rows,
            $num_rows, $full_sql_query,
            isset($disp_query) ? $disp_query : null,
            isset($disp_message) ? $disp_message : null, $profiling_results,
            isset($query_type) ? $query_type : null,
            isset($selectedTables) ? $selectedTables : null, $sql_query,
            isset($complete_query) ? $complete_query : null
        );
    } // end rows returned
}

/**
 * Function to execute the query and send the response
 *
 * @param array      $analyzed_sql_results   analysed sql results
 * @param bool       $is_gotofile            whether goto file or not
 * @param string     $db                     current database
 * @param string     $table                  current table
 * @param bool|null  $find_real_end          whether to find real end or not
 * @param string     $sql_query_for_bookmark the sql query to be stored as bookmark
 * @param array|null $extra_data             extra data
 * @param bool       $is_affected            whether affected or not
 * @param string     $message_to_show        message to show
 * @param string     $disp_mode              display mode
 * @param string     $message                message
 * @param array|null $sql_data               sql data
 * @param string     $goto                   goto page url
 * @param string     $pmaThemeImage          uri of the PMA theme image
 * @param string     $disp_query             display query
 * @param string     $disp_message           display message
 * @param string     $query_type             query type
 * @param string     $sql_query              sql query
 * @param array      $selectedTables         array of table names selected from the
 *                                           database structure page, for an action
 *                                           like check table, optimize table,
 *                                           analyze table or repair table
 * @param string     $complete_query         complete query
 *
 * @return void
 */
function PMA_executeQueryAndSendQueryResponse($analyzed_sql_results,
    $is_gotofile, $db, $table, $find_real_end, $sql_query_for_bookmark,
    $extra_data, $is_affected, $message_to_show, $disp_mode, $message,
    $sql_data, $goto, $pmaThemeImage, $disp_query, $disp_message,
    $query_type, $sql_query, $selectedTables, $complete_query
) {
    // Include PMA_Index class for use in PMA_DisplayResults class
    include_once './libraries/Index.class.php';

    include 'libraries/DisplayResults.class.php';

    // Handle remembered sorting order, only for single table query
    // Handling is not required when it's a union query
    // (the parser never sets the 'union' key to 0)
    if (PMA_isRememberSortingOrder($analyzed_sql_results)
        && ! isset($analyzed_sql_results['analyzed_sql'][0]['queryflags']['union'])
    ) {
        PMA_handleSortOrder($db, $table, $analyzed_sql_results, $sql_query);
    }

    $displayResultsObject = new PMA_DisplayResults(
        $GLOBALS['db'], $GLOBALS['table'], $GLOBALS['goto'], $sql_query
    );
    $displayResultsObject->setConfigParamsForDisplayTable();

    // assign default full_sql_query
    $full_sql_query = $sql_query;

    // Do append a "LIMIT" clause?
    if (PMA_isAppendLimitClause($analyzed_sql_results)) {
        list($sql_limit_to_append, $full_sql_query) = PMA_appendLimitClause(
            $analyzed_sql_results['analyzed_sql']
        );
    } else {
        $sql_limit_to_append = '';
    }

    $GLOBALS['reload'] = PMA_hasCurrentDbChanged($db);
    $GLOBALS['dbi']->selectDb($db);

    // Execute the query
    list($result, $num_rows, $unlim_num_rows, $profiling_results, $extra_data)
        = PMA_executeTheQuery(
            $analyzed_sql_results,
            $full_sql_query,
            $is_gotofile,
            $db,
            $table,
            isset($find_real_end) ? $find_real_end : null,
            isset($sql_query_for_bookmark) ? $sql_query_for_bookmark : null,
            isset($extra_data) ? $extra_data : null
        );

    PMA_sendQueryResponse(
        $num_rows,
        $unlim_num_rows,
        $is_affected,
        $db,
        $table,
        isset($message_to_show) ? $message_to_show : null,
        $analyzed_sql_results,
        $displayResultsObject,
        $extra_data,
        isset($result) ? $result : null,
        isset($disp_mode) ? $disp_mode : null,
        isset($message) ? $message : null,
        isset($sql_data) ? $sql_data : null,
        $goto,
        $pmaThemeImage,
        $sql_limit_to_append,
        $full_sql_query,
        isset($disp_query) ? $disp_query : null,
        isset($disp_message) ? $disp_message : null,
        $profiling_results,
        isset($query_type) ? $query_type : null,
        isset($selectedTables) ? $selectedTables : null,
        $sql_query,
        isset($complete_query) ? $complete_query : null
    );
}

/**
 * Function to define pos to display a row
 *
 * @param Int $number_of_line Number of the line to display
 * @param Int $max_rows       Number of rows by page
 *
 * @return Int Start position to display the line
 */
function PMA_getStartPosToDisplayRow($number_of_line, $max_rows = null)
{
    if (null === $max_rows) {
        $max_rows = $_SESSION['tmpval']['max_rows'];
    }

    return @((ceil($number_of_line / $max_rows) - 1) * $max_rows);
}

/**
 * Function to calculate new pos if pos is higher than number of rows
 * of displayed table
 *
 * @param String   $db    Database name
 * @param String   $table Table name
 * @param Int|null $pos   Initial position
 *
 * @return Int Number of pos to display last page
 */
function PMA_calculatePosForLastPage($db, $table, $pos)
{
    if (null === $pos) {
        $pos = $_SESSION['tmpval']['pos'];
    }

    $unlim_num_rows = PMA_Table::countRecords($db, $table, true);
    //If position is higher than number of rows
    if ($unlim_num_rows <= $pos && 0 != $pos) {
        $pos = PMA_getStartPosToDisplayRow($unlim_num_rows);
    }

    return $pos;
}

?>