Home » Php » php SQL Issue with Self Join and SELECT query results

php SQL Issue with Self Join and SELECT query results

Posted by: admin October 29, 2018 Leave a comment

Questions:

I have a SQL database containing the following (among other columns):

+------------+-------------+------------+-------+---------+-----------+
| 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. The following, simple “SELECT” query “works”, i.e. “success!” 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, e.g. through:

  $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”

I’m expecting it’s a problem with $result->fetch_assoc(), but I’m not sure.

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).

$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.

Can anyone see where I’m going wrong? Note: I’ve tried various uses of AS within the query as well, e.g.:

$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)

$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!

Answers: