根据用户输入加入表时的MySQL语法错误

时间:2018-08-09 21:13:24

标签: php mysql sql

我有五个表可以产生所需的结果,但是有五个不同的查询。

表为[温度,零件尺寸,体积,刚度和重量]。每个表都有一个“类型”列,在所有表中具有相同的值。

以下查询为我提供了所需的结果...但是在五个不同的表中。

$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会做到这一点,而且我很确定这是一个简单的语法错误,但我无法弄清楚。

我已经尝试过此方法(以及许多变化...使用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 ");

当我尝试INNER JOIN或JOIN表时,似乎会出现问题。我已经花了几个小时了。 :/有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

我不知道您使用的是哪个版本的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);
?>