WeirdMystery Posted September 7, 2009 Share Posted September 7, 2009 Hello I'm working on my own site with user pages, right now I'm working on descriptions for each user. $te = "SELECT 'desc' FROM `front_user` " ."WHERE `id`='".$_GET["id"]."' "; $le = mysql_query($te); while ($row = mysql_fetch_assoc($le)) { echo $row["desc"]; That only prints out "desc", I want it to print out the value of "desc". Such a user id as 22 and his description is "hello" I want it to print hello. I don't know what I'm doing wrong... Quote Link to comment https://forums.phpfreaks.com/topic/173433-solved-echos-out-row-name-not-value/ Share on other sites More sharing options...
RussellReal Posted September 7, 2009 Share Posted September 7, 2009 I am not COMPLETELY sure.. but I'd guess that because you're selecting 'desc' instead of desc or `desc` you're selecting a string rather than a field.. either that or that user's description is 'desc' because I read somewhere mysql supports single quotes but that again is not really solid information from me try removing the ' ' around desc in the select clause Quote Link to comment https://forums.phpfreaks.com/topic/173433-solved-echos-out-row-name-not-value/#findComment-914244 Share on other sites More sharing options...
WeirdMystery Posted September 7, 2009 Author Share Posted September 7, 2009 Wait there is that slanted ' I never knew that . Can you tell me how to do that? IT may be that after all. Well now nothing echos out... Quote Link to comment https://forums.phpfreaks.com/topic/173433-solved-echos-out-row-name-not-value/#findComment-914245 Share on other sites More sharing options...
WeirdMystery Posted September 7, 2009 Author Share Posted September 7, 2009 Now nothing echos out, I'm think you got me ont he right track... Quote Link to comment https://forums.phpfreaks.com/topic/173433-solved-echos-out-row-name-not-value/#findComment-914248 Share on other sites More sharing options...
gevans Posted September 7, 2009 Share Posted September 7, 2009 It's usually to the left of your number 1; $te = "SELECT `'desc`' FROM `front_user` WHERE `id`='".$_GET["id"]."' "; Quote Link to comment https://forums.phpfreaks.com/topic/173433-solved-echos-out-row-name-not-value/#findComment-914249 Share on other sites More sharing options...
Mark Baker Posted September 7, 2009 Share Posted September 7, 2009 DESC is a SQL keyword, so it needs to b quoted with backticks if used as a column or table name: $te = "SELECT `desc` FROM `front_user` WHERE `id`='".$_GET["id"]."' Quote Link to comment https://forums.phpfreaks.com/topic/173433-solved-echos-out-row-name-not-value/#findComment-914250 Share on other sites More sharing options...
PFMaBiSmAd Posted September 7, 2009 Share Posted September 7, 2009 Or since you should in general avoid using reserved keywords as column names - desc is a reserved keyword and should not be used as a column name. If you do use it as a column name it requires special handling everywhere you use it. In mysql, you can use the back-tick ` to force reserved keywords and other things that are not normally permitted as column names to be used. Since the back-tick is mysql specific, it should not be used if you intend your code to be usable on a different sql server. Quote Link to comment https://forums.phpfreaks.com/topic/173433-solved-echos-out-row-name-not-value/#findComment-914252 Share on other sites More sharing options...
WeirdMystery Posted September 7, 2009 Author Share Posted September 7, 2009 Changed the name to des. Still prints out nothing... Here is the whole code. <?php include "dbconfig.php"; if ($_GET["id"]) { $q = "SELECT * FROM `front_user` " ."WHERE `id`='".$_GET["id"]."' "; $r = mysql_query($q); if ( $obj = @mysql_fetch_object($r) ) { $te = "SELECT `des` FROM `front_user` " ."WHERE `id`='".$_GET["id"]."' "; $le = mysql_query($te); echo $row[des]; } else { echo "Error: ID doesn't exist."; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/173433-solved-echos-out-row-name-not-value/#findComment-914255 Share on other sites More sharing options...
PFMaBiSmAd Posted September 7, 2009 Share Posted September 7, 2009 Well, you removed the line of code that is fetching a row from the result set - while ($row = mysql_fetch_assoc($le)) { Are you learning php, developing php code, and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON in your php.ini to get php to help you? There would have been an error about $row being undefined that would have helped you possibly troubleshoot your code yourself. Also, why are executing one query to retrieve all the columns (the *) in any matching rows, then if that query finds a row, execute a query again to retrieve only the des column, when the first query, when it succeeds has already retrieved the des column along with all the other columns? Remove the @ from in front of mysql_fetch_object($r). That just hides errors that would help you find out why your code is not working. And to find if a SELECT query worked or not you use an if() test to see if it returned a FALSE value or a result resource. And to find how many rows a SELECT query returned, you use mysql_num_rows() (after you have determined that the query worked.) Quote Link to comment https://forums.phpfreaks.com/topic/173433-solved-echos-out-row-name-not-value/#findComment-914262 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.