-4

I want to display all returned columns in one row seperated by "," or ";".

I submitted this question earlier but made it more confusing. Basically I have a mysql db with 19 columns A-S it also has over 200,000 rows of data. Some of the data are blank cells some match in certain columns but not other columns. What I have been able to do so far is take input based off those 19 columns and based off any and or all of those input return values into a table that match those columns. So if the user leaves one column blank it will return it based off the values the user used. So the user can use 1 column or all 19 potentially A-S.

What I am trying to do however though is in another table on that page above the individual entries that display in multiple rows is have just one row that returns all 19 columns and all the values seperated by "," or ";".

So for a quick example lets say i have 3 columns A,B,C user inputs 3 into the input that corresponds to B and hits submit. the return currently would look something like this

A|B|C
4|3|3
2|3|9
1|3|2

I want to somehow show on this page this

A    |B|C 
4,2,1|3|3,9,2

I just cant figure out how to take the user entries and return them to that one row separated by a character.

<body>

<form action="" method="post">

<input name="Asearch" placeholder="A" type="search" autofocus>
<input name="Bsearch" placeholder="B"type="search">
<input name="Csearch" placeholder="C" type="search">
<input name="Dsearch" placeholder="D" type="search">
<input name="Esearch" placeholder="E" type="search">
<input name="Fsearch" placeholder="F" type="search">
<input name="Gsearch" placeholder="G" type="search">
<input name="Hsearch" placeholder="H" type="search">
<input name="Isearch" placeholder="I" type="search">
<input name="Jsearch" placeholder="J" type="search">
<input name="Ksearch" placeholder="K" type="search">
<input name="Lsearch" placeholder="L" type="search">
<input name="Msearch" placeholder="M" type="search">
<input name="Nsearch" placeholder="N" type="search">
<input name="Osearch" placeholder="O" type="search">
<input name="Psearch" placeholder="P" type="search">
<input name="Qsearch" placeholder="Q" type="search">
<input name="Rsearch" placeholder="R" type="search">
<input name="Ssearch" placeholder="S" type="search">
<input type="submit" name="button">

</form>

<table align="center" border="1px" style="width:auto"; line-height:40px;">
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>E</th>
<th>F</th>
<th>G</th>
<th>H</th>
<th>I</th>
<th>J</th>
<th>K</th>
<th>L</th>
<th>M</th>
<th>N</th>
<th>O</th>
<th>P</th>
<th>Q</th>
<th>R</th>
<th>S</th>
</tr>

<?php

$con=mysql_connect('localhost', 'USER', 'PW');
$db=mysql_select_db('DB');


if(isset($_POST['button'])){    //trigger button click

$searchA=$_POST['Asearch'];
$searchB=$_POST['Bsearch'];
$searchC=$_POST['Csearch'];
$searchD=$_POST['Dsearch'];
$searchE=$_POST['Esearch'];
$searchF=$_POST['Fsearch'];
$searchG=$_POST['Gsearch'];
$searchH=$_POST['Hsearch'];
$searchI=$_POST['Isearch'];
$searchJ=$_POST['Jsearch'];
$searchK=$_POST['Ksearch'];
$searchL=$_POST['Lsearch'];
$searchM=$_POST['Msearch'];
$searchN=$_POST['Nsearch'];
$searchO=$_POST['Osearch'];
$searchP=$_POST['Psearch'];
$searchQ=$_POST['Qsearch'];
$searchR=$_POST['Rsearch'];
$searchStem=$_POST[‘Ssearch’];



$query=mysql_query("select * from DB where A like '%{$searchA}%' AND B like '%{$searchB}%' AND C like '%{$searchC}%' AND D like '%{$searchD}%'
AND E like '%{$searchE}%' AND F like '%{$searchF}%' AND G like'{$searchG}%' AND H like '%{$searchH}%'
AND I like '%{$searchI}%' AND J like '%{$searchJ}%' AND K like '%{$searchK}%' AND L like '%{$searchL}%'
AND M like '%{$searchM}%' AND M like '%{$searchM}%' AND N like '%{$searchN}%' AND O like '%{$searchO}%'
AND Q like '%{$searchQ}%' AND R like '%{$searchR}%' AND S like '%{$searchS}%' LIMIT 1000");


if (mysql_num_rows($query) > 0) {


while ($row = mysql_fetch_array($query)) {
echo "<tr><td>".$row['A']."</td><td>".$row['B']." </td><td>".$row['C']."</td><td>".$row['D']."</td><td>".$row['E']."</td><td>".$row['F']."</td><td>".$row['G']."</td>
<td>".$row['H']."</td><td>".$row['I']."</td><td>".$row['J']."</td><td>".$row['K']."</td><td>".$row['L']."</td><td>".$row['M']."</td><td>".$row['N']."</td>
<td>".$row['O']."</td><td>".$row['P']."</td><td>".$row['Q']." </td><td>".$row['R']."</td><td>".$row[‘S’]."</td></tr>";
}}else{echo "No DATA<br><br>";}
}else{                          //while not in use of search  returns all the values
$query=mysql_query("select * from DB LIMIT 10");

while ($row = mysql_fetch_array($query)) {
echo "<tr><td>".$row['A']."</td><td>".$row['B']."</td><td>".$row[‘C’]."</td><td>".$row['D']."</td><td>".$row['E']."</td><td>".$row['F']."</td><td>".$row['G']."</td>
<td>".$row['H']."</td><td>".$row['I']."</td><td>".$row['J']."</td><td>".$row['K']."</td><td>".$row['L']."</td><td>".$row['M']."</td><td>".$row['N']."</td>
<td>".$row['O']."</td><td>".$row['P']."</td><td>".$row['Q']."</td><td>".$row['R']."</td><td>".$row['S']."</td></tr>";
}}mysql_close();?>

Have changed the Search and now i don't get the table returned i get the no data error

$query=mysql_query("select group_concat(distinct A order by A) as A,   group_concat(distinct B order by B) as B, group_concat(distinct C by C) as C, group_concat(distinct D order by D) as D, group_concat(distinct E order by E) as E, group_concat(distinct F order by F) as F, group_concat(distinct G order by G) as G,group_concat(distinct H order by H) as H, group_concat(distinct I order by I) as I, group_concat(distinct J order by J) as J, group_concat(distinct K order by K) as K, group_concat(distinct L order by L) as L, group_concat(distinct M order by M) as M, group_concat(distinct N order by N) as N, group_concat(distinct O order by O) as O, group_concat(distinct P order by P) as P, group_concat(distinct Q order by Q) as Q, group_concat(distinct R order by R) as R, group_concat(distinct S order by S) as S from DB where A like '%{$searchA}%' AND B like '%{$searchB}%' AND C like '%{$searchC}%' AND D like '%{$searchD}%' AND E like '%{$searchE}%' AND F like '%{$searchF}%' AND G like'%{$searchG}%' AND H like '%{$searchH}%' AND I like '%{$searchI}%' AND J like '%{$searchJ}%' AND K like '%{$searchK}%' AND L like '%{$searchL}%' AND M like '%{$searchM}%' AND M like '%{$searchM}%' AND N like '%{$searchN}%' AND O like '%{$searchO}%' AND Q like '%{$searchQ}%' AND R like '%{$searchR}%' AND S like '%{$searchS}%' LIMIT 1000");
if (mysql_num_rows($query) > 0) {


while ($row = mysql_fetch_array($query)) {
echo "<tr><td>".$row['NODE']."</td><td>".$row['KMA']."</td><td>".$row['IRISCARSHE']."</td><td>".$row['CMTS']."</td><td>".$row['DIVISION']."</td><td>".$row['SYS']."</td><td>".$row['PRN']."</td>
<td>".$row['VODBACKOFFICE']."</td><td>".$row['ZIP']."</td><td>".$row['CITY']."</td><td>".$row['STATE']."</td><td>".$row['BILLER']."</td><td>".$row['HUBNAME']."</td><td>".$row['SITEIDBU']."</td>
<td>".$row['ALTHUBID']."</td><td>".$row['USPSCITIES']."</td><td>".$row['COUNTYBYZIP']."</td><td>".$row['HUBID']."</td><td>".$row['SYSTEMORTIMEZONE']."</td>
</tr>";
}}else{echo "No DATA<br><br>";}
  • 1
    You really should not use the `mysql_` functions anymore. They were deprecated years ago and are no longer present in PHP 7. Use the `mysqli_` or `PDO` API. – trincot Dec 29 '18 at 23:34

1 Answers1

2

You can use group_concat in your SQL query to achieve this:

select group_concat(distinct A order by A) as A,
       group_concat(distinct B order by B) as B
from   DB 
where  A like '%{$searchA}%' AND B like '%{$searchB}%' 

Note that you have a missing % in the like condition for G.

Also, please do yourself a favour and step away from the mysql_ API. It has been deprecated years ago (there is big warning sign in the documentation). Use mysqli or PDO instead.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • Thanks. I started working off yours. I got a query I believe that should work based of the 19 values the user could input but nothing is being displayed. not sure at this point if it is the query or if i should be displaying it to the html table some other way? – Buddy Harrison Dec 30 '18 at 01:26
  • Make sure you test the query directly in PhpAdmin first to make sure it has no syntax issues. – trincot Dec 30 '18 at 07:59
  • Thanks so much. Caught what my issue was. Appreciate it so much. Its working now. – Buddy Harrison Dec 30 '18 at 16:03
  • Great to hear ;-) – trincot Dec 30 '18 at 16:08