for Loic
[phpmyadmin/crack.git] / db_details_qbe.php3
blob3277f7d4f1bb8796bce48ca782d60909781097d9
1 <?php
2 /* $Id$ */
5 /**
6 * Get the values of the variables posted or sent to this script and display
7 * the headers
8 */
9 require('./libraries/grab_globals.lib.php3');
10 require('./libraries/common.lib.php3');
11 require('./libraries/relation.lib.php3');
14 /**
15 * Gets the relation settings
17 $cfgRelation = PMA_getRelationsParam();
20 /**
21 * A query has been submitted -> execute it, else display the headers
23 if (isset($submit_sql) && eregi('^SELECT', $encoded_sql_query)) {
24 $goto = 'db_details.php3';
25 $zero_rows = htmlspecialchars($strSuccess);
26 $sql_query = urldecode($encoded_sql_query);
27 if (get_magic_quotes_gpc()) {
28 $sql_query = addslashes($sql_query);
30 include('./sql.php3');
31 exit();
32 } else {
33 $sub_part = '_qbe';
34 include('./db_details_common.php3');
35 $url_query .= '&amp;goto=db_details_qbe.php3';
36 include('./db_details_db_info.php3');
39 if (isset($submit_sql) && !eregi('^SELECT', $encoded_sql_query)) {
40 echo '<p class="warning">' . $strHaveToShow . '</p>';
44 /**
45 * Initialize some variables
47 if (empty($Columns)) {
48 $Columns = 3; // Initial number of columns
50 if (!isset($Add_Col)) {
51 $Add_Col = '';
53 if (!isset($Add_Row)) {
54 $Add_Row = '';
56 if (!isset($Rows)) {
57 $Rows = '';
59 if (!isset($InsCol)) {
60 $InsCol = '';
62 if (!isset($DelCol)) {
63 $DelCol = '';
65 if (!isset($prev_Criteria)) {
66 $prev_Criteria = '';
68 // workaround for a PHP3 problem
69 if (!isset($Criteria)) {
70 //$Criteria = '';
71 $Criteria = array();
72 for ($i = 0; $i < $Columns; $i++) {
73 $Criteria[$i] = '';
76 if (!isset($InsRow)) {
77 // $InsRow = '';
78 $InsRow = array();
79 for ($i = 0; $i < $Columns; $i++) {
80 $InsRow[$i] = '';
83 if (!isset($DelRow)) {
84 // $DelRow = '';
85 $DelRow = array();
86 for ($i = 0; $i < $Columns; $i++) {
87 $DelRow[$i] = '';
90 if (!isset($AndOrRow)) {
91 // $AndOrRow = '';
92 $AndOrRow = array();
93 for ($i = 0; $i < $Columns; $i++) {
94 $AndOrRow[$i] = '';
97 if (!isset($AndOrCol)) {
98 // $AndOrCol = '';
99 $AndOrCol = array();
100 for ($i = 0; $i < $Columns; $i++) {
101 $AndOrCol[$i] = '';
104 // minimum width
105 $wid = 12;
106 $col = $Columns + $Add_Col;
107 if ($col < 0) {
108 $col = 0;
110 $row = $Rows + $Add_Row;
111 if ($row < 0) {
112 $row = 0;
117 * Prepares the form
119 $tbl_result = PMA_mysql_list_tables($db);
120 $tbl_result_cnt = mysql_num_rows($tbl_result);
121 $i = 0;
122 $k = 0;
124 // The tables list sent by a previously submitted form
125 if (!empty($TableList)) {
126 for ($x = 0; $x < count($TableList); $x++) {
127 $tbl_names[urldecode($TableList[$x])] = ' selected="selected"';
129 } // end if
131 // The tables list gets from MySQL
132 while ($i < $tbl_result_cnt) {
133 $tbl = PMA_mysql_tablename($tbl_result, $i);
134 $fld_results = @PMA_mysql_list_fields($db, $tbl) or PMA_mysqlDie(PMA_mysql_error(), 'PMA_mysql_list_fields(' . $db . ', ' . $tbl . ')', FALSE, $err_url);
135 $fld_results_cnt = ($fld_results) ? mysql_num_fields($fld_results) : 0;
136 $j = 0;
138 if (empty($tbl_names[$tbl]) && !empty($TableList)) {
139 $tbl_names[$tbl] = '';
140 } else {
141 $tbl_names[$tbl] = ' selected="selected"';
142 } // end if
144 // The fields list per selected tables
145 if ($tbl_names[$tbl] == ' selected="selected"') {
146 $fld[$k++] = PMA_backquote($tbl) . '.*';
147 while ($j < $fld_results_cnt) {
148 $fld[$k] = PMA_mysql_field_name($fld_results, $j);
149 $fld[$k] = PMA_backquote($tbl) . '.' . PMA_backquote($fld[$k]);
151 // increase the width if necessary
152 if (strlen($fld[$k]) > $wid) {
153 $wid = strlen($fld[$k]);
154 } //end if
156 $k++;
157 $j++;
158 } // end while
159 } // end if
160 if ($fld_results) {
161 mysql_free_result($fld_results);
164 $i++;
165 } // end if
166 mysql_free_result($tbl_result);
168 // largest width found
169 $realwidth = $wid . 'ex';
173 * Displays the form
177 <!-- Query by example form -->
178 <form action="db_details_qbe.php3" method="post">
179 <table border="<?php echo $cfg['Border']; ?>">
181 <!-- Fields row -->
182 <tr>
183 <td align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
184 <b><?php echo $strField; ?>&nbsp;:&nbsp;</b>
185 </td>
186 <?php
187 $z = 0;
188 for ($x = 0; $x < $col; $x++) {
189 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
191 <td align="center" bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
192 <select style="width: <?php echo $realwidth; ?>" name="Field[<?php echo $z; ?>]" size="1">
193 <option value=""></option>
194 <?php
195 echo "\n";
196 for ($y = 0; $y < sizeof($fld); $y++) {
197 if ($fld[$y] == '') {
198 $sel = ' selected="selected"';
199 } else {
200 $sel = '';
202 echo ' ';
203 echo '<option value="' . urlencode($fld[$y]) . '"' . $sel . '>' . htmlspecialchars($fld[$y]) . '</option>' . "\n";
204 } // end for
206 </select>
207 </td>
208 <?php
209 $z++;
210 } // end if
211 echo "\n";
213 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
214 continue;
217 <td align="center" bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
218 <select style="width: <?php echo $realwidth; ?>" name="Field[<?php echo $z; ?>]" size="1">
219 <option value=""></option>
220 <?php
221 echo "\n";
222 for ($y = 0; $y < sizeof($fld); $y++) {
223 if (isset($Field[$x]) && $fld[$y] == urldecode($Field[$x])) {
224 $curField[$z] = urldecode($Field[$x]);
225 $sel = ' selected="selected"';
226 } else {
227 $sel = '';
228 } // end if
229 echo ' ';
230 echo '<option value="' . urlencode($fld[$y]) . '"' . $sel . '>' . htmlspecialchars($fld[$y]) . '</option>' . "\n";
231 } // end for
233 </select>
234 </td>
235 <?php
236 $z++;
237 echo "\n";
238 } // end for
240 </tr>
242 <!-- Sort row -->
243 <tr>
244 <td align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
245 <b><?php echo $strSort; ?>&nbsp;:&nbsp;</b>
246 </td>
247 <?php
248 $z = 0;
249 for ($x = 0; $x < $col; $x++) {
250 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
252 <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
253 <select style="width: <?php echo $realwidth; ?>" name="Sort[<?php echo $z; ?>]" size="1">
254 <option value=""></option>
255 <option value="ASC"><?php echo $strAscending; ?></option>
256 <option value="DESC"><?php echo $strDescending; ?></option>
257 </select>
258 </td>
259 <?php
260 $z++;
261 } // end if
262 echo "\n";
264 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
265 continue;
268 <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
269 <select style="width: <?php echo $realwidth; ?>" name="Sort[<?php echo $z; ?>]" size="1">
270 <option value=""></option>
271 <?php
272 echo "\n";
274 // If they have chosen all fields using the * selector,
275 // then sorting is not available
276 // Robbat2 - Fix for Bug #570698
277 if (isset($Sort[$x]) && isset($Field[$x]) && (substr(urldecode($Field[$x]),-2) == '.*')) {
278 $Sort[$x] = '';
279 } //end if
281 if (isset($Sort[$x]) && $Sort[$x] == 'ASC') {
282 $curSort[$z] = $Sort[$x];
283 $sel = ' selected="selected"';
284 } else {
285 $sel = '';
286 } // end if
287 echo ' ';
288 echo '<option value="ASC"' . $sel . '>' . $strAscending . '</option>' . "\n";
289 if (isset($Sort[$x]) && $Sort[$x] == 'DESC') {
290 $curSort[$z] = $Sort[$x];
291 $sel = ' selected="selected"';
292 } else {
293 $sel = '';
294 } // end if
295 echo ' ';
296 echo '<option value="DESC"' . $sel . '>' . $strDescending . '</option>' . "\n";
298 </select>
299 </td>
300 <?php
301 $z++;
302 echo "\n";
303 } // end for
305 </tr>
307 <!-- Show row -->
308 <tr>
309 <td align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
310 <b><?php echo $strShow; ?>&nbsp;:&nbsp;</b>
311 </td>
312 <?php
313 $z = 0;
314 for ($x = 0; $x < $col; $x++) {
315 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
317 <td align="center" bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
318 <input type="checkbox" name="Show[<?php echo $z; ?>]" />
319 </td>
320 <?php
321 $z++;
322 } // end if
323 echo "\n";
325 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
326 continue;
328 if (isset($Show[$x])) {
329 $checked = ' checked="checked"';
330 $curShow[$z] = $Show[$x];
331 } else {
332 $checked = '';
335 <td align="center" bgcolor="<?php echo $cfg['BgcolorOne']; ?>">
336 <input type="checkbox" name="Show[<?php echo $z; ?>]"<?php echo $checked; ?> />
337 </td>
338 <?php
339 $z++;
340 echo "\n";
341 } // end for
343 </tr>
345 <!-- Criteria row -->
346 <tr>
347 <td align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
348 <b><?php echo $strCriteria; ?>&nbsp;:&nbsp;</b>
349 </td>
350 <?php
351 $z = 0;
352 for ($x = 0; $x < $col; $x++) {
353 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
355 <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
356 <input type="text" name="Criteria[<?php echo $z; ?>]" value="" class="textfield" style="width: <?php echo $realwidth; ?>" size="20" />
357 </td>
358 <?php
359 $z++;
360 } // end if
361 echo "\n";
363 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
364 continue;
366 if (isset($Criteria[$x])) {
367 if (get_magic_quotes_gpc()) {
368 $stripped_Criteria = stripslashes($Criteria[$x]);
369 } else {
370 $stripped_Criteria = $Criteria[$x];
373 if ((empty($prev_Criteria) || !isset($prev_Criteria[$x]))
374 || urldecode($prev_Criteria[$x]) != htmlspecialchars($stripped_Criteria)) {
375 $curCriteria[$z] = $stripped_Criteria;
376 $encoded_Criteria = urlencode($stripped_Criteria);
377 } else {
378 $curCriteria[$z] = urldecode($prev_Criteria[$x]);
379 $encoded_Criteria = $prev_Criteria[$x];
382 <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
383 <input type="hidden" name="prev_Criteria[<?php echo $z; ?>]" value="<?php echo $encoded_Criteria; ?>" />
384 <input type="text" name="Criteria[<?php echo $z; ?>]" value="<?php echo htmlspecialchars($stripped_Criteria); ?>" class="textfield" style="width: <?php echo $realwidth; ?>" size="20" />
385 </td>
386 <?php
387 $z++;
388 echo "\n";
389 } // end for
391 </tr>
393 <!-- And/Or columns and rows -->
394 <?php
395 $w = 0;
396 for ($y = 0; $y <= $row; $y++) {
397 $bgcolor = ($y % 2) ? $cfg['BgcolorOne'] : $cfg['BgcolorTwo'];
398 if (isset($InsRow[$y]) && $InsRow[$y] == 'on') {
399 $chk['or'] = ' checked="checked"';
400 $chk['and'] = '';
402 <tr>
403 <td align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $bgcolor; ?>" nowrap="nowrap">
404 <!-- Row controls -->
405 <table bgcolor="<?php echo $bgcolor; ?>">
406 <tr>
407 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
408 <small><?php echo $strQBEIns; ?>&nbsp;:</small>
409 <input type="checkbox" name="InsRow[<?php echo $w; ?>]" />
410 </td>
411 <td align="<?php echo $cell_align_right; ?>">
412 <b><?php echo $strAnd; ?>&nbsp;:</b>
413 </td>
414 <td>
415 <input type="radio" name="AndOrRow[<?php echo $w; ?>]" value="and"<?php echo $chk['and']; ?> />
416 &nbsp;
417 </td>
418 </tr>
419 <tr>
420 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
421 <small><?php echo $strQBEDel; ?>&nbsp;:</small>
422 <input type="checkbox" name="DelRow[<?php echo $w; ?>]" />
423 </td>
424 <td align="<?php echo $cell_align_right; ?>">
425 <b><?php echo $strOr; ?>&nbsp;:</b>
426 </td>
427 <td>
428 <input type="radio" name="AndOrRow[<?php echo $w; ?>]" value="or"<?php echo $chk['or']; ?> />
429 &nbsp;
430 </td>
431 </tr>
432 </table>
433 </td>
434 <?php
435 $z = 0;
436 for ($x = 0; $x < $col; $x++) {
437 if ($InsCol[$x] == 'on') {
438 echo "\n";
439 $or = 'Or' . $w . '[' . $z . ']';
441 <td align="center" bgcolor="<?php echo $bgcolor; ?>">
442 <textarea cols="20" rows="2" style="width: <?php echo $realwidth; ?>" name="<?php echo $or; ?>"></textarea>
443 </td>
444 <?php
445 $z++;
446 } // end if
447 if ($DelCol[$x] == 'on') {
448 continue;
451 echo "\n";
452 $or = 'Or' . $w . '[' . $z . ']';
454 <td align="center" bgcolor="<?php echo $bgcolor; ?>">
455 <textarea cols="20" rows="2" style="width: <?php echo $realwidth; ?>" name="<?php echo $or; ?>"></textarea>
456 </td>
457 <?php
458 $z++;
459 } // end for
460 $w++;
461 echo "\n";
463 </tr>
464 <?php
465 } // end if
467 if (isset($DelRow[$y]) && $DelRow[$y] == 'on') {
468 continue;
471 if (isset($AndOrRow[$y])) {
472 $curAndOrRow[$w] = $AndOrRow[$y];
474 if (isset($AndOrRow[$y]) && $AndOrRow[$y] == 'and') {
475 $chk['and'] = ' checked="checked"';
476 $chk['or'] = '';
477 } else {
478 $chk['or'] = ' checked="checked"';
479 $chk['and'] = '';
481 echo "\n";
483 <tr>
484 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
485 <!-- Row controls -->
486 <table bgcolor="<?php echo $bgcolor; ?>">
487 <tr>
488 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
489 <small><?php echo $strQBEIns; ?>&nbsp;:</small>
490 <input type="checkbox" name="InsRow[<?php echo $w; ?>]" />
491 </td>
492 <td align="<?php echo $cell_align_right; ?>">
493 <b><?php echo $strAnd; ?>&nbsp;:</b>
494 </td>
495 <td>
496 <input type="radio" name="AndOrRow[<?php echo $w; ?>]" value="and"<?php echo $chk['and']; ?> />
497 </td>
498 </tr>
499 <tr>
500 <td align="<?php echo $cell_align_right; ?>" nowrap="nowrap">
501 <small><?php echo $strQBEDel; ?>&nbsp;:</small>
502 <input type="checkbox" name="DelRow[<?php echo $w; ?>]" />
503 </td>
504 <td align="<?php echo $cell_align_right; ?>">
505 <b><?php echo $strOr; ?>&nbsp;:</b>
506 </td>
507 <td>
508 <input type="radio" name="AndOrRow[<?php echo $w; ?>]" value="or"<?php echo $chk['or']; ?> />
509 </td>
510 </tr>
511 </table>
512 </td>
513 <?php
514 $z = 0;
515 for ($x = 0; $x < $col; $x++) {
516 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
517 echo "\n";
518 $or = 'Or' . $w . '[' . $z . ']';
520 <td align="center" bgcolor="<?php echo $bgcolor; ?>">
521 <textarea cols="20" rows="2" style="width: <?php echo $realwidth; ?>" name="<?php echo $or; ?>"></textarea>
522 </td>
523 <?php
524 $z++;
525 } // end if
526 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
527 continue;
530 echo "\n";
531 $or = 'Or' . $y;
532 if (!isset(${$or})) {
533 ${$or} = '';
535 if (!empty(${$or}) && isset(${$or}[$x])) {
536 if (get_magic_quotes_gpc()) {
537 $stripped_or = stripslashes(${$or}[$x]);
538 } else {
539 $stripped_or = ${$or}[$x];
541 } else {
542 $stripped_or = '';
545 <td align="center" bgcolor="<?php echo $bgcolor; ?>">
546 <textarea cols="20" rows="2" style="width: <?php echo $realwidth; ?>" name="Or<?php echo $w . '[' . $z . ']'; ?>"><?php echo htmlspecialchars($stripped_or); ?></textarea>
547 </td>
548 <?php
549 if (!empty(${$or}) && isset(${$or}[$x])) {
550 ${'cur' . $or}[$z] = ${$or}[$x];
552 $z++;
553 } // end for
554 $w++;
555 echo "\n";
557 </tr>
558 <?php
559 echo "\n";
560 } // end for
563 <!-- Modify columns -->
564 <tr>
565 <td align="<?php echo $cell_align_right; ?>" bgcolor="<?php echo $cfg['ThBgcolor']; ?>">
566 <b><?php echo $strModify; ?>&nbsp;:&nbsp;</b>
567 </td>
568 <?php
569 $z = 0;
570 for ($x = 0; $x < $col; $x++) {
571 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
572 $curAndOrCol[$z] = $AndOrCol[$y];
573 if ($AndOrCol[$z] == 'or') {
574 $chk['or'] = ' checked="checked"';
575 $chk['and'] = '';
576 } else {
577 $chk['and'] = ' checked="checked"';
578 $chk['or'] = '';
581 <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
582 <b><?php echo $strOr; ?>&nbsp;:</b>
583 <input type="radio" name="AndOrCol[<?php echo $z; ?>]" value="or"<?php echo $chk['or']; ?> />
584 &nbsp;&nbsp;<b><?php echo $strAnd; ?>&nbsp;:</b>
585 <input type="radio" name="AndOrCol[<?php echo $z; ?>]" value="and"<?php echo $chk['and']; ?> />
586 <br />
587 <?php echo $strQBEIns . "\n"; ?>
588 <input type="checkbox" name="InsCol[<?php echo $z; ?>]" />
589 &nbsp;&nbsp;<?php echo $strQBEDel . "\n"; ?>
590 <input type="checkbox" name="DelCol[<?php echo $z; ?>]" />
591 </td>
592 <?php
593 $z++;
594 } // end if
595 echo "\n";
597 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
598 continue;
601 if (isset($AndOrCol[$y])) {
602 $curAndOrCol[$z] = $AndOrCol[$y];
604 if (isset($AndOrCol[$z]) && $AndOrCol[$z] == 'or') {
605 $chk['or'] = ' checked="checked"';
606 $chk['and'] = '';
607 } else {
608 $chk['and'] = ' checked="checked"';
609 $chk['or'] = '';
612 <td align="center" bgcolor="<?php echo $cfg['BgcolorTwo']; ?>">
613 <b><?php echo $strOr; ?>&nbsp;:</b>
614 <input type="radio" name="AndOrCol[<?php echo $z; ?>]" value="or"<?php echo $chk['or']; ?> />
615 &nbsp;&nbsp;<b><?php echo $strAnd; ?>&nbsp;:</b>
616 <input type="radio" name="AndOrCol[<?php echo $z; ?>]" value="and"<?php echo $chk['and']; ?> />
617 <br />
618 <?php echo $strQBEIns . "\n"; ?>
619 <input type="checkbox" name="InsCol[<?php echo $z; ?>]" />
620 &nbsp;&nbsp;<?php echo $strQBEDel . "\n"; ?>
621 <input type="checkbox" name="DelCol[<?php echo $z; ?>]" />
622 </td>
623 <?php
624 $z++;
625 echo "\n";
626 } // end for
628 </tr>
629 </table>
632 <!-- Other controls -->
633 <table border="0">
634 <tr>
635 <td valign="top">
636 <table border="0" align="<?php echo $cell_align_left; ?>">
637 <tr>
638 <td rowspan="4" valign="top">
639 <?php echo $strUseTables; ?>&nbsp;:
640 <br />
641 <select name="TableList[]" size="7" multiple="multiple">
642 <?php
643 while (list($key, $val) = each($tbl_names)) {
644 echo ' ';
645 echo '<option value="' . urlencode($key) . '"' . $val . '>' . htmlspecialchars($key) . '</option>' . "\n";
648 </select>
649 </td>
650 <td align="<?php echo $cell_align_right; ?>" valign="bottom">
651 <input type="hidden" value="<?php echo $db; ?>" name="db" />
652 <input type="hidden" value="<?php echo $z; ?>" name="Columns" />
653 <?php
654 $w--;
656 <input type="hidden" value="<?php echo $w; ?>" name="Rows" />
657 <?php echo $strAddDeleteRow; ?>&nbsp;:
658 <select size="1" name="Add_Row">
659 <option value="-3">-3</option>
660 <option value="-2">-2</option>
661 <option value="-1">-1</option>
662 <option value="0" selected="selected">0</option>
663 <option value="1">1</option>
664 <option value="2">2</option>
665 <option value="3">3</option>
666 </select>
667 </td>
668 </tr>
669 <tr>
670 <td align="<?php echo $cell_align_right; ?>" valign="bottom">
671 <?php echo $strAddDeleteColumn; ?>&nbsp;:
672 <select size="1" name="Add_Col">
673 <option value="-3">-3</option>
674 <option value="-2">-2</option>
675 <option value="-1">-1</option>
676 <option value="0" selected="selected">0</option>
677 <option value="1">1</option>
678 <option value="2">2</option>
679 <option value="3">3</option>
680 </select>
681 </td>
682 </tr>
683 <!-- Generates a query -->
684 <tr align="center" valign="top">
685 <td>
686 <input type="submit" name="modify" value="<?php echo $strUpdateQuery; ?>" />
687 <input type="hidden" name="server" value="<?php echo $server; ?>" />
688 <input type="hidden" name="lang" value="<?php echo $lang; ?>" />
689 <input type="hidden" name="convcharset" value="<?php echo $convcharset; ?>" />
690 </td>
691 </tr>
692 <!-- Executes a query -->
693 <tr align="center" valign="top">
694 <td>
695 <input type="submit" name="submit_sql" value="<?php echo $strRunQuery; ?>" />
696 </td>
697 </tr>
698 </table>
699 </td>
700 <td>
701 <!-- Displays the current query -->
702 <?php echo sprintf($strQueryOnDb, htmlspecialchars($db)); ?><br />
703 <textarea cols="30" rows="7" name="sql_query">
704 <?php
705 // 1. SELECT
706 $last_select = 0;
707 $encoded_qry = '';
708 if (!isset($qry_select)) {
709 $qry_select = '';
711 for ($x = 0; $x < $col; $x++) {
712 if (!empty($curField[$x]) && isset($curShow[$x]) && $curShow[$x] == 'on') {
713 if ($last_select) {
714 $qry_select .= ', ';
716 $qry_select .= $curField[$x];
717 $last_select = 1;
719 } // end for
720 if (!empty($qry_select)) {
721 $encoded_qry .= urlencode('SELECT ' . $qry_select . "\n");
722 echo 'SELECT ' . htmlspecialchars($qry_select) . "\n";
725 // 2. FROM
727 // Create LEFT JOINS out of Relations
728 // Code originally by Mike Beck <mike.beck@ibmiller.de>
729 // If we can use Relations we could make some left joins
730 // First find out if relations are available in this database
732 // First we need the really needed Tables - those in TableList might
733 // still be all Tables.
734 if (isset($Field) && count($Field) > 0) {
736 // Initialize some variables
737 $alltabs = array();
738 $where = array();
739 $wheretabs = array();
740 $reltabs = array();
741 $rel = array();
742 $rest = array();
743 $found = array();
745 // we only start this if we have fields, otherwise it would be dumb
746 while (list(, $value) = each($Field)) {
747 $parts = explode('.', $value);
748 if (urldecode($parts[0]) != '') {
749 $alltabs[] = substr(urldecode($parts[0]), 1, strlen(urldecode($parts[0])) - 2);
751 } // end while
753 if ($cfgRelation['relwork'] && count($alltabs) > 0) {
755 // now we need all tables that we have in the whereclause
756 for ($x = 0; $x < count($Criteria); $x++) {
757 $wtable = explode('.', urldecode($Field[$x]));
758 $ctable = str_replace('`', '', $wtable[0]);
759 if (!empty($Field[$x]) && !empty($Criteria[$x])) {
760 if (empty($where[$ctable]) || $where[$ctable] != '=') {
761 $where[$ctable] = substr($Criteria[$x], 0, 1);
764 } // end for
766 if (count($where) > 1) {
767 // if we have enough whereclauses then we want those that have
768 // a '=' (not for example 'like')
769 while (list($key, $value) = each($where)) {
770 if ($value == '=') {
771 $wheretabs[] = $key;
774 } // end if
777 // if there was nothing starting with '=' we have to use all we got in
778 // the first place (in this case we can use & here - if it works with
779 // old versions of php ??)
780 if (count($wheretabs) == 0) {
781 $wheretabs = $where;
783 // I expect it will make sense to have the table which is most often
784 // found as foreign_table as the one to start our FROM...
785 // but we have to make sure that if there is a WHERE clause then we
786 // have a master out of those that are used there
788 // We will need this a few times:
789 $incrit = '(\'' . implode('\', \'', $alltabs) . '\')';
791 $rel_query = 'SELECT master_table AS wer, COUNT(foreign_table) AS hits'
792 . ' FROM ' . PMA_backquote($cfgRelation['relation'])
793 . ' WHERE master_db = \'' . $db . '\''
794 . ' AND foreign_db = \'' . $db . '\''
795 . ' AND master_table IN ' . $incrit
796 . ' AND foreign_table IN ' . $incrit;
797 if (!empty($column)) {
798 $rel_query .= ' AND master_field = \'' . $column . '\'';
800 $rel_query .= ' GROUP BY master_table ORDER BY hits DESC';
802 $relations = PMA_query_as_cu($rel_query);
803 while ($row = PMA_mysql_fetch_array($relations)) {
804 // we want the first one (highest number of hits) or the first one
805 // that is in the WHERE clause
806 if (!isset($master)) {
807 $master = $row['wer'];
809 while (list($key, $value) = each($wheretabs)) {
810 if ($row['wer'] == $key) {
811 $master = $row['wer'];
812 break;
814 } // end while
815 reset($wheretabs);
816 } // end while
818 if (isset ($master) && $master != '') {
819 $qry_from = PMA_backquote($master);
821 // now we want one Array that has all tablenames but master
822 while (list(, $value) = each($alltabs)) {
823 if ($value != $master) {
824 $reltabs[] = $value;
825 $rel[$value]['mcon'] = 0;
827 } // end while
829 // now we only use everything but the first table
830 $incrit_s = '(\'' . implode('\', \'', $reltabs) . '\')';
831 $rel_query = 'SELECT *'
832 . ' FROM ' . PMA_backquote($cfgRelation['relation'])
833 . ' WHERE master_db = \'' . $db . '\''
834 . ' AND foreign_db = \'' . $db . '\''
835 . ' AND master_table IN ' . $incrit
836 . ' AND foreign_table IN ' . $incrit_s;
837 if (!empty($column)) {
838 $rel_query .= ' AND master_field = \'' . $column . '\'';
840 $rel_query .= ' ORDER BY foreign_table, master_table';
842 if (isset($dbh)) {
843 PMA_mysql_select_db($cfgRelation['db'], $dbh);
844 $relations = @PMA_mysql_query($rel_query, $dbh) or PMA_mysqlDie(PMA_mysql_error($dbh), $rel_query, '', $err_url_0);
845 PMA_mysql_select_db($db, $dbh);
846 } else {
847 PMA_mysql_select_db($cfgRelation['db']);
848 $relations = @PMA_mysql_query($rel_query) or PMA_mysqlDie('', $rel_query, '', $err_url_0);
849 PMA_mysql_select_db($db);
852 while ($row = PMA_mysql_fetch_array($relations)) {
853 $foreign_table = $row['foreign_table'];
854 if ($rel[$foreign_table]['mcon'] == 0) {
855 // if we already found a link to the master table we don't
856 // want another otherwise we take whatever we get
857 $rel[$foreign_table]['link'] = ' LEFT JOIN ' . PMA_backquote($foreign_table)
858 . ' ON ' . PMA_backquote($row['master_table']) . '.' . PMA_backquote($row['master_field'])
859 . ' = ' . PMA_backquote($row['foreign_table']) . '.' . PMA_backquote($row['foreign_field']);
861 if ($row['master_table'] == $master) {
862 $rel[$foreign_table]['mcon'] = 1;
864 } // end while
865 // possibly we still don't have all - there might be some that are
866 // only found as a foreign_table in relation to one of those that we
867 // already have
868 if ($master != '') {
869 $found[] = $master;
870 $qry_from = PMA_backquote($master);
871 } // end if
873 while (list($key, $varr) = each($rel)) {
874 if (!isset($varr['link']) || $varr['link'] == '') {
875 $rest[] = $key;
876 } else {
877 $found[] = $key;
879 } // end while
880 if (count($rest) > 0) {
881 $incrit_d = '(\'' . implode('\', \'', $found) . '\')';
882 $incrit_s = '(\'' . implode('\', \'', $rest) . '\')';
884 $rel_query = 'SELECT *'
885 . ' FROM ' . PMA_backquote($cfgRelation['relation'])
886 . ' WHERE master_db = \'' . $db . '\''
887 . ' AND foreign_db = \'' . $db . '\''
888 . ' AND master_table IN ' . $incrit_s
889 . ' AND foreign_table IN ' . $incrit_d;
890 if (!empty($column)) {
891 $rel_query .= ' AND master_field = \'' . $column . '\'';
893 $rel_query .= ' ORDER BY master_table, foreign_table';
894 if (isset($dbh)) {
895 PMA_mysql_select_db($cfgRelation['db'], $dbh);
896 $relations = @PMA_mysql_query($rel_query, $dbh) or PMA_mysqlDie(PMA_mysql_error($dbh), $rel_query, '', $err_url_0);
897 PMA_mysql_select_db($db, $dbh);
898 } else {
899 PMA_mysql_select_db($cfgRelation['db']);
900 $relations = @PMA_mysql_query($rel_query) or PMA_mysqlDie('', $rel_query, '', $err_url_0);
901 PMA_mysql_select_db($db);
904 while ($row = PMA_mysql_fetch_array($relations)) {
905 $found_table = $row['master_table'];
906 if ($rel[$found_table]['mcon'] == 0) {
907 // if we allready found a link to the mastertable we
908 // don't want another otherwise we take whatever we get
909 $rel[$found_table]['link'] = ' LEFT JOIN ' . $found_table
910 . ' ON ' . PMA_backquote($row['master_table']) . '.' . PMA_backquote($row['master_field'])
911 . ' = ' . PMA_backquote($row['foreign_table']) . '.' . PMA_backquote($row['foreign_field']);
913 // in extreme cases we hadn't found a master yet, so
914 // let's use the one we found now
915 if ($master == '') {
916 $master = $row['master_table'];
919 if ($row["foreign_table"] == $master) {
920 $rel[$found_table]['mcon'] = 1;
922 } // end while
923 } // end if
925 // now let's see what we found - every table that doesn't have a
926 // link gets added directly to the FROM the links go to a second
927 // variable $lj which is added afterwards
928 $lj = '';
929 $ljm = '';
930 reset($rel);
931 while (list($key, $varr) = each($rel)) {
932 if ($varr['link'] == '') {
933 if ($qry_from != '') {
934 $qry_from .= ', ';
936 $qry_from .= PMA_backquote($key);
937 // debug echo "add ". $key . ": " . PMA_backquote($key) . "\n";
938 } else if ($varr['mcon'] == 0) {
939 // those that have no link with the mastertable we will
940 // show at the end
941 $lj .= $varr['link'];
942 } else {
943 $ljm .= $varr['link'];
945 } // end while
947 // on one occasion i had qry_from at this point end with a , as I
948 // can't find why this happened i check this now:
949 $qry_from = ereg_replace(', $', '', $qry_from);
950 $qry_from .= $ljm . $lj;
951 } // end if ($master != '')
952 } // end rel work and $alltabs > 0
954 if (empty($qry_from) && count($alltabs)) {
955 // there might be more than one mentioning of the table in here
956 // as array_unique is only PHP4 we have to do this by hand
957 $temp = array();
958 while (list($k, $v) = each($alltabs)) {
959 $temp[$v] = 1;
961 unset($alltabs);
962 $alltabs = array();
963 while (list($k, $v) = each($temp)) {
964 $alltabs[] = $k;
966 $qry_from = implode(', ', PMA_backquote($alltabs));
969 } // end count($Field) > 0
971 if (!empty($qry_from)) {
972 $encoded_qry .= urlencode('FROM ' . $qry_from . "\n");
973 echo 'FROM ' . htmlspecialchars($qry_from) . "\n";
976 // 3. WHERE
977 $qry_where = '';
978 $criteria_cnt = 0;
979 for ($x = 0; $x < $col; $x++) {
980 if (!empty($curField[$x]) && !empty($curCriteria[$x]) && $x && isset($last_where)) {
981 $qry_where .= ' ' . strtoupper($curAndOrCol[$last_where]) . ' ';
983 if (!empty($curField[$x]) && !empty($curCriteria[$x])) {
984 $qry_where .= '(' . $curField[$x] . ' ' . $curCriteria[$x] . ')';
985 $last_where = $x;
986 $criteria_cnt++;
988 } // end for
989 if ($criteria_cnt > 1) {
990 $qry_where = '(' . $qry_where . ')';
993 // OR rows ${"cur".$or}[$x]
994 if (!isset($curAndOrRow)) {
995 $curAndOrRow = array();
997 for ($y = 0; $y <= $row; $y++) {
998 $criteria_cnt = 0;
999 $qry_orwhere = '';
1000 $last_orwhere = '';
1001 for ($x = 0; $x < $col; $x++) {
1002 if (!empty($curField[$x]) && !empty(${'curOr' . $y}[$x]) && $x) {
1003 $qry_orwhere .= ' ' . strtoupper($curAndOrCol[$last_orwhere]) . ' ';
1005 if (!empty($curField[$x]) && !empty(${'curOr' . $y}[$x])) {
1006 $qry_orwhere .= '(' . $curField[$x]
1007 . ' '
1008 . (get_magic_quotes_gpc() ? stripslashes(${'curOr' . $y}[$x]) : ${'curOr' . $y}[$x])
1009 . ')';
1010 $last_orwhere = $x;
1011 $criteria_cnt++;
1013 } // end for
1014 if ($criteria_cnt > 1) {
1015 $qry_orwhere = '(' . $qry_orwhere . ')';
1017 if (!empty($qry_orwhere)) {
1018 $qry_where .= "\n"
1019 . strtoupper(isset($curAndOrRow[$y]) ? $curAndOrRow[$y] . ' ' : '')
1020 . $qry_orwhere;
1021 } // end if
1022 } // end for
1024 if (!empty($qry_where) && $qry_where != '()') {
1025 $encoded_qry .= urlencode('WHERE ' . $qry_where . "\n");
1026 echo 'WHERE ' . htmlspecialchars($qry_where) . "\n";
1027 } // end if
1029 // 4. ORDER BY
1030 $last_orderby = 0;
1031 if (!isset($qry_orderby)) {
1032 $qry_orderby = '';
1034 for ($x = 0; $x < $col; $x++) {
1035 if ($last_orderby && $x && !empty($curField[$x]) && !empty($curSort[$x])) {
1036 $qry_orderby .= ', ';
1038 if (!empty($curField[$x]) && !empty($curSort[$x])) {
1039 // if they have chosen all fields using the * selector,
1040 // then sorting is not available
1041 // Robbat2 - Fix for Bug #570698
1042 if (substr($curField[$x], -2) != '.*') {
1043 $qry_orderby .= $curField[$x] . ' ' . $curSort[$x];
1044 $last_orderby = 1;
1047 } // end for
1048 if (!empty($qry_orderby)) {
1049 $encoded_qry .= urlencode('ORDER BY ' . $qry_orderby);
1050 echo 'ORDER BY ' . htmlspecialchars($qry_orderby) . "\n";
1053 </textarea>
1054 <input type="hidden" name="encoded_sql_query" value="<?php echo $encoded_qry; ?>" />
1055 </td>
1056 </tr>
1057 </table>
1059 </form>
1062 <?php
1064 * Displays the footer
1066 require('./footer.inc.php3');