我正在为我的网站创建一个详细信息页面,其中将从数据库(mysql)中检索信息。我创建了一个小的PHP页面,它从我的地址表中的人员表中检索所有内容。我的人员表与地址表有关系。但是当我输出连接到address_id的person_address表时,它只显示id而不是信息。
<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = "persons";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT person_id, person_firstname, person_lastname,
person_email, person_phonenumber, person_cv,
person_address
FROM person";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["person_id"]. " -firstname: " . $row["person_firstname"]. "lastname " . $row["person_address"]. "email " . $row["person_email"]. "phonenumber " . $row["person_phonenumber"]. "cv " . $row["person_cv"]. "address " . $row["person_address"]."<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
我正在尝试编写OOP,我希望你们能帮助我。我是PHP的新手,我知道我的选择查询并不好,因为我还需要选择我的地址表,但我无法以一种可行的方式更改查询。当然稍后我会使用Javascript和AJAX,然后我会做一个选择下拉菜单,管理员可以选择address_city或address_state来显示生活在所选区域的人。
答案 0 :(得分:1)
您正在此处打印person_address,这是您人员表中的ID。因此,您需要使用此ID从地址表中获取详细信息
为此,您需要更改查询: -
$sql = "SELECT person_id, person_firstname, person_lastname,
person_email, person_phonenumber, person_cv,
address_street,address_housenumber,
address_city,address_state,address_zipcode
FROM person
inner join address on address.address_id = person.person_address";