我有一个小脚本,从2个单独的表中加入2行并将它们一起打印。
但是,我想从2个单独的表中逐个拉出并打印这2个分开的行(分别为css设计每一行结果)而不是JOIN
sql JOIN进程:
SELECT b.topic, a.message, a.threadID
FROM wbb1_post a
JOIN wbb1_thread b ON b.threadID = a.threadID
整个脚本 index3.php :
<?php
//header('Content-Type: text/html; charset=utf-8');
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
$id =(int)$_GET['id'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
mysqli_set_charset($conn,"utf8");
$sql = "SELECT b.topic, a.message, a.threadID
FROM wbb1_post a
JOIN wbb1_thread b ON b.threadID = a.threadID
WHERE a.threadID = $id";
//print_r
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$obj[] = $row;
}
} else {
}
$conn->close();
?>
test1.php 页面代码:
<div>
<?php
$_GET['id'] = 4;
include 'index3.php' ;?>
</div>
单独打印2行结果我想到了类似的东西:
index3.php 代码:
$sql = "SELECT a.message, a.threadID
FROM wbb1_post a
WHERE a.threadID = $id";
$sql2 = "SELECT b.topic, b.threadID
FROM wbb1_thread b
WHERE b.threadID = $sub";
//print_r
$result = $conn->query($sql);
$result = $conn->query($sql2);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$obj[] = $row;
}
} else {
}
$conn->close();
?>
test1.php 代码:
<div>
<?php
$_GET['sub'] = 4;
include 'index3.php' ;?>
</div>
<br><br>
<div>
<?php
$_GET['id'] = 4;
include 'index3.php' ;?>
</div>
但我得到的是主题行['id']两次。
答案 0 :(得分:0)
使用UNION
$sql = "SELECT a.message AS Val, a.threadID
FROM wbb1_post a WHERE a.threadID = $id
UNION ALL
SELECT b.topic AS Val, b.threadID
FROM wbb1_post a WHERE b.threadID = $id";