自我加入和SELECT查询结果

时间:2018-05-24 04:20:46

标签: php sql

我有一个SQL数据库,其中包含以下内容(以及其他列):

+------------+-------------+------------+-------+---------+-----------+
| sess_id    | place_order |    city    | st    | country | continent |
+------------+-------------+------------+-------+---------+-----------+
|        123 |           1 | Brussels   |       | Germany | Europe    |
|        123 |           2 | Berlin     |       | Germany | Europe    |
|        456 |           1 | Munich     |       | Germany | Europe    |
|        456 |           2 | Test       |       | Germany | Europe    |
|        456 |           3 | Ams        |       | Germany | Europe    |
|        789 |           1 | Not Munich |       | Germany | Europe    |
|        789 |           2 | Test       |       | Germany | Europe    |
+------------+-------------+------------+-------+---------+-----------+

我知道我已成功设置SQL连接,因为我使用INSERT将数据写入“cities”表。以下简单的“SELECT”查询“有效”,即“成功!”回应:

  $sql_query = "SELECT `city` FROM cities WHERE `country`='$country'";

  $result = $conn->query($sql_query);

  if ($result->num_rows > 0) {

   echo "success!";

    } else {

        echo "0 results";
    }
}

但是,当我尝试使用$results执行任何操作时,例如通过:

  $sql_query = "SELECT `city` FROM cities WHERE `country`='$country'";

  $result = $conn->query($sql_query);

  if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {

    echo "city: " . $row["city"];

}

} else {

    echo "0 results";
}

我收到以下错误: “此页面无效 localhost没有发送任何数据。 ERR_EMPTY_RESPONSE“

我期待$result->fetch_assoc()出现问题,但我不确定。

此外,我最终需要通过以下查询使用自联接(这与我的管理员的php完全一样,并产生我预期的结果)。

$sql_query = "SELECT A.sess_id, A.city, A.place_order, B.city, B.st, 
B.country, B.continent, B.place_order, B.sess_id FROM 
cities AS A, cities AS B WHERE A.sess_id = B.sess_id AND A.city = 
'$city' AND A.place_order = '$counter' AND B.place_order = 
'$next_counter'";

$ city =我的测试查询中的慕尼黑,$ counter = 1和$ next_counter = 2.目标是用上面的$sql_query替换它。

谁能看到我哪里出错了?注意:我也在查询中尝试了AS的各种用法,例如:

$sql_query = "SELECT A.sess_id AS sessId1, A.city AS sessID2, 
A.place_order AS placeOrder1, B.city AS city2, B.st AS st2, 
B.country AS country2, B.continent AS 
continent2, B.place_order AS placeOrder2, B.sess_id AS sessId2 FROM 
cities AS A, cities AS B WHERE A.sess_id = B.sess_id AND A.city = 
'$city' AND A.place_order = '$counter' AND B.place_order = 
'$next_counter'";

&安培; (简化“where”,仅为表添加AS)

$sql_query = "SELECT A.sess_id, A.city, A.place_order, B.city, B.st, 
B.country, B.continent, B.place_order, B.sess_id FROM 
cities AS A, cities AS B WHERE A.sess_id = B.sess_id";

没有运气。

谢谢!任何帮助将不胜感激;我已经坚持了很长一段时间了,不知道还有什么可以尝试的!

1 个答案:

答案 0 :(得分:0)

我的修复:

我调整了使用PHP连接SQL的方式:

  DEFINE('DB_USERNAME', 'username');
  DEFINE('DB_PASSWORD', 'password');
  DEFINE('DB_HOST', 'localhost');
  DEFINE('DB_DATABASE', 'sample.db');

  $mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

  if (mysqli_connect_error()) {

    die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());

  } else {

    echo 'Connected successfully.';

  }

而是通过以下方式查询:

$sql_query = "SELECT city FROM cities WHERE country='$country'";


$result = $mysqli->query($sql_query);

  if ($result->num_rows > 0) {


// Associative array
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
echo $row["city"];

// Free result set
mysqli_free_result($result);


} else {
    echo "0 results";
}

}

我更复杂的自联接SQL查询现在也可以正常工作。

我还更新了我的Mac(安全更新),更新了MAMP,重新启动了我的Mac,并在Visual Studio Code中使用了PHP Debug 1.12.3

我不完全确定这些修补程序中的一个是否解决了我的问题,但此解决方案有效。感谢Vivek_23坚持我!