0

I have a standard select query which selects all records from the products table and then display it in a table. I need another query to run after that based on the result, in order to get the a field from the customers table.

So for example. my query from the products table shows the user_id field. I need to run a SQL query simultaneously in order to get the user_name from the customers table.

$result = mysqli_query($con,"SELECT * FROM products (SELECT eu_name FROM customer WHERE $id_mobile = $id_mobile");

echo "<table border='1'>
<tr>
<th>user_id</th>
<th>user_name</th>
<th>selected_product</th>


while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))

echo "<td>" . $row['eu_name'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "<td>" . $row['selected_product'] . "</td>";
8
  • 4
    you can do with join but before that u need to post the table structures in your question. Commented Mar 24, 2014 at 17:10
  • 2
    in your given example, you haven't closed the first echo. Commented Mar 24, 2014 at 17:11
  • You definitely do not need a second a query. You just need to improve the first one. Commented Mar 24, 2014 at 17:19
  • @Nikola Hi that was just a quick copy and paste, just so that people have an idea what im talking about. Commented Mar 24, 2014 at 17:19
  • @AbhikChakraborty - what exactly do you need from the table stucture Commented Mar 24, 2014 at 17:20

1 Answer 1

1

With the given table structure

customers(user_id, user_name, user_email) 
products (product_id, product_name, user_id)

You can get all the products associated with customer as

select p.product_id,p.product_name,u.user_name,u.user_email
from products p
inner join customers u on u.user_id = p.user_id

You can add where condition for any additional filter of data.

Sign up to request clarification or add additional context in comments.

Comments

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.