MySQL同表关联的id替换名称,用另一个表中的名称替换已拉取的SQL ID值

I have some code (see below) that populates a table with all the records. I however want to replace the ID that is presented for site_id with its actual name which is stored in another table. The site_id for example is the primary key in a table called sites_tbl, and I want to pull the associated sitename and display this in the table rather than the ID which comes from the sheets_tbl as a foreign key. I assume I need to do some kind of loop, where foreach site_id within the $data variable Select the sitename Where site_id = the $row['site_id'] However I cannot get it to work.

$sql = "SELECT * FROM sheet_tbl";

$stmt = $conn->prepare($sql);

$stmt->execute();

$data = $stmt->fetchAll();

=$row['sheet_id']?>=$row['username']?>=$row['site_id']?>

解决方案

I would advise a very sim

你可能感兴趣的:(MySQL同表关联的id替换名称,用另一个表中的名称替换已拉取的SQL ID值)