How to Get Multiple Data from Two Different Table from One Database in PHP

how to get multiple data from two different table from one database in php

You should use another and only one query with JOIN statement.

SELECT * FROM cart c JOIN products p ON p.productid = c.productid WHERE c.userid = :uid

The data you will receive will contains products data from cart.

Selecting data from two different tables of the same database in PHP/ MYSQL

You could use IN in a way that the results from one query can be passed to another.

Perhaps this is an idea:

SELECT section_title FROM section WHERE section_id IN (SELECT section_id FROM subsection WHERE [some condition here to get the correct set of ids from subsection table])


What you're attempting to do can be condensed down into a single query.

In your example, it appears as though you're aiming to loop through the results of your first query and execute another SELECT for each result.

This can be simplified with a left join. With a left join, you'll get all of the results from the left table (table1) and the matching results from the right (table2). Where there is no match, the result will be null.

FROM table1 t1
LEFT JOIN table2 t2 ON t1.MID = t2.MID

MySQL how to fetch data from multiple table and insert into other?

When you need to insert into some table data selected from other tables you can use next approach:

  • first: build query that select data that should be inserted. In your case in can be next:

    SELECT 1,,,
    FROM first.items
    JOIN ON =
    WHERE = 1;

  • second: combine the query with INSERT statement like:

    INSERT INTO third.details (id, items_id, items_name, shop_name) 
    SELECT 1,,,
    FROM first.items
    JOIN ON =
    WHERE = 1;

PHP/MySQL Echo from multiple tables

basically the comments say it already: you can use joins for this task. However I think what you need is a proper introduction into the options and what it all means, so let me try help you there:

First question: What is your problem? If you want to get data from two tables indepedently then what you have two do is to make to separate queries. You more or less run this:

//first run this query
$sql = mysql_query("SELECT * FROM TICKET ",$mysql)
//Run the query and process the data

//then run this query
$sql = mysql_query("SELECT * FROM VISITOR ",$mysql)
//Process this data, too

What you probably actually want to do is to get data from ONE table based on data from ANOTHER table. For example you want to get some data about users and in one table you have their email address and their street address and in the other table you have their name. So you JOIN both tables together based on some information (key) they both contain. So then you go from

TABLE 1: id | email | address
TABLE 2: id | firstname | lastname

To this form:

NEW TABLE: id | email | address | firstname | lastname

There are different kinds of joins. The code for this could look something link this:

$sql = mysql_query("SELECT,, table1.address, table2.firstname, table2.lastname FROM TABLE1 LEFT JOIN TABLE2 ON ( =",$mysql)

As said before joins are common and properly explained elsewhere. I find this a good tutorial but of course the mentioned documentation ( also explained it maybe a bit more condensed.

mySQL select query, get data from multiple tables

Database : Postgresql

Try this:

SELECT,, t1.lastname, (array_agg(t2.order))[1] as order, 
(array_agg(t2.order))[2] as order1, (array_agg(t2.order))[3] as order2,
'Clicks to view' as "Moredetails"
FROM table1 as t1
LEFT JOIN table2 as t2 ON = t2."userID"
LEFT JOIN table3 as t3 ON = t3."userID"
GROUP BY 1,2,3


id name lastname order order1 order2  MoreDetails
1 John doe pizza pasta lasgana click to view

PHP mysql select data multiple tables on same page

Try this :

Select config_name, config_Option_name, price 

from table1
inner join table2 on (

Insert data into multiple tables using one form

You must call mysql_query() for every query.

$sql1 = "INSERT INTO donator ...";
$sql2 = "INSERT INTO donatie ...";
mysql_query($sql1, $con);
mysql_query($sql2, $con);


mysql_query() is deprecated! Please use mysqli_query()

You can also use mysqli_multi_query()

$query = "INSERT INTO donator ...; INSERT INTO donatie ...;";
mysqli_multi_query($link, $query);

Related Topics

Leave a reply