0

Here is my MySQL code that I am trying to run using PHP. I give it two values each from RelationA variable and RelationB variable. Upon reading it, the script parses the result table and generates following output.

<?PHP
$Relation_a1=$_POST["relationa1"];
$Relation_a2=$_POST["relationa2"];
$Relation_a3=$_POST["relationa3"];
$Relation_a4=$_POST["relationa4"];
$Relation_a5=$_POST["relationa5"];
$Relation_a6=$_POST["relationa6"];
$Relation_a7=$_POST["relationa7"];
$Relation_a8=$_POST["relationa8"];
$Relation_a9=$_POST["relationa9"];
$Relation_a10=$_POST["relationa10"];
$Relation_b1=$_POST["relationb1"];
$Relation_b2=$_POST["relationb2"];
$Relation_b3=$_POST["relationb3"];
$Relation_b4=$_POST["relationb4"];
$Relation_b5=$_POST["relationb5"];
$Relation_b6=$_POST["relationb6"];
$Relation_b7=$_POST["relationb7"];
$Relation_b8=$_POST["relationb8"];
$Relation_b9=$_POST["relationb9"];
$Relation_b10=$_POST["relationb10"];
$connect=mysql_connect('localhost','user','user')
or die('could not open:'. mysql_error());
mysql_select_db('user_db');     
$result1=mysql_query("SELECT GROUP_CONCAT(id SEPARATOR '_') into @var1 FROM relative1 WHERE name='$Relation_a1' OR name='$Relation_a2' OR name='$Relation_a3' OR name='$Relation_a4' OR name='$Relation_a5' OR name='$Relation_a6' OR name='$Relation_a7' OR name='$Relation_a8' OR name='$Relation_a9' OR name='$Relation_a10'");
$result2=mysql_query("SELECT GROUP_CONCAT(id SEPARATOR '_') into @var2 FROM relative2 WHERE name='$Relation_b1' OR name='$Relation_b2' OR name='$Relation_b3' OR name='$Relation_b4' OR name='$Relation_b5' OR name='$Relation_b6' OR name='$Relation_b7' OR name='$Relation_b8' OR name='$Relation_b9' OR name='$Relation_b10'");
$result=mysql_query("SELECT final_result FROM result WHERE id IN (SELECT GROUP_CONCAT(@var1,'-',@var2))");
$ncols=mysql_num_fields($result);/* How many columns (fields) */
$queryResults = NULL;
if( mysql_num_rows($result)>0)
{
print "<br>";
print "<table cellpadding=0 cellspacing=0 width=100% border=1 bordercolor=skyblue>";
print "<tr>";
for ( $i=0; $i<$ncols; $i++ ) 
        {
        $column_name=mysql_field_name($result,$i);  /* field name */        
            print "<td align=center><b>".$column_name."</b></td>";
        }
    print "</tr>";
    $total=0;
    while ( $row = mysql_fetch_array($result) ) /* process result row-by-row */
    { 
        print "<tr>";
        $total++;
        for ( $i=0; $i<$ncols; $i++ ) 
        {

            $column_value=$row[$i];            /* field value */
            print "<td align=center>".$column_value . "</td>"; 
        }
    }
    mysql_free_result($result);
}
else
{
echo ("<h3>There is no data pertaining to you request</h3>");
}
 ?> 

My output is always the same no matter what input I feed to the Relation tables.

Nazmul
  • 575
  • 3
  • 18
  • Have checked `@var1` and `@var2` values? Suggestion: don't use mysql_ functions, instead use: mysqli_ or PDO functions, use IN instead of many ORs – Gerardo Charles Rojas Vega May 24 '15 at 19:50
  • Hi Charles, Yes I did checked var1 on my server end. But I am not sure, how to print them in the front end on the site. I tried echoing the result1 and 2. Also tried var_dump but I got lost. – Ravi Varma May 24 '15 at 19:59
  • So it would just depends on the data/result you're expecting to get comparing with the one you're getting until now (try add that to your question) – Gerardo Charles Rojas Vega May 24 '15 at 20:05
  • Here is a solution I found out by myself $result3=mysql_query("SELECT GROUP_CONCAT(@var1,'-',@var2) INTO @var3"); $result=mysql_query("SELECT final_result FROM result WHERE id =@var3"); – Ravi Varma May 24 '15 at 23:31
  • If that solve your problem you may try `SELECT final_result FROM result WHERE id = CONCAT(@var1,'-',@var2)` – Gerardo Charles Rojas Vega May 25 '15 at 00:18

0 Answers0