0

i have 2 different table named productlist and rsales

the ff data in productlist are

id|pcode|
1 |1220 |

and the ff data in rsales are

id|total|total_discount|
3 |500  |      50      |

my problem is that i want to select data from two different table which is productlist and rsales and display it horizontally. i tried everything but it display vertically. the result must be something like this

id|pcode|total|total_discount|
1 |1220 |500  |   50         | 

this is my code so far but it only display data from single table

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("inventory", $con);



$result = mysql_query("SELECT * FROM productlist");

while($row = mysql_fetch_array($result))
  {
echo '<tr>';
echo '<td>'.$row['pcode'].'</td>';
echo '</tr>';
  }

mysql_close($con);
?>
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
user2656724
  • 79
  • 10
  • Use join for two tables. So that you can get values in single row. – Saranya Sadhasivam Aug 27 '13 at 10:59
  • Where is the link between `productlist` and `rsales`? Also, using `mysql_*` is now strongly discouraged. The library is deprecated and you should really be using MySQLi or PDO... – BenM Aug 27 '13 at 10:59
  • You propably want to read this: [MySql Join](http://dev.mysql.com/doc/refman/5.0/en/join.html) – Jan Zeiseweis Aug 27 '13 at 11:00
  • 1
    There is no relevant column between these tables. Based on which column you want to merge data between these tables. – DB_learner Aug 27 '13 at 11:00
  • im just new to php and don't really know all. how can i apply what you are trying to say sir :) – user2656724 Aug 27 '13 at 11:03
  • there is no link between rsales and produclist i just wanna get data from these table. – user2656724 Aug 27 '13 at 11:04
  • hi there, @Mani is telling you that you need a common bit of info in each row to be able to link the data from different tables together. I linked a Q&A that I posted a while back in my answer below. You would do really well to have a good read of it. – Fluffeh Aug 27 '13 at 11:08

3 Answers3

0

Is the id difference between them offset by 2?If so,you can use Fluffeh answer like this:

select
 ffdata.pcode,
 rsales.total,
 rsales.total_discount
from
 ffdata
    join rsales
        on ffdata.id=rsales.id+2
Mihai
  • 26,325
  • 7
  • 66
  • 81
  • This won't work as the tables have a different number of columns - nor would it really work anyhow as the data would come out on different rows - which is not what the question asked for. – Fluffeh Aug 27 '13 at 11:05
  • i think union can't be use for the kind of problem because there is no link or no common at all i just want to get data from two table . but i tried this code and error appears Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\eduards.org\admin\itemsales.php on line 136 – user2656724 Aug 27 '13 at 11:13
0

You need to use a join in your SQL so that the data comes out in a single row in the results and is matched on the correct fields.

I wrote a lengthy Q&A about it a while back for just this sort of question which you will really do well to read, but in short, your query should look something like this:

select
    ffdata.pcode,
    rsales.total,
    rsales.total_discount
from
    ffdata
        join rsales
            on ffdata.id=rsales.id

This is assuming that you have a common key on the two tables that links the records.

Community
  • 1
  • 1
Fluffeh
  • 33,228
  • 16
  • 67
  • 80
-1

there is no link between rsales and produclist but you can try like this, you will get all fileds from both the tables using this

$result = mysql_query("SELECT * FROM productlist,rsales");
chirag ode
  • 950
  • 7
  • 15
  • this is awesome code simple and direct. but what about if im going to sum the "total" value from rsale? any idea sir? i tried sum(total) but error appears :D – user2656724 Aug 27 '13 at 11:22
  • for that you need to define all the column names instead of (*) in your query with sum(total) – chirag ode Aug 27 '13 at 11:37