Gibbs Posted January 16, 2007 Share Posted January 16, 2007 Hello,I'm following a tutorial and trying to get the usernames from my database to appear in a drop-down list by using <option>.TBL_USERS is defined (as i'm already using it on the admin page successfully. The original code from the tutorial was:[code]$sql = mysql_query("SELECT * FROM users WHERE id != '$_SESSION[username]' ORDER BY username");[/code]I decided to change it to this:[code]$sql = mysql_query("SELECT * FROM ". TBL_USERS. " WHERE id != $session->username ORDER BY username");[/code]This is the code being used (underneath the above) to fill in the option fields:[code]if (mysql_num_rows($sql) > 0) { while ($x = mysql_fetch_assoc($sql)) echo "<option value=\"$x[id]\">$x[username]</option>\n";} [/code]My admin page has this function which works (and might be able to help):[code]function displayUsers(){ global $database; $q = "SELECT username,userlevel,email,timestamp " ."FROM ".TBL_USERS." ORDER BY userlevel DESC,username"; $result = $database->query($q);[/code]I'm sorry if that's confusing. If I could get any help I would really appreciate it!Thanks Link to comment https://forums.phpfreaks.com/topic/34397-getting-usernames-into-option-fields/ Share on other sites More sharing options...
Cep Posted January 16, 2007 Share Posted January 16, 2007 I dont think you can do this,$session->username within an SQL statement. Link to comment https://forums.phpfreaks.com/topic/34397-getting-usernames-into-option-fields/#findComment-161877 Share on other sites More sharing options...
Gibbs Posted January 16, 2007 Author Share Posted January 16, 2007 [quote author=Cep link=topic=122629.msg505947#msg505947 date=1168949081]I dont think you can do this,$session->username within an SQL statement.[/quote]That is my problem (thanks for pointing it out!).This now works:[code]$sql = mysql_query("SELECT * FROM ". TBL_USERS. " ORDER BY username");[/code]However I need to exclude the logged in users name. This is the part thats not working:[code]WHERE id != ". $session->username ."[/code]When I echo $session->username I get the username. Why won't MySQL read this and exclude it?Thanks for the help so far Link to comment https://forums.phpfreaks.com/topic/34397-getting-usernames-into-option-fields/#findComment-161883 Share on other sites More sharing options...
HuggieBear Posted January 16, 2007 Share Posted January 16, 2007 Just change it to this then...[code]$username = $session->username;$sql = mysql_query("SELECT * FROM " .TBL_USERS. " WHERE id != $username ORDER BY username");[/code]RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/34397-getting-usernames-into-option-fields/#findComment-161890 Share on other sites More sharing options...
HuggieBear Posted January 16, 2007 Share Posted January 16, 2007 The feature to edit has disappeared so I've had to post this below...If it's a varchar rather than an int then don't forget the single quotes around the value e.g.[code]$sql = mysql_query("SELECT * FROM " .TBL_USERS. " WHERE id != '$username' ORDER BY username");[/code]RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/34397-getting-usernames-into-option-fields/#findComment-161891 Share on other sites More sharing options...
Gibbs Posted January 16, 2007 Author Share Posted January 16, 2007 [quote author=HuggieBear link=topic=122629.msg505960#msg505960 date=1168950326]Just change it to this then...[code]$username = $session->username;$sql = mysql_query("SELECT * FROM " .TBL_USERS. " WHERE id != $username ORDER BY username");[/code]RegardsHuggie[/quote]To kill two birds with one stone I have "$from = $session->username;"It doesn't work when using this line:[quote]$sql = mysql_query("SELECT * FROM ". TBL_USERS. " WHERE id != $from ORDER BY username");[/quote]http://vortex-inc.net/pm.php Link to comment https://forums.phpfreaks.com/topic/34397-getting-usernames-into-option-fields/#findComment-161895 Share on other sites More sharing options...
Gibbs Posted January 16, 2007 Author Share Posted January 16, 2007 [quote author=HuggieBear link=topic=122629.msg505961#msg505961 date=1168950415]The feature to edit has disappeared so I've had to post this below...If it's a varchar rather than an int then don't forget the single quotes around the value e.g.[code]$sql = mysql_query("SELECT * FROM " .TBL_USERS. " WHERE id != '$username' ORDER BY username");[/code]RegardsHuggie[/quote]Even with the '' it doesn't work... Link to comment https://forums.phpfreaks.com/topic/34397-getting-usernames-into-option-fields/#findComment-161896 Share on other sites More sharing options...
paul2463 Posted January 16, 2007 Share Posted January 16, 2007 the reason it wont work is that you need single speech marks around a php variable used in an SQL Statement[code]$sql = mysql_query("SELECT * FROM ". TBL_USERS. " WHERE id != '$from' ORDER BY username");[/code]Just Like HuggieBear mentioned in his last post Link to comment https://forums.phpfreaks.com/topic/34397-getting-usernames-into-option-fields/#findComment-161898 Share on other sites More sharing options...
paul2463 Posted January 16, 2007 Share Posted January 16, 2007 it could also be the gaps in your FROM part".TBL_USERS." no gaps Link to comment https://forums.phpfreaks.com/topic/34397-getting-usernames-into-option-fields/#findComment-161901 Share on other sites More sharing options...
Gibbs Posted January 16, 2007 Author Share Posted January 16, 2007 [quote author=paul2463 link=topic=122629.msg505968#msg505968 date=1168950954]the reason it wont work is that you need single speech marks around a php variable used in an SQL Statement[code]$sql = mysql_query("SELECT * FROM ". TBL_USERS. " WHERE id != '$from' ORDER BY username");[/code]Just Like HuggieBear mentioned in his last post[/quote]I am. This is what I have currently:[code]$sql = mysql_query("SELECT * FROM ". TBL_USERS. " WHERE id != '$from' ORDER BY username");[/code]When I echo $from I get the username so I don't think that's the problem... If I take out this section it works, but obviously keeps the username there...[code]"WHERE id != '$from'[/code] Link to comment https://forums.phpfreaks.com/topic/34397-getting-usernames-into-option-fields/#findComment-161904 Share on other sites More sharing options...
HuggieBear Posted January 16, 2007 Share Posted January 16, 2007 OK, report the error then...[code]<?php$sql = "SELECT * FROM " .TBL_USERS. " WHERE id != $from ORDER BY username";$result = mysql_query($sql) or die("Can't execute $sql: " .mysql_error());$x = mysql_fetch_array($result, MYSQL_ASSOC);print_r($x);?>[/code]This will report an error on failure of the query and then dump the contents of the first row in the result set to make sure it has data in it.RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/34397-getting-usernames-into-option-fields/#findComment-161905 Share on other sites More sharing options...
Gibbs Posted January 16, 2007 Author Share Posted January 16, 2007 Removing the spaces doesn't make a difference... Link to comment https://forums.phpfreaks.com/topic/34397-getting-usernames-into-option-fields/#findComment-161907 Share on other sites More sharing options...
Gibbs Posted January 16, 2007 Author Share Posted January 16, 2007 [quote author=HuggieBear link=topic=122629.msg505975#msg505975 date=1168951193]OK, report the error then...[code]<?php$sql = "SELECT * FROM " .TBL_USERS. " WHERE id != $from ORDER BY username";$result = mysql_query($sql) or die("Can't execute $sql: " .mysql_error());$x = mysql_fetch_array($result, MYSQL_ASSOC);print_r($x);?>[/code]This will report an error on failure of the query and then dump the contents of the first row in the result set to make sure it has data in it.RegardsHuggie[/quote]No errors... Link to comment https://forums.phpfreaks.com/topic/34397-getting-usernames-into-option-fields/#findComment-161910 Share on other sites More sharing options...
Gibbs Posted January 16, 2007 Author Share Posted January 16, 2007 I think it's a problem with the variable. Even if I add it just over the SQL code it doesn't help. Screw it, if people are sad enough to send messages to themselves i'll let them :P.Thanks for your help :) Link to comment https://forums.phpfreaks.com/topic/34397-getting-usernames-into-option-fields/#findComment-161914 Share on other sites More sharing options...
Cep Posted January 16, 2007 Share Posted January 16, 2007 Try,[code=php:0]$sql = "SELECT * FROM " .TBL_USERS. " WHERE id NOT LIKE '$from' ORDER BY username";[/code] Link to comment https://forums.phpfreaks.com/topic/34397-getting-usernames-into-option-fields/#findComment-161918 Share on other sites More sharing options...
Cep Posted January 16, 2007 Share Posted January 16, 2007 Actually I just realised your using id as the field in the SQL statement when surely you should be using username?$sql = "SELECT * FROM " .TBL_USERS. " WHERE username != '$from' ORDER BY username"; Link to comment https://forums.phpfreaks.com/topic/34397-getting-usernames-into-option-fields/#findComment-161920 Share on other sites More sharing options...
Gibbs Posted January 16, 2007 Author Share Posted January 16, 2007 [quote author=Cep link=topic=122629.msg505990#msg505990 date=1168952262]Actually I just realised your using id as the field in the SQL statement when surely you should be using username?$sql = "SELECT * FROM " .TBL_USERS. " WHERE username != '$from' ORDER BY username";[/quote]It works! Thanks for everyones help! Link to comment https://forums.phpfreaks.com/topic/34397-getting-usernames-into-option-fields/#findComment-161929 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.