Yammyguy Posted April 1, 2009 Share Posted April 1, 2009 Hello! I'm constantly getting an error with this simple damn query. $sql = mysql_query("SELECT description FROM mag_art_desc ORDER BY ID DESC LIMIT 1"); $project = mysql_fetch_array($sql); echo'<b>Most Recent Entry:</b><br/>'.$project; This is the error I'm getting: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /var/www/article/index.php on line 146 I've also tried replacing mysql_fetch_array() with mysql_fetch_row() with the same result. I don't understand why I'm getting this error. Every other database query on this page is working! I haven't closed the database connection anywhere yet... Is it something simple? Quote Link to comment https://forums.phpfreaks.com/topic/151999-solved-grabbing-a-single-line-from-a-table/ Share on other sites More sharing options...
Maq Posted April 1, 2009 Share Posted April 1, 2009 description may be reserved so I put backticks around it, but the or die clause will let you know what the SQL error is: $sql = mysql_query("SELECT `description` FROM mag_art_desc ORDER BY ID DESC LIMIT 1") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/151999-solved-grabbing-a-single-line-from-a-table/#findComment-798255 Share on other sites More sharing options...
Yammyguy Posted April 1, 2009 Author Share Posted April 1, 2009 Thanks Maq! I completely forgot to add the "OR DIE" portion to the code... I found out the problem... it only seems to work if I have "SELECT * FROM" where as if I put "SELECT (enter column name here) FROM" returns an error? ...Seems kind of in-efficient to return the WHOLE row to an array when I'm just looking for a single value. Is there something I'm missing here? Thanks anyways - I guess I got it working. Quote Link to comment https://forums.phpfreaks.com/topic/151999-solved-grabbing-a-single-line-from-a-table/#findComment-798265 Share on other sites More sharing options...
Maq Posted April 1, 2009 Share Posted April 1, 2009 ...Seems kind of in-efficient to return the WHOLE row to an array when I'm just looking for a single value. Is there something I'm missing here? Yes and Yes. If description is a column in your table then you should get results back without errors. What exactly did the mysql_error say when you put the or die clause in? You're right it is in-efficient. If you don't need ALL columns then only select what you do need. Quote Link to comment https://forums.phpfreaks.com/topic/151999-solved-grabbing-a-single-line-from-a-table/#findComment-798268 Share on other sites More sharing options...
Yammyguy Posted April 1, 2009 Author Share Posted April 1, 2009 Yes and Yes. If description is a column in your table then you should get results back without errors. What exactly did the mysql_error say when you put the or die clause in? You're right it is in-efficient. If you don't need ALL columns then only select what you do need. description IS a column. here is the error I received: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'description FROM mag_art_desc ORDER BY ID DESC LIMIT 1' at line 1 I also need to ask one question... did you have any professional training with PHP? How do you guys learn so much, and understand the syntax, and know what goes where?? I feel like no matter how much I learn, I just can't seem to get these apps to do what I really want them to do... :-\ Quote Link to comment https://forums.phpfreaks.com/topic/151999-solved-grabbing-a-single-line-from-a-table/#findComment-798275 Share on other sites More sharing options...
Maq Posted April 1, 2009 Share Posted April 1, 2009 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'description FROM mag_art_desc ORDER BY ID DESC LIMIT 1' at line 1 Hmm, I'm not really sure then. Because description IS NOT a reserved word so that's not the problem. Maybe you have to select what you order by. If * works than just use that. I also need to ask one question... did you have any professional training with PHP? How do you guys learn so much, and understand the syntax, and know what goes where?? I feel like no matter how much I learn, I just can't seem to get these apps to do what I really want them to do... :-\ No professional training. I did graduate from a university with a BS in computer science and did an internship that entailed mostly PHP. To tell you the truth, I learned most of my PHP from this site Quote Link to comment https://forums.phpfreaks.com/topic/151999-solved-grabbing-a-single-line-from-a-table/#findComment-798282 Share on other sites More sharing options...
Yammyguy Posted April 1, 2009 Author Share Posted April 1, 2009 Well thanks again for your help! Quote Link to comment https://forums.phpfreaks.com/topic/151999-solved-grabbing-a-single-line-from-a-table/#findComment-798301 Share on other sites More sharing options...
Maq Posted April 1, 2009 Share Posted April 1, 2009 Well thanks again for your help! Sure no problem. Please mark as [sOLVED] Quote Link to comment https://forums.phpfreaks.com/topic/151999-solved-grabbing-a-single-line-from-a-table/#findComment-798306 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.