我有五个表可以产生我需要的结果,但是有五个不同的查询。
表包括温度、partSize、体积、刚度和重量。每个表都有一个列"type“,它在所有表中保存相同的值。
下面的查询在5个不同的表中给出了我need...but的结果。
$result_temp = mysqli_query($conn, "SELECT * FROM temperature WHERE '%{$_POST['temperature']}%' LIKE '%{$_SESSION['temperature']}%'");
$result_size = mysqli_query($conn, "SELECT * FROM partSize WHERE '%{$_POST['partSize']}%' LIKE '%{$_SESSION['partSize']}%'");
$result_vol = mysqli_query($conn, "SELECT * FROM volume WHERE '%{$_POST['volume']}%' LIKE '%{$_SESSION['volume']}%'");
$result_stiff = mysqli_query($conn, "SELECT * FROM stiffness WHERE '%{$_POST['stiffness']}%' LIKE '%{$_SESSION['stiffness']}%'");
$result_wght = mysqli_query($conn, "SELECT * FROM weight WHERE '%{$_POST['weight']}%' LIKE '%{$_SESSION['weight']}%'");我需要一个将这些表的结果结合在一起的解决方案,而我被卡住了。我一直收到这个错误:mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean
我知道mySQL会做到这一点,我非常确定这是一个简单的语法错误,但我无法解决这个问题。
我已经尝试过了(还有很多variations...with CONCAT,没有CONCAT,没有刻度线,没有刻度线):
$result = mysqli_query($conn, "SELECT * FROM `temperature` WHERE CONCAT ('%{$_POST['partSize']}%') LIKE CONCAT ('%{$_SESSION['partSize']}%') INNER JOIN `partSize` ON temperature.type = partSize.type INNER JOIN `volume` ON partSize.type = volume.type INNER JOIN `stiffness` ON volume.type = stiffness.type INNER JOIN `weight` ON stiffness.type = weight.type ");当我尝试内部连接或连接表时,问题似乎就出现了。我已经在这上面花了好几个小时了。:/有人能帮上忙吗?
发布于 2018-08-10 05:34:24
我不知道您使用的是哪个版本的PHP,但我建议您切换到PDO并使用准备好的语句(不管您的版本是什么)。但是,语句的语法应该是:
SELECT [columns] FROM [first table] INNER JOIN [other tables] ON [first table] WHERE [conditions]说到这里,让我们看一下这个例子:
<?php
$config = array(
'host' => '[db host ip]',
'username' => '[db username]',
'password' => '[db password]',
'name' => '[db name]'
);
$dsn = "mysql:dbname=$config[name];host=$config[host];charset=utf8mb4";
$db = new PDO($dsn, $config['username'], $config['password']);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$stmt = $db->prepare('
SELECT
*
FROM
`temperature` AS t
INNER JOIN `partSize` AS p ON t.`type` = p.`type`
INNER JOIN `volumne` AS v ON t.`type` = v.`type`
INNER JOIN `stiffness` AS s ON t.`type` = s.`type`
INNER JOIN `weight` AS w ON t.`type` = w.`type`
WHERE
t.`temperature` LIKE CONCAT('%', :temperature, '%') AND
t.`partSize` LIKE CONCAT('%', :partSize, '%') AND
t.`volumne` LIKE CONCAT('%', :volumne, '%') AND
t.`stiffness` LIKE CONCAT('%', :stiffness, '%') AND
t.`weight` LIKE CONCAT('%', :weight, '%');');
// Execute the query passing the $_POST values as the parameters
$stmt->execute(array(
':temperature' => $_POST['temperature'],
':partSize' => $_POST['partSize'],
':volume' => $_POST['volume'],
':stiffness' => $_POST['stiffness'],
':weight' => $_POST['weight']
);
// Get all of the rows returned by the query and store them in an associative array
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>https://stackoverflow.com/questions/51775860
复制相似问题