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
  1. #1
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    selecting data from one table based on data in another

    Hi, I am currently trying to make a part for my user driven website where one user can subscribe to another and whoever they have subscribed to is echoed back on there profile page.

    my users table structure is: id username password and my subscription table is: id user_id follow_id

    i am currently trying to query the logged in users subscriptions with this sql_query

    $subsQuery = mysql_query("SELECT U.username FROM users AS U INNER JOIN subscription AS Sub.follow_id=U.id WHERE Sub.user_id=".$ID); $ID being the logged in users id.

    However when trying to echo this on the page using this bit of php:

    <? while ($subs = mysql_fetch_assoc($subsQuery)) { ?><br /><br />
    <? echo $subs['username'] ?>

    <? } ?>

    it throws back this error:

    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/avince/public_html/users/user.php on line 53

    Any help would be much appreciated!

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    What that error is telling you, is you are trying to fetch a result row from a query that isn't a valid query resultset. This means your query did not work.

    To avoid getting this error message, you need to catch the query error. In fact, you should always write your code with good error catching. It's a simple thing to do and gives you as a programmer the ability to quickly troubleshoot problems.

    In this case, I recommend assigning the text of the query to a variable. This way, you can echo that variable when there is a problem. (You should actually write a function or class that performs the query and catches the error so you don't have to write the same code dozens of times. This has an additional benefit-- in a production environment you don't want to be echoing details of your database setup to the browser; you want to redirect errors to an error log only you can see, and if your query error catching resides in just a single function, it's a simple matter to redirect all errors to a log.)

    Now then... here's a simple way to catch query errors:

    PHP Code:
    $query "SELECT U.username FROM users AS U INNER JOIN subscription AS Sub.follow_id=U.id WHERE Sub.user_id=".$ID;

    $result mysql_query($query);
    if (!
    $result) {
        die(
    Query ErrorQuery text$query<br />Error" . mysql_error());


  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Quote Originally Posted by abduraooft View Post
    Booo.... oh well. Hey if you search this forum on "supplied argument is not a valid MySQL result resource" you max out the result count at 300.

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Quote Originally Posted by Fumigator View Post
    Booo.... oh well. Hey if you search this forum on "supplied argument is not a valid MySQL result resource" you max out the result count at 300.
    I'm going to add that tip in my sig
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

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