php/mysql query to retrieve dynamic value

Discussion in 'PHP' started by aronsthlm, Mar 3, 2009.

  1. #1
    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!
     
    aronsthlm, Mar 3, 2009 IP
  2. magiatar

    magiatar Active Member

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    73
    #2
    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']} ";
    }
     
    magiatar, Mar 4, 2009 IP
  3. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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):
     
    SmallPotatoes, Mar 4, 2009 IP
  4. aronsthlm

    aronsthlm Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you!

    I'm using the code magiatar gave me, and it works out perfectly.

    SmallPotatoes: Thanks aswell! :)
     
    aronsthlm, Mar 4, 2009 IP
  5. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Magiatar's query will also show the same restaurant as being near itself sometimes. Better to add the:

    and id != {$id}

    to the query.
     
    SmallPotatoes, Mar 4, 2009 IP