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; ?>"></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; ?>"></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; ?>"></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 . ']'; ?>"><?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">
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
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
739 $wheretabs = 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);
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);
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)) {
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) {
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'];
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) {
825 $rel[$value]['mcon'] = 0;
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';
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);
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;
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
870 $qry_from = PMA_backquote($master);
873 while (list($key, $varr) = each($rel)) {
874 if (!isset($varr['link']) ||
$varr['link'] == '') {
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';
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);
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
916 $master = $row['master_table'];
919 if ($row["foreign_table"] == $master) {
920 $rel[$found_table]['mcon'] = 1;
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
931 while (list($key, $varr) = each($rel)) {
932 if ($varr['link'] == '') {
933 if ($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
941 $lj .= $varr['link'];
943 $ljm .= $varr['link'];
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
958 while (list($k, $v) = each($alltabs)) {
963 while (list($k, $v) = each($temp)) {
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";
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] . ')';
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++
) {
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]
1008 . (get_magic_quotes_gpc() ?
stripslashes($
{'curOr' . $y}[$x]) : $
{'curOr' . $y}[$x])
1014 if ($criteria_cnt > 1) {
1015 $qry_orwhere = '(' . $qry_orwhere . ')';
1017 if (!empty($qry_orwhere)) {
1019 . strtoupper(isset($curAndOrRow[$y]) ?
$curAndOrRow[$y] . ' ' : '')
1024 if (!empty($qry_where) && $qry_where != '()') {
1025 $encoded_qry .= urlencode('WHERE ' . $qry_where . "\n");
1026 echo 'WHERE ' . htmlspecialchars($qry_where) . "\n";
1031 if (!isset($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];
1048 if (!empty($qry_orderby)) {
1049 $encoded_qry .= urlencode('ORDER BY ' . $qry_orderby);
1050 echo 'ORDER BY ' . htmlspecialchars($qry_orderby) . "\n";
1054 <input type
="hidden" name
="encoded_sql_query" value
="<?php echo $encoded_qry; ?>" />
1064 * Displays the footer
1066 require('./footer.inc.php3');