Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Joining querys?

  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    97
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Joining querys?

    Hello, i was just wondering if theres a way to join a query? like this

    "SELECT `id` FROM `users` WHERE `username` = '$user' AND SELECT `id` from `films` WHERE `title` = '$new_row['last_movie']";

    something like this?

    Thanks

    Kevin

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    That won't work.
    What is it you're trying to accomplish, are the tables related to each other are they completely independant?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Do you have any field relating your two tables?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    97
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Well what it is is that im doing two query's, and ive been told it's unsafe and i was just wondering if there's an easier way there all on the same database but different tables


    PHP Code:
    <hr color='silver' width='100%'/>
    <b>Last Movie:</b> <a href="#"><?php    

        $id 
    $_GET['id'];
        
        
    $new "SELECT * FROM `users` WHERE `id` = '".$id."' ORDER BY '".$id."' DESC LIMIT 10";
        
    $new_res mysql_query($new) or die(mysql_error());
        
    $new_row mysql_fetch_assoc($new_res);
        
        
    $imov "SELECT `id` FROM `films` WHERE `title` = '".$new_row['last_movie_id']."'";
        
    $imov_res mysql_query($imov) or die(mysql_error());
        
    $imov_row mysql_fetch_array($imov_res);

        

        echo 
    '<a href="preview.php?id='$imov_row['id'] ,'">',$new_row['last_movie_id'],'</a>';

        
    ?></a>

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The 'unsafe' part is you're $id variable.
    The tables are however related where films.title = users.last_movie_id.
    PHP Code:
    // I assumed $id is a string since its string quoted in the query:
    if (@get_magic_quotes_gpc())
    {
        
    $_GET['id'] = stripslashes($_GET['id']);
    }
    $id mysql_real_escape_string($_GET['id']);
    $sQry 'SELECT u.last_movie_id, f.id FROM users u
                    LEFT JOIN
                    films f on (f.title = u.last_movie_id)
                    WHERE u.id = \'' 
    $id '\'
                    ORDER BY u.id DESC'
    ;
                    
    //LIMIT 10'; // This limit here is unecessary, since I would assume that last_movie_id has a 0 - 1 multiplicity

    // edit: I guess I should add the rest
    $obQry = @mysql_query($sQry) or die(mysql_error());
    if (
    mysql_num_rows($obQry) == 1// This should be true so long as the user can only have one last movie
    {
        
    $result mysql_fetch_assoc($obQry);
        
    printf('<a href="preview.php?id=%s">%s</a>'$result['id'], $result['last_movie_id']);

    Last edited by Fou-Lu; 03-26-2009 at 05:47 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •