用php [关闭]从2个mysql表中获取数据

I have 2 tables, contacts and contacts_group. contacts has (id, name, lastname, email,), contacts_group has (id, owner, mail, group_name). How can i select this together with php.

Hans, you could use two different queries:

$contactsquery = "SELECT * FROM contacts";
$contacts_group_query = "SELECT * FROM contacts_group";

Of course you would need a DB Connection on top:

define("DB_SERVER","localhost");
define("DB_USER","chatApp");
define("DB_PASS","ibmpass");
define("DB_NAME","chatApp");

$connection = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);

And to perform them you would use

$result_1 = mysqli_query($connection, $contactsquery);
$result_2 = mysqli_query($connection, $contacts_group_query);

Or you could create a foreign key on the contacts_group to keep track of the contacts, and then join them in code as:

SELECT * FROM contacts_group cg JOIN contacts c ON c.id_group = cg.id

But I would highly recommend you to read this basic introduction to PHP & Queries before getting deeper, as you will have many doubts about it.

You should have a foreign key in contacts which refers to id in contacts_group, then you can do something like this:

SELECT * FROM contacts_group cg JOIN contacts c ON c.id_group = cg.id

You should read about relationships in MySQL ;)