Jump to content

[SOLVED] grabbing a single line from a table..


Yammyguy

Recommended Posts

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?

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());

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. :)

...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.

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...  :-\

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  ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.