Page 1 of 1

mysql_query question

Posted: Wed Dec 17, 2003 6:50 pm
by dull1554
ok, i'm trying to write a script that uses mysql......i need to chesk the field "post_id", but i want it to check the value of "post_id" at the bottom or last row

heres what i know how to do

Code: Select all

mysql_query("SELECT post_id FROM news WHERE i don't know where to select from");
does anyone know how to do this, or a better way of doing what i want to do?

Posted: Wed Dec 17, 2003 6:55 pm
by qads

Code: Select all

<?php
mysql_query("SELECT post_id FROM news WHERE 1 ORDER BY ID DESC limit 1");
?>
hope you have a ID field in your table.

Posted: Wed Dec 17, 2003 7:03 pm
by dull1554
so wait, i have to add another field to my news table, then what would i do put the same number in that as i do in the "post_id" i started it at one, and what i want to do this for is to see how many posts there are and then make the new post's "post_id will be one greater then the one before, heres my table setup......

post_id | subject | date | post_text

i can add another field if i have to but i dont really want to, could i not just do

Code: Select all

<?php
mysql_query("SELECT post_id FROM news WHERE 1 ORDER BY post_id DESC limit 1");
?>
??????????????????????

Posted: Wed Dec 17, 2003 7:12 pm
by Weirdan
if you need the count of the post you'd better use [mysql_man]COUNT[/mysql_man] sql function:

Code: Select all

select count(post_id) from news
if you need to
dull1554 wrote: [...]make the new post's "post_id will be one greater then the one before[...]
you'd better set your table as follows:

Code: Select all

create table news (post_id int auto_increment, subject varchar(128), date datetime,  post_text text,primary key(post_id))
then you can add your news:

Code: Select all

insert into news values(null,'subject here', null, 'looooong text here...')
nulls will become actual id and dateinsert on insertion automatically.

Posted: Wed Dec 17, 2003 7:13 pm
by qads
hmm...you dont want to add another field? why? it wont bite you.

all you need is a ID field, so it becomes

ID | post_id | subject | date | post_text

the ID field needs to be INT, with auto_increment. with this, you dont need find the total number of rows and then add 1 for post_id, beacuse ID will do this for you.

you can just change post_id for this, it doest HAVE to be called ID.

just make the post_id field into
INT, primary, auto_increment.

thats it.

Posted: Wed Dec 17, 2003 7:25 pm
by dull1554
i did not know you could do auto increment
ill just make my post_id field autoincrement

thanks everyone!!!!!!!!!!