2

I want to retrieve data from multiple tables using dot operator/join where arguments are passed from an HTML/PHP form.

HTML CODE

<input name="rollno" type="text" placeholder="Roll Number" required>
<input name="submit_view_details" type="submit" value="Proceed">

PHP CODE

if(isset($_POST['submit_view_details']))
{
    $rollno = (int) $_POST['rollno'];
    $query = "select * from table1, table2 where table1.{$rollno}=table2.{$rollno}";
    $result=mysqli_query($connection,$query);
}

In the browser if enter the input 1 and echo this query then it looks like follows:
select * from table1, table2 where table1.1=table2.1

and no row is fetched despite of having data in the table(s).

it only works if the query looks like follows:
select * from table1,table2 where table1.rollno=table2.rollno

However, in that case it fetches all the rows but I need only the row of the rollno that user entered in the above mentioned form.

I am just not able to work this out. Help would be much appreciated. Thanks!

2
  • try this $query = "select * from table1, table2 where table1.rollno=table2.rollno AND table1.rollno = $rollno"; Commented Apr 29, 2015 at 10:55
  • @GaneshPatil Ya that works. I was missing that only. Thanks Commented Apr 29, 2015 at 11:07

4 Answers 4

3

Use the AND keyword to specify the rollno.

SELECT * FROM table1, table2 WHERE table1.rollno = table2.rollno 
AND table1.rollno = {$rollno};

You could probably use the keyword JOIN instead like this :

SELECT * FROM table1 NATURAL JOIN table2 
WHERE rollno = {$rollno};
Sign up to request clarification or add additional context in comments.

Comments

1

You need joins

take a reference of joins from here,

i am sure it will help

http://www.tutorialspoint.com/mysql/mysql-using-joins.htm

2 Comments

There is no such thing as mysqli or mysql JOINS. JOINS are just standard SQL.
if my answer was helpful can you mark it as useful, it will be helpful to others too
0

You should use join like this

$query = "SELECT tbl1.*, tbl2.* 
          FROM tbl1 
          INNER JOIN tbl2 ON tbl1.id = tbl2.id 
          WHERE tbl1.column = value ";

1 Comment

How can i add LIMIT to it?
0
foreach ($pieces_2 AS $value) {
         $pieces_3[] ="(CONCAT_WS('|',$presql2) like '%$value%')";      //concat all columns from one table
 }
 $serch_jyouken = implode(" and ",$pieces_3);       // for multiple keywords                    
 $result1 = mysqli_query($connection, "select p.p_no from pfr_data p where (" .$serch_jyouken .")");
 $res1 = array();
 while($r1 = mysqli_fetch_array($result1){
         $res1[] = $r1['p_no'] ;        //fetch primary key from table and store it into array 
 }
 foreach ($pieces_2 AS $value) {            
         $pieces_4[] ="(CONCAT_WS('|',$presql3) like '%$value%')";      // same as above
 }
 $serch_jyouken1 = implode(" and ",$pieces_4);
 $result2 = mysqli_query($connection, "select p2.p_no from pfr_mod_inform p2 where (" .$serch_jyouken1 .")" );
$res2 = array();
 while($r2 = mysqli_fetch_array($result2)){                                                                               
         $res2[] = $r2['p_no'];
 }
 $res_mrg = array_merge($res1 , $res2);     //merge array
 $result = implode("','",$res_mrg );        // array to sring
 $sql5 = $presql ." from pfr_data p where p.p_no in ('$result') order by p.section_p,p.status,p.no";

1 Comment

this is long method but i sure it will help & i will give result within second

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.