While this is a mysql/database question I believe the answer to my question is using php, and that's why i'm posting it here instead of the database section. I have a database with restaurants. They are located in different parts of the city, and have a 'area' row, which defines where in the city the place is. I use this query to get the information and echo it on each page: <?php include 'include/opendb.php'; $query = "SELECT * FROM restaurants WHERE restaurantid = '$id'"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "{$row['name']} in {$row['area']} "; } include 'include/closedb.php'; ?> PHP: Now, here's what I want to do (And my problem.) I also want to show a list with, let's say, 5 other places in the same area (with the same value as the one on the page. To do this I'd have to run this query: SELECT * FROM restaurants WHERE area = same-as-the-restaurant-above" So, my question is: How do I get that value inside the SELECT-part of the mysql query? (As above.) As you probably can tell I'm no database/php genious, so any answers would help a lot!
After the while: On the while before of after the echo, set too: $area=$row['area']; After the while: $sql="SELECT * FROM restaurants WHERE area = '".$area."' LIMIT 5;"; $result = mysql_query($sql); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "{$row['name']} in {$row['area']} "; }
echo "{$row['name']} in {$row['area']} "; $area = mysql_real_escape_string($row['area']); $sql2 = "select name from restaurants where area = '{$area}' and id != {$id} limit 5"; $result2 = mysql_result($sql2) or die(mysql_error()); if (mysql_num_rows($result2)) { echo "<p>Some other fine restaurants in the area:</p><ul>"; while ($row2 = mysql_fetch_assoc($result2)) echo "<li>{$row2['name']}</li>"; echo "</ul>"; } Code (markup):
Thank you! I'm using the code magiatar gave me, and it works out perfectly. SmallPotatoes: Thanks aswell!
Magiatar's query will also show the same restaurant as being near itself sometimes. Better to add the: and id != {$id} to the query.