ecopetition Posted July 25, 2008 Share Posted July 25, 2008 Hello. I'm trying to get row data from a cell in a table in a MySQL database based on information I have about one cell in the same row in the same table (but a different column). I have data about one cell in the table and want to retrieve the data from another cell in a different column of the same row by specifying the name of the column I want to get the data from (or by any alternative). Can anyone please help me with this if they're not too confused? Thank you Peter Quote Link to comment https://forums.phpfreaks.com/topic/116579-solved-getting-data-from-a-cell-in-a-mysql-database/ Share on other sites More sharing options...
JonnoTheDev Posted July 25, 2008 Share Posted July 25, 2008 That is confusing! But it seems to me that all that is required is a simple SELECT query: SELECT fieldRequired1, fieldRequired2 FROM tableName WHERE fieldYouHaveValueOf='X' Quote Link to comment https://forums.phpfreaks.com/topic/116579-solved-getting-data-from-a-cell-in-a-mysql-database/#findComment-599442 Share on other sites More sharing options...
ecopetition Posted July 25, 2008 Author Share Posted July 25, 2008 Yeah I tried that type of code: (this is from a forum software) $sql = "SELECT group_id FROM group_members WHERE user_id = ". $config['last_reg_user_id'] ." "; $result = $db->query($sql); while($row = $db->fetch_assoc($result)) { $newest_user_group[] = $row; } But it doesn't work. It returns an array. Quote Link to comment https://forums.phpfreaks.com/topic/116579-solved-getting-data-from-a-cell-in-a-mysql-database/#findComment-599458 Share on other sites More sharing options...
MFHJoe Posted July 25, 2008 Share Posted July 25, 2008 $sql = "SELECT group_id FROM group_members WHERE user_id = ". $config['last_reg_user_id'] ." "; $result = $db->query($sql); while($row = $db->fetch_assoc($result)) { $newest_user_group = $row['group_id']; } The above code should assign $newest_user_group the value of group_id you selected from the group_members table. I'm presuming that's what you wanted to do. Quote Link to comment https://forums.phpfreaks.com/topic/116579-solved-getting-data-from-a-cell-in-a-mysql-database/#findComment-599460 Share on other sites More sharing options...
JonnoTheDev Posted July 25, 2008 Share Posted July 25, 2008 Does this array value contain any data $config['last_reg_user_id']? If not it will return no results. Your query is certainly gramatically correct. Try printing the $sql variable to the screen first to check Quote Link to comment https://forums.phpfreaks.com/topic/116579-solved-getting-data-from-a-cell-in-a-mysql-database/#findComment-599462 Share on other sites More sharing options...
ecopetition Posted July 25, 2008 Author Share Posted July 25, 2008 Yes it should, but on printing it returns the text "Array". neil.johnson: $config['last_reg_user_id'] does indeed have a value, in this case it is the number 27. Quote Link to comment https://forums.phpfreaks.com/topic/116579-solved-getting-data-from-a-cell-in-a-mysql-database/#findComment-599464 Share on other sites More sharing options...
JonnoTheDev Posted July 25, 2008 Share Posted July 25, 2008 OK so your query is indeed returning values. When your are returning the query data in the form of $row through a loop you are adding to the $newest_user_group array: $newest_user_group[] = $row; To see what the array contains use after the loop: print_r($newest_user_group); This will be the field values in the database records that you returned with your query Quote Link to comment https://forums.phpfreaks.com/topic/116579-solved-getting-data-from-a-cell-in-a-mysql-database/#findComment-599466 Share on other sites More sharing options...
ecopetition Posted July 25, 2008 Author Share Posted July 25, 2008 The code returned now is Array ( [0] => Array ( [primary_group] => 3 ) ) The number "3" in the end there is the number I need but it seems to be hiding. Quote Link to comment https://forums.phpfreaks.com/topic/116579-solved-getting-data-from-a-cell-in-a-mysql-database/#findComment-599468 Share on other sites More sharing options...
JonnoTheDev Posted July 25, 2008 Share Posted July 25, 2008 If there should be only 1 record in the database for the value you are adding into the query then use this code: $result = $db->query("SELECT group_id FROM group_members WHERE user_id = ".$config['last_reg_user_id']." LIMIT 1"); $row = $db->fetch_assoc($result); if(is_numeric($row['group_id'])) { print "found group id: ".$row['group_id']; } else { print "no record found"; } Quote Link to comment https://forums.phpfreaks.com/topic/116579-solved-getting-data-from-a-cell-in-a-mysql-database/#findComment-599470 Share on other sites More sharing options...
MFHJoe Posted July 25, 2008 Share Posted July 25, 2008 The code returned now is Array ( [0] => Array ( [primary_group] => 3 ) ) The number "3" in the end there is the number I need but it seems to be hiding. $newest_user_group[0]['primary_group']; Should be the correct reference then. Quote Link to comment https://forums.phpfreaks.com/topic/116579-solved-getting-data-from-a-cell-in-a-mysql-database/#findComment-599472 Share on other sites More sharing options...
ecopetition Posted July 25, 2008 Author Share Posted July 25, 2008 Ok great got it - thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/116579-solved-getting-data-from-a-cell-in-a-mysql-database/#findComment-599476 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.