我必须选择与字段值匹配的多个记录。 E.g。
ID ---- REGION
--------------
01 ---- NORTH
02 ---- SOUTH
03 ---- NORTH
04 ---- WEST
我想选择区域NORTH中的所有ID。我想在数组中得到的结果是{01,03}
如何在MYSQL和PHP中执行此操作?
答案 0 :(得分:0)
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM TABLE WHERE REGION = 'NORTH'";
$result = $conn->query($sql);
$IDs = [];
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$IDs[] = $row["ID"]
}
}
$conn->close();
var_dump($IDs);