6 * Get the values of the variables posted or sent to this script and display
9 require('./libraries/grab_globals.lib.php3');
10 require('./libraries/common.lib.php3');
11 require('./libraries/relation.lib.php3');
15 * Gets the relation settings
17 $cfgRelation = PMA_getRelationsParam();
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');
34 include('./db_details_common.php3');
35 $url_query .= '&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>';
45 * Initialize some variables
47 if (empty($Columns)) {
48 $Columns = 3; // Initial number of columns
50 if (!isset($Add_Col)) {
53 if (!isset($Add_Row)) {
59 if (!isset($InsCol)) {
62 if (!isset($DelCol)) {
65 if (!isset($prev_Criteria)) {
68 // workaround for a PHP3 problem
69 if (!isset($Criteria)) {
72 for ($i = 0; $i < $Columns; $i++
) {
76 if (!isset($InsRow)) {
79 for ($i = 0; $i < $Columns; $i++
) {
83 if (!isset($DelRow)) {
86 for ($i = 0; $i < $Columns; $i++
) {
90 if (!isset($AndOrRow)) {
93 for ($i = 0; $i < $Columns; $i++
) {
97 if (!isset($AndOrCol)) {
100 for ($i = 0; $i < $Columns; $i++
) {
106 $col = $Columns +
$Add_Col;
110 $row = $Rows +
$Add_Row;
119 $tbl_result = PMA_mysql_list_tables($db);
120 $tbl_result_cnt = mysql_num_rows($tbl_result);
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"';
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;
138 if (empty($tbl_names[$tbl]) && !empty($TableList)) {
139 $tbl_names[$tbl] = '';
141 $tbl_names[$tbl] = ' selected="selected"';
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]);
161 mysql_free_result($fld_results);
166 mysql_free_result($tbl_result);
168 // largest width found
169 $realwidth = $wid . 'ex';
177 <!-- Query by example form
-->
178 <form action
="db_details_qbe.php3" method
="post">
179 <table border
="<?php echo $cfg['Border']; ?>">
183 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
184 <b
><?php
echo $strField; ?
> 
;: 
;</b
>
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
>
196 for ($y = 0; $y < sizeof($fld); $y++
) {
197 if ($fld[$y] == '') {
198 $sel = ' selected="selected"';
203 echo '<option value="' . urlencode($fld[$y]) . '"' . $sel . '>' . htmlspecialchars($fld[$y]) . '</option>' . "\n";
213 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
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
>
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"';
230 echo '<option value="' . urlencode($fld[$y]) . '"' . $sel . '>' . htmlspecialchars($fld[$y]) . '</option>' . "\n";
244 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
245 <b
><?php
echo $strSort; ?
> 
;: 
;</b
>
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
>
264 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
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
>
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) == '.*')) {
281 if (isset($Sort[$x]) && $Sort[$x] == 'ASC') {
282 $curSort[$z] = $Sort[$x];
283 $sel = ' selected="selected"';
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"';
296 echo '<option value="DESC"' . $sel . '>' . $strDescending . '</option>' . "\n";
309 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
310 <b
><?php
echo $strShow; ?
> 
;: 
;</b
>
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; ?>]" />
325 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
328 if (isset($Show[$x])) {
329 $checked = ' checked="checked"';
330 $curShow[$z] = $Show[$x];
335 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorOne']; ?>">
336 <input type
="checkbox" name
="Show[<?php echo $z; ?>]"<?php
echo $checked; ?
> />
345 <!-- Criteria row
-->
347 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
348 <b
><?php
echo $strCriteria; ?
> 
;: 
;</b
>
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" />
363 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
366 if (isset($Criteria[$x])) {
367 if (get_magic_quotes_gpc()) {
368 $stripped_Criteria = stripslashes($Criteria[$x]);
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);
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" />
393 <!-- And/Or columns
and rows
-->
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"';
403 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $bgcolor; ?>" nowrap
="nowrap">
404 <!-- Row controls
-->
405 <table bgcolor
="<?php echo $bgcolor; ?>">
407 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
408 <small
><?php
echo $strQBEIns; ?
> 
;:</small
>
409 <input type
="checkbox" name
="InsRow[<?php echo $w; ?>]" />
411 <td align
="<?php echo $cell_align_right; ?>">
412 <b
><?php
echo $strAnd; ?
> 
;:</b
>
415 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
420 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
421 <small
><?php
echo $strQBEDel; ?
> 
;:</small
>
422 <input type
="checkbox" name
="DelRow[<?php echo $w; ?>]" />
424 <td align
="<?php echo $cell_align_right; ?>">
425 <b
><?php
echo $strOr; ?
> 
;:</b
>
428 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
436 for ($x = 0; $x < $col; $x++
) {
437 if ($InsCol[$x] == 'on') {
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; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
447 if ($DelCol[$x] == 'on') {
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; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
467 if (isset($DelRow[$y]) && $DelRow[$y] == 'on') {
471 if (isset($AndOrRow[$y])) {
472 $curAndOrRow[$w] = $AndOrRow[$y];
474 if (isset($AndOrRow[$y]) && $AndOrRow[$y] == 'and') {
475 $chk['and'] = ' checked="checked"';
478 $chk['or'] = ' checked="checked"';
484 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
485 <!-- Row controls
-->
486 <table bgcolor
="<?php echo $bgcolor; ?>">
488 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
489 <small
><?php
echo $strQBEIns; ?
> 
;:</small
>
490 <input type
="checkbox" name
="InsRow[<?php echo $w; ?>]" />
492 <td align
="<?php echo $cell_align_right; ?>">
493 <b
><?php
echo $strAnd; ?
> 
;:</b
>
496 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
500 <td align
="<?php echo $cell_align_right; ?>" nowrap
="nowrap">
501 <small
><?php
echo $strQBEDel; ?
> 
;:</small
>
502 <input type
="checkbox" name
="DelRow[<?php echo $w; ?>]" />
504 <td align
="<?php echo $cell_align_right; ?>">
505 <b
><?php
echo $strOr; ?
> 
;:</b
>
508 <input type
="radio" name
="AndOrRow[<?php echo $w; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
515 for ($x = 0; $x < $col; $x++
) {
516 if (!empty($InsCol) && isset($InsCol[$x]) && $InsCol[$x] == 'on') {
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; ?>" dir
="<?php echo $text_dir; ?>"></textarea
>
526 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
532 if (!isset($
{$or})) {
535 if (!empty($
{$or}) && isset($
{$or}[$x])) {
536 if (get_magic_quotes_gpc()) {
537 $stripped_or = stripslashes($
{$or}[$x]);
539 $stripped_or = $
{$or}[$x];
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 . ']'; ?>" dir
="<?php echo $text_dir; ?>"><?php
echo htmlspecialchars($stripped_or); ?
></textarea
>
549 if (!empty($
{$or}) && isset($
{$or}[$x])) {
550 $
{'cur' . $or}[$z] = $
{$or}[$x];
563 <!-- Modify columns
-->
565 <td align
="<?php echo $cell_align_right; ?>" bgcolor
="<?php echo $cfg['ThBgcolor']; ?>">
566 <b
><?php
echo $strModify; ?
> 
;: 
;</b
>
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"';
577 $chk['and'] = ' checked="checked"';
581 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
582 <b
><?php
echo $strOr; ?
> 
;:</b
>
583 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
584  
; 
;<b
><?php
echo $strAnd; ?
> 
;:</b
>
585 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
587 <?php
echo $strQBEIns . "\n"; ?
>
588 <input type
="checkbox" name
="InsCol[<?php echo $z; ?>]" />
589  
; 
;<?php
echo $strQBEDel . "\n"; ?
>
590 <input type
="checkbox" name
="DelCol[<?php echo $z; ?>]" />
597 if (!empty($DelCol) && isset($DelCol[$x]) && $DelCol[$x] == 'on') {
601 if (isset($AndOrCol[$y])) {
602 $curAndOrCol[$z] = $AndOrCol[$y];
604 if (isset($AndOrCol[$z]) && $AndOrCol[$z] == 'or') {
605 $chk['or'] = ' checked="checked"';
608 $chk['and'] = ' checked="checked"';
612 <td align
="center" bgcolor
="<?php echo $cfg['BgcolorTwo']; ?>">
613 <b
><?php
echo $strOr; ?
> 
;:</b
>
614 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="or"<?php
echo $chk['or']; ?
> />
615  
; 
;<b
><?php
echo $strAnd; ?
> 
;:</b
>
616 <input type
="radio" name
="AndOrCol[<?php echo $z; ?>]" value
="and"<?php
echo $chk['and']; ?
> />
618 <?php
echo $strQBEIns . "\n"; ?
>
619 <input type
="checkbox" name
="InsCol[<?php echo $z; ?>]" />
620  
; 
;<?php
echo $strQBEDel . "\n"; ?
>
621 <input type
="checkbox" name
="DelCol[<?php echo $z; ?>]" />
632 <!-- Other controls
-->
636 <table border
="0" align
="<?php echo $cell_align_left; ?>">
638 <td rowspan
="4" valign
="top">
639 <?php
echo $strUseTables; ?
> 
;:
641 <select name
="TableList[]" size
="7" multiple
="multiple">
643 while (list($key, $val) = each($tbl_names)) {
645 echo '<option value="' . urlencode($key) . '"' . $val . '>' . htmlspecialchars($key) . '</option>' . "\n";
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" />
656 <input type
="hidden" value
="<?php echo $w; ?>" name
="Rows" />
657 <?php
echo $strAddDeleteRow; ?
> 
;:
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
>
670 <td align
="<?php echo $cell_align_right; ?>" valign
="bottom">
671 <?php
echo $strAddDeleteColumn; ?
> 
;:
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
>
683 <!-- Generates a query
-->
684 <tr align
="center" valign
="top">
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; ?>" />
692 <!-- Executes a query
-->
693 <tr align
="center" valign
="top">
695 <input type
="submit" name
="submit_sql" value
="<?php echo $strRunQuery; ?>" />
701 <!-- Displays the current query
-->
702 <?php
echo sprintf($strQueryOnDb, htmlspecialchars($db)); ?
><br
/>
703 <textarea cols
="30" rows
="7" name
="sql_query" dir
="<?php echo $text_dir; ?>">
708 if (!isset($qry_select)) {
711 for ($x = 0; $x < $col; $x++
) {
712 if (!empty($curField[$x]) && isset($curShow[$x]) && $curShow[$x] == 'on') {
716 $qry_select .= $curField[$x];
720 if (!empty($qry_select)) {
721 $encoded_qry .= urlencode('SELECT ' . $qry_select . "\n");
722 echo 'SELECT ' . htmlspecialchars($qry_select) . "\n";
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.
733 //echo '</textarea><pre style="background-color: white;">';
734 // First we need the really needed Tables - those in TableList might still be
736 if (isset($Field) && count($Field) > 0) {
738 // Initialize some variables
744 $col_where = array();
747 // We only start this if we have fields, otherwise it would be dumb
748 // echo "get everything\n";
749 while (list(, $value) = each($Field)) {
750 $parts = explode('.', $value);
751 if (!empty($parts[0]) && !empty($parts[1])) {
752 $tab_raw = urldecode($parts[0]);
753 $tab = str_replace('`', '', $tab_raw);
754 // echo 'new Tab: ' . $tab . "\n";
755 $tab_all[$tab] = $tab;
756 $col_raw = urldecode($parts[1]);
757 $col_all[] = $tab . '.' . str_replace('`', '', $col_raw);
758 // echo 'new col: ' . $tab . '.' . str_replace('`', '', $col_raw) . "\n";
762 // echo "check where clauses\n";
763 if ($cfgRelation['relwork'] && count($tab_all) > 0) {
764 // Now we need all tables that we have in the where clause
765 $crit_cnt = count($Criteria);
766 for ($x = 0; $x < $crit_cnt; $x++
) {
767 $curr_tab = explode('.', urldecode($Field[$x]));
768 if (!empty($curr_tab[0]) && !empty($curr_tab[1])) {
769 $tab_raw = urldecode($curr_tab[0]);
770 $tab = str_replace('`', '', $tab_raw);
772 $col_raw = urldecode($curr_tab[1]);
773 $col1 = str_replace('`', '', $col_raw);
774 $col1 = $tab . '.' . $col1;
775 // Now we know that our array has the same numbers as $Criteria
776 // we can check which of our columns has a where clause
777 if (!empty($Criteria[$x])) {
778 if (substr($Criteria[$x], 0, 1) == '=' ||
eregi('is', $Criteria[$x])) {
779 $col_where[$col] = $col1;
780 $tab_wher[$tab] = $tab;
781 // echo 'new where clause: ' . $tab_wher[$tab] . "||\n";
787 // Cleans temp vars w/o further use
792 if (count($tab_wher) == 1) {
793 // If there is exactly one column that has a decent where-clause
794 // we will just use this
795 $master = key($tab_wher);
796 // echo 'nur ein where: master = ' .$master . "||\n";
798 // Now let's find out which of the tables has an index
799 // echo "prüfe indexe:\n";
800 while (list(, $tab) = each($tab_all)) {
801 $ind_qry = 'SHOW INDEX FROM ' . PMA_backquote($tab);
802 $ind_rs = PMA_mysql_query($ind_qry);
803 while ($ind = PMA_mysql_fetch_array($ind_rs)) {
804 $col1 = $tab . '.' . $ind['Column_name'];
805 if (isset($col_all[$col1])) {
806 if ($ind['non_unique'] == 0) {
807 if (isset($col_where[$col1])) {
808 $col_unique[$col1] = 'Y';
810 $col_unique[$col1] = 'N';
812 //echo 'neuen unique index gefunden: ' . $col . "\n";
814 if (isset($col_where[$col1])) {
815 $col_index[$col1] = 'Y';
817 $col_index[$col1] = 'N';
819 //echo 'neuen index gefunden: ' . $col . "\n";
822 } // end while (each col of tab)
823 } // end while (each tab)
824 // now we want to find the best.
825 if (isset($col_unique) && count($col_unique) > 0) {
826 $col_cand = $col_unique;
827 //echo "Kandidaten sind jetzt alle mit unique index\n";
829 } else if (isset($col_index) && count($col_index) > 0) {
830 $col_cand = $col_index;
832 //echo "Kandidaten sind jetzt alle mit index\n";
833 } else if (isset($col_where) && count($col_where) > 0) {
834 $col_cand = $tab_wher;
835 //echo "Kandidaten sind jetzt alle im whereclause\n";
838 $col_cand = $tab_all;
840 //echo "Kandidaten sind jetzt alle \n";
843 // If we came up with $col_unique (very good) or $col_index (still
844 // good) as $col_cand we want to check if we have any 'Y' there
845 // (that would mean that they were also found in the whereclauses
846 // which would be great). if yes, we take only those
847 if ($needsort == 1) {
848 while (list($col, $is_where) = each($col_cand)) {
849 $tab = explode('.', $col);
851 if ($is_where == 'Y') {
859 //echo "Kandidaten konnten auf index+where beschränkt werden\n";
862 //echo "keiner der Kandidaten mit Index ist im wherclause\n";
866 // If our array of candidates has more than one member we'll just
867 // find the smallest table.
868 // Of course the actual query would be faster if we check for
869 // the Criteria which gives the smallest result set in its table,
870 // but it would take too much time to check this
871 if (count($col_cand) > 1) {
872 //echo "wir haben immer noch mehr als einen Kandidaten. Prüfe Größe\n";
873 // Of course we only want to check each table once
874 $checked_tables = $col_cand;
875 while (list(, $tab) = each($col_cand)) {
876 if ($checked_tables[$tab] != 1 ) {
877 //echo 'prüfe jetzt: Tabelle ' . $tab . "\n";
878 $rows_qry = 'SELECT COUNT(1) AS anz '
879 . 'FROM ' . PMA_backquote($tab);
880 $rows_rs = PMA_mysql_query($rows_qry);
881 while ($res = PMA_mysql_fetch_array($rows_rs)) {
882 $tsize[$tab] = $res['anz'];
883 //echo "$tab hat: " . $tsize[$tab] . "\n";
885 $checked_tables[$tab] = 1;
887 $csize[$tab] = $tsize[$tab];
888 //echo 'erster csize: ' . $csize[$tab] . "\n";
892 $master = key($csize);
893 //echo 'kleinste Datei: ' . $master . "\n";
895 $master = $col_cand[0];
896 //echo 'master ist der einzige Kandidat: ' . $master . "\n";
898 } // end if (exactly one where clause)
899 //echo 'ich habe mich entschieden: ' . $master;
904 * Removes unwanted entries from an array (PHP3 compliant)
906 * @param array the array to work with
907 * @param array the list of keys to remove
909 * @return array the cleaned up array
913 function PMA_arrayShort($array, $key)
915 while (list($k, $v) = each($array)) {
925 } // end of the "PMA_arrayShort()" function
929 * Finds all related tables
931 * @param string wether to go from master to foreign or vice versa
933 * @return boolean always TRUE
935 * @global array the list of tables that we still couldn't connect
936 * @global array the list of allready connected tables
937 * @global string the current databse name
938 * @global string the super user connection id
939 * @global array the list of relation settings
943 function PMA_getRelatives($from) {
944 global $tab_left, $tab_know, $fromclause;
945 global $dbh, $db, $cfgRelation;
947 if ($from == 'master') {
952 $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
953 $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
955 $rel_query = 'SELECT *'
956 . ' FROM ' . PMA_backquote($cfgRelation['relation'])
957 . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($db) . '\''
958 . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($db) . '\''
959 . ' AND ' . $from . '_table IN ' . $in_know
960 . ' AND ' . $to . '_table IN ' . $in_left;
962 PMA_mysql_select_db($cfgRelation['db'], $dbh);
963 $relations = @PMA_mysql_query
($rel_query, $dbh) or PMA_mysqlDie(PMA_mysql_error($dbh), $rel_query, '', $err_url_0);
964 PMA_mysql_select_db($db, $dbh);
966 PMA_mysql_select_db($cfgRelation['db']);
967 $relations = @PMA_mysql_query
($rel_query) or PMA_mysqlDie('', $rel_query, '', $err_url_0);
968 PMA_mysql_select_db($db);
970 while ($row = PMA_mysql_fetch_array($relations)) {
971 $found_table = $row[$to . '_table'];
972 if (isset($tab_left[$found_table])) {
973 $fromclause .= "\n" . ' LEFT JOIN '
974 . PMA_backquote($row[$to . '_table']) . ' ON '
975 . PMA_backquote($row[$from . '_table']) . '.'
976 . PMA_backquote($row[$from . '_field']) . ' = '
977 . PMA_backquote($row[$to . '_table']) . '.'
978 . PMA_backquote($row[$to . '_field']) . ' ';
979 $tab_know[$found_table] = $found_table;
980 $tab_left = PMA_arrayShort($tab_left, $found_table);
985 } // end of the "PMA_getRelatives()" function
988 $tab_left = PMA_arrayShort($tab_all, $master);
989 $tab_know[$master] = $master;
993 while (count($tab_left) > 0) {
995 PMA_getRelatives('master');
997 PMA_getRelatives('foreign');
1002 while (list(, $tab) = each($tab_left)) {
1003 $emerg .= ', ' . $tab;
1004 $tab_left = PMA_arrayShort($tab_left, $tab);
1008 $qry_from = $master . $emerg . $fromclause;
1009 } // end if ($cfgRelation['relwork'] && count($tab_all) > 0)
1011 } // end count($Field) > 0
1013 // Now let's see what we got
1014 if (!empty($qry_from)) {
1015 $encoded_qry .= urlencode('FROM ' . $qry_from . "\n");
1016 echo 'FROM ' . htmlspecialchars($qry_from) . "\n";
1022 for ($x = 0; $x < $col; $x++
) {
1023 if (!empty($curField[$x]) && !empty($curCriteria[$x]) && $x && isset($last_where)) {
1024 $qry_where .= ' ' . strtoupper($curAndOrCol[$last_where]) . ' ';
1026 if (!empty($curField[$x]) && !empty($curCriteria[$x])) {
1027 $qry_where .= '(' . $curField[$x] . ' ' . $curCriteria[$x] . ')';
1032 if ($criteria_cnt > 1) {
1033 $qry_where = '(' . $qry_where . ')';
1035 // OR rows ${'cur' . $or}[$x]
1036 if (!isset($curAndOrRow)) {
1037 $curAndOrRow = array();
1039 for ($y = 0; $y <= $row; $y++
) {
1043 for ($x = 0; $x < $col; $x++
) {
1044 if (!empty($curField[$x]) && !empty($
{'curOr' . $y}[$x]) && $x) {
1045 $qry_orwhere .= ' ' . strtoupper($curAndOrCol[$last_orwhere]) . ' ';
1047 if (!empty($curField[$x]) && !empty($
{'curOr' . $y}[$x])) {
1048 $qry_orwhere .= '(' . $curField[$x]
1050 . (get_magic_quotes_gpc() ?
stripslashes($
{'curOr' . $y}[$x]) : $
{'curOr' . $y}[$x])
1056 if ($criteria_cnt > 1) {
1057 $qry_orwhere = '(' . $qry_orwhere . ')';
1059 if (!empty($qry_orwhere)) {
1061 . strtoupper(isset($curAndOrRow[$y]) ?
$curAndOrRow[$y] . ' ' : '')
1066 if (!empty($qry_where) && $qry_where != '()') {
1067 $encoded_qry .= urlencode('WHERE ' . $qry_where . "\n");
1068 echo 'WHERE ' . htmlspecialchars($qry_where) . "\n";
1073 if (!isset($qry_orderby)) {
1076 for ($x = 0; $x < $col; $x++
) {
1077 if ($last_orderby && $x && !empty($curField[$x]) && !empty($curSort[$x])) {
1078 $qry_orderby .= ', ';
1080 if (!empty($curField[$x]) && !empty($curSort[$x])) {
1081 // if they have chosen all fields using the * selector,
1082 // then sorting is not available
1083 // Robbat2 - Fix for Bug #570698
1084 if (substr($curField[$x], -2) != '.*') {
1085 $qry_orderby .= $curField[$x] . ' ' . $curSort[$x];
1090 if (!empty($qry_orderby)) {
1091 $encoded_qry .= urlencode('ORDER BY ' . $qry_orderby);
1092 echo 'ORDER BY ' . htmlspecialchars($qry_orderby) . "\n";
1096 <input type
="hidden" name
="encoded_sql_query" value
="<?php echo $encoded_qry; ?>" />
1106 * Displays the footer
1108 require('./footer.inc.php3');