简体   繁体   English

带有自连接和SELECT查询结果的php SQL问题

[英]php SQL Issue with Self Join and SELECT query results

I have a SQL database containing the following (among other columns): 我有一个包含以下内容的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    |
+------------+-------------+------------+-------+---------+-----------+

I know that I've successfully set up my SQL connection as I've written data to my 'cities' table using INSERT. 我知道我已经成功地建立了SQL连接,因为我已经使用INSERT将数据写入了“ city”表。 The following, simple "SELECT" query "works", ie "success!" 下面,简单的“ SELECT”查询“工程”,即“成功!” is echoed: 回声:

  $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";
    }
}

However, when I try to do anything with $results , eg through: 但是,当我尝试使用$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";
}

I get the following error: "This page isn't working localhost didn't send any data. ERR_EMPTY_RESPONSE" 我收到以下错误: “此页面无法正常运行,本地主机未发送任何数据。ERR_EMPTY_RESPONSE”

I'm expecting it's a problem with $result->fetch_assoc() , but I'm not sure. 我期望$result->fetch_assoc()有问题,但是我不确定。

Further, my ultimate need is to use self join through the following query (which works exactly as intended in php my admin and yields my expected results). 此外,我的最终需求是通过以下查询使用自我联接(这完全符合我的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 = Munich in my test query, $counter = 1 and $next_counter = 2. The goal is to replace this with the above $sql_query . 在我的测试查询中,$ city =慕尼黑,$ counter = 1和$ next_counter =2。目标是将其替换为上面的$sql_query

Can anyone see where I'm going wrong? 谁能看到我要去哪里错了? Note: I've tried various uses of AS within the query as well, eg: 注意:我也尝试在查询中使用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'";

& (simplifying the "where", adding AS for table only) &(简化“ 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";

With no luck. 没有运气。

Thank you! 谢谢! Any help would be greatly appreciated; 任何帮助将不胜感激; I've been stuck on this for quite some time now and am not sure what else to try! 我已经在这个问题上停留了很长时间,并且不确定是否还有其他尝试!

My fix: 我的解决方法:

I adjusted the way I'm connecting to SQL with PHP to: 我将使用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.';

  }

And instead queried via: 而是通过以下方式查询:

$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";
}

}

My more complicated self join SQL query now works as well. 我更复杂的自连接SQL查询现在也可以工作。

I also updated my Mac (security updates), updated MAMP, restarted my Mac, and utilized PHP Debug 1.12.3 in Visual Studio Code. 我还更新了Mac(安全更新),更新了MAMP,重新启动了Mac,并在Visual Studio Code中使用了PHP Debug 1.12.3

I'm not entirely sure if one of these fixes or all solved my problem, but this solution works. 我不确定这些修补程序之一或全部解决了我的问题,但是此解决方案有效。 Thanks to Vivek_23 for sticking with me! 感谢Vivek_23坚持与我合作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM