sstoveld Posted December 10, 2009 Share Posted December 10, 2009 hey guys, having a minor (i think) problem here that i havent been able to figure out. long story short, im building a hockey stat tracking site for a local hockey league around here. my problem is, i am trying to run a query using php, but the value getting returned is Resource id #4 instead of what it is supposed to be. i read around and found people saying you need to use mysql_fetch_array, which is what i am doing. let me start by posting my code: <?php $querystats = mysql_query('SELECT * FROM stats ORDER BY team_id'); $statsresult = mysql_query($querystats); if (!$statsresult) { exit ('<p>Error in query.'. mysql_error().'</p>'); } while($row = mysql_fetch_array($statsresult)) { echo $row['team_id']; print_r($team_id); $querystandings = mysql_query('SELECT * FROM standings ORDER BY points DESC'); if (!$querystandings) { exit ('<p>Error in query.'. mysql_error().'</p>'); } while ($row = mysql_fetch_array($querystandings)) { echo('<tr><td>' . '<a href="teams.php?team_id='.$row['team_id'].'"> ' . $row['team'] . '</a></td><td>' . $row['games_played'] . '</td><td>' . $row['wins'] . '</td><td>' . $row['losses'] . '</td><td>' . $row['ties'] . '</td><td>' . $row['points'] . '</td></tr>'); } } ?> i believe the problem is the $querystats. here's what my table structure looks like: here's a link to my site: http://gamera.caset.buffalo.edu/~sstoveld/DMS315/projects/final/standings.php you can login with: username: test password: test the problem is on the standings page, if you hover over a team name and look at where the link directs to, it ends with team_id= i need to get the proper team_id in the link, which is where im hoping someone can help me. ive been looking through this trying many different things that i cant seem to get to work. if my above code is completely wrong with using the $querystats query, how can i get this to work. originally the $querystats query wasnt there, and only the $querystandings query was being used, but the links wouldnt work because i am not selecting the team_id from the stats table. when i modify the $querystandings query to left join on stats, the loop goes through and adds about 25 entries per team, which is not what i want. if you need any more info, please ask, i cant figure this out Quote Link to comment https://forums.phpfreaks.com/topic/184612-error-in-query-resource-id-4/ Share on other sites More sharing options...
sstoveld Posted December 10, 2009 Author Share Posted December 10, 2009 sorry for a bump so soon, dont see an option to edit my post... anyway, i edited my code so the standings page on my site doesnt actually show an error in my mysql, but the links still are missing the team_id=# at the end... here's the code i am currently working on: <?php $querystandings = mysql_query('SELECT * FROM standings ORDER BY points DESC'); if (!$querystandings) { exit ('<p>Error in query.'. mysql_error().'</p>'); } while ($row = mysql_fetch_array($querystandings)) { echo('<tr><td>' . '<a href="teams.php?team_id='.$row['team_id'].'"> ' . $row['team'] . '</a></td><td>' . $row['games_played'] . '</td><td>' . $row['wins'] . '</td><td>' . $row['losses'] . '</td><td>' . $row['ties'] . '</td><td>' . $row['points'] . '</td></tr>'); } ?> edit: for some reason, my 2nd post has a modify option, but not the first Quote Link to comment https://forums.phpfreaks.com/topic/184612-error-in-query-resource-id-4/#findComment-974576 Share on other sites More sharing options...
blueman378 Posted December 10, 2009 Share Posted December 10, 2009 change $querystats = mysql_query('SELECT * FROM stats ORDER BY team_id'); $statsresult = mysql_query($querystats); to $querystats = "SELECT * FROM stats ORDER BY team_id"; $statsresult = mysql_query($querystats); Quote Link to comment https://forums.phpfreaks.com/topic/184612-error-in-query-resource-id-4/#findComment-974632 Share on other sites More sharing options...
sstoveld Posted December 10, 2009 Author Share Posted December 10, 2009 that didnt seem to work, the team_id is not getting assigned to the links, and it still lists about many entries for each team (one entry for each player on the team) edit: but it did fix my resource id #4 problem Quote Link to comment https://forums.phpfreaks.com/topic/184612-error-in-query-resource-id-4/#findComment-974773 Share on other sites More sharing options...
premiso Posted December 10, 2009 Share Posted December 10, 2009 You have no column called "team_id" in your standings table, instead you have the team name. You should have the team_id in there instead of the name however. The way you are trying to pull this information is correct, unfortunately your database is not setup that way. You can either modify your table and start inserting the team_id instead of the team name, or you will have to do a separate query to pull the team_id using the team name (I would suggest modifying your database tables to be correct). But here is how you can get the team_id. Given that you have a table called team, the team name column is also called team and the id field is called id. SELECT games_played, wins, losses, ties, points, (SELECT t.id FROM team t WHERE t.team = st.team) AS team_id FROM stats st ORDER BY team_id That is an example query, unsure if it will work but yea. Hopefully it helps you out. Quote Link to comment https://forums.phpfreaks.com/topic/184612-error-in-query-resource-id-4/#findComment-974827 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.