...

View Full Version : selecting data from one table based on data in another



weemee500
10-20-2009, 10:53 AM
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!

Fumigator
10-20-2009, 03:59 PM
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:



$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 Error! Query text: $query<br />Error: " . mysql_error());
}

abduraooft
10-20-2009, 04:03 PM
cross posted http://www.codingforums.com/showthread.php?t=180087 :(

Fumigator
10-20-2009, 04:16 PM
cross posted http://www.codingforums.com/showthread.php?t=180087 :(

Booo.... oh well. :p Hey if you search this forum on "supplied argument is not a valid MySQL result resource" you max out the result count at 300. :thumbsup:

abduraooft
10-20-2009, 04:29 PM
Booo.... oh well. :p Hey if you search this forum on "supplied argument is not a valid MySQL result resource" you max out the result count at 300. :thumbsup:I'm going to add that tip in my sig ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum