badeand Posted July 27, 2012 Share Posted July 27, 2012 This is a part of my Mysql query and getting the values from the rows wheres tasks.owner.id = logged in user so it only shows the tasks assigned to the logged in user. This is a task manager script i?m trying to make, so i want it to echo out who made this task by name, and the ID of the user that made it is stored in $made_id in table "tasks" and i need this to get the name (row) with the same id in the "users" table. How could i do this? As i see it, the query already contains booth the ID and the Name of the users, but i need to put theese to together in some way. $query = mysql_query("SELECT * FROM tasks JOIN login_users WHERE tasks.owner_id = '$user' GROUP BY tasks.id "); if ( mysql_num_rows( $query ) > 0 ) { while ($row = mysql_fetch_array($query)){ $id = $row['id']; $title = $row['title']; $details = $row['details']; $yourref = $row['yourref']; $made_id = $row['made_id']; $made_date = $row['made_date']; $due_date = $row['due_date']; $name = $row['name']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/266332-get-name-by-id-from-other-table/ Share on other sites More sharing options...
peipst9lker Posted July 27, 2012 Share Posted July 27, 2012 SELECT *, b.name FROM tasks AS a LEFT JOIN login_users AS b ON a.owner_id=b.id WHERE a.owner_id = '$user' GROUP BY a.id Quote Link to comment https://forums.phpfreaks.com/topic/266332-get-name-by-id-from-other-table/#findComment-1364823 Share on other sites More sharing options...
badeand Posted July 27, 2012 Author Share Posted July 27, 2012 If i?m using that it print out an error Quote Link to comment https://forums.phpfreaks.com/topic/266332-get-name-by-id-from-other-table/#findComment-1364827 Share on other sites More sharing options...
peipst9lker Posted July 27, 2012 Share Posted July 27, 2012 That was helpful! Maybe you can tell us that error? Have you adjusted the query? Are the table/column names right? I don't know your structures and have just prototyped something out. Quote Link to comment https://forums.phpfreaks.com/topic/266332-get-name-by-id-from-other-table/#findComment-1364830 Share on other sites More sharing options...
Barand Posted July 27, 2012 Share Posted July 27, 2012 If you are already selecting *, why select b.name again? Why the GROUP BY? Quote Link to comment https://forums.phpfreaks.com/topic/266332-get-name-by-id-from-other-table/#findComment-1364832 Share on other sites More sharing options...
peipst9lker Posted July 27, 2012 Share Posted July 27, 2012 Oops forgot to put an "a." infront of it, the GROUP BY is a little non-sense I just copied it from badeand. Quote Link to comment https://forums.phpfreaks.com/topic/266332-get-name-by-id-from-other-table/#findComment-1364833 Share on other sites More sharing options...
badeand Posted July 27, 2012 Author Share Posted July 27, 2012 Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/home.php on line 36 $query = mysql_query("SELECT *, b.name FROM tasks AS a LEFT JOIN login_users AS b ON a.owner_id=b.id WHERE a.owner_id = '$user' GROUP BY a.id"); The table names are correct. Should it be: ON a.made_id=b.id Since it`s the made_id that i?m trying to link up with the id on the users table? But this query failed as well.. Quote Link to comment https://forums.phpfreaks.com/topic/266332-get-name-by-id-from-other-table/#findComment-1364834 Share on other sites More sharing options...
cyberRobot Posted July 27, 2012 Share Posted July 27, 2012 Have you tried adding mysql_error() after the query is executed? http://php.net/manual/en/function.mysql-error.php Quote Link to comment https://forums.phpfreaks.com/topic/266332-get-name-by-id-from-other-table/#findComment-1364837 Share on other sites More sharing options...
peipst9lker Posted July 27, 2012 Share Posted July 27, 2012 No, "a.owner_id" should be the column in the tasks-table. Quote Link to comment https://forums.phpfreaks.com/topic/266332-get-name-by-id-from-other-table/#findComment-1364844 Share on other sites More sharing options...
badeand Posted July 27, 2012 Author Share Posted July 27, 2012 Ok, so now it?s finaly returning results, and correct results. Solved $query = mysql_query("SELECT *, b.name FROM tasks AS a LEFT JOIN login_users AS b ON a.made_id=b.user_id WHERE a.owner_id = '$user'"); Works fine! Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/266332-get-name-by-id-from-other-table/#findComment-1364847 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.