ghurty Posted January 18, 2010 Share Posted January 18, 2010 I am trying to have a sql command pull up the "name" value for a "group" number of the same record. I have a table has both fields. $queryid = "SELECT 'name' FROM `users` WHERE `group` = " . $GROUP; $resultid = mysql_query($queryid) or die("Web site query failed"); if ($debug) : fputs($stdlog, "Name has been set to=". $resultid. "\n" ); endif ; For some reason the debug is showing that it has been set to "resource #10". What should I change? Thank You Quote Link to comment https://forums.phpfreaks.com/topic/188863-why-is-this-query-giving-me-a-value-of-resource-10/ Share on other sites More sharing options...
trq Posted January 18, 2010 Share Posted January 18, 2010 The code is giving the expected result. mysql_query returns a result resource. You'll likely want to pass it to mysql_fetch_assoc (or similar) if you actually want to get to the data itself. Quote Link to comment https://forums.phpfreaks.com/topic/188863-why-is-this-query-giving-me-a-value-of-resource-10/#findComment-997101 Share on other sites More sharing options...
ghurty Posted January 18, 2010 Author Share Posted January 18, 2010 I just need the value that is in the "name" field. How would I rewrite it to get that value? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/188863-why-is-this-query-giving-me-a-value-of-resource-10/#findComment-997103 Share on other sites More sharing options...
oni-kun Posted January 18, 2010 Share Posted January 18, 2010 I just need the value that is in the "name" field. How would I rewrite it to get that value? Thanks while ($row = mysql_fetch_assoc($resultid)) { echo $row['name']; } Quote Link to comment https://forums.phpfreaks.com/topic/188863-why-is-this-query-giving-me-a-value-of-resource-10/#findComment-997108 Share on other sites More sharing options...
trq Posted January 18, 2010 Share Posted January 18, 2010 oni-kun's example is good, but you really don't want / need the loop. Quote Link to comment https://forums.phpfreaks.com/topic/188863-why-is-this-query-giving-me-a-value-of-resource-10/#findComment-997109 Share on other sites More sharing options...
ghurty Posted January 18, 2010 Author Share Posted January 18, 2010 I will try to use oni-kun's example, but is there a better way? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/188863-why-is-this-query-giving-me-a-value-of-resource-10/#findComment-997111 Share on other sites More sharing options...
oni-kun Posted January 18, 2010 Share Posted January 18, 2010 I will try to use oni-kun's example, but is there a better way? Thanks Oh yes, I believe you can just do this: fputs($stdlog, "Name has been set to=". mysql_fetch_row($resultid) . "\n" ); Quote Link to comment https://forums.phpfreaks.com/topic/188863-why-is-this-query-giving-me-a-value-of-resource-10/#findComment-997117 Share on other sites More sharing options...
trq Posted January 18, 2010 Share Posted January 18, 2010 $queryid = "SELECT 'name' FROM `users` WHERE `group` = " . $GROUP; $resultid = mysql_query($queryid) or die("Web site query failed"); $row = mysql_fetch_assoc($resultid); if ($debug) : fputs($stdlog, "Name has been set to={$rwo['name']}\n" ); } Quote Link to comment https://forums.phpfreaks.com/topic/188863-why-is-this-query-giving-me-a-value-of-resource-10/#findComment-997118 Share on other sites More sharing options...
Buddski Posted January 18, 2010 Share Posted January 18, 2010 You will need to change the query for it to work as expected.. $queryid = "SELECT 'name' FROM `users` WHERE `group` = " . $GROUP; to $queryid = "SELECT `name` FROM `users` WHERE `group` = " . $GROUP; Quote Link to comment https://forums.phpfreaks.com/topic/188863-why-is-this-query-giving-me-a-value-of-resource-10/#findComment-997121 Share on other sites More sharing options...
ghurty Posted January 19, 2010 Author Share Posted January 19, 2010 When I have the code like: $queryid = "SELECT `name` FROM `users` WHERE `group` = " . $GROUP; $resultid = mysql_query($queryid) or die("Web site query failed"); $row = mysql_fetch_assoc($resultid); if ($debug) : fputs($stdlog, "Name has been set to={$row[`name`]}\n" ); } It locks up and doesnt process it. It appears the problem is this line: fputs($stdlog, "Name has been set to={$row[`name`]}\n" ); Because when I remove it, it does complete the script. When I have: fputs($stdlog, "Name has been set to= $row\n" ); It displays: Name has been set to=Array Thank You Quote Link to comment https://forums.phpfreaks.com/topic/188863-why-is-this-query-giving-me-a-value-of-resource-10/#findComment-997804 Share on other sites More sharing options...
trq Posted January 19, 2010 Share Posted January 19, 2010 You using backticks in place of single quotes. fputs($stdlog, "Name has been set to={$row['name']}\n" ); Quote Link to comment https://forums.phpfreaks.com/topic/188863-why-is-this-query-giving-me-a-value-of-resource-10/#findComment-997810 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.