mysql_query question

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

mysql_query question

Post 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?
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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");
?>
??????????????????????
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

i did not know you could do auto increment
ill just make my post_id field autoincrement

thanks everyone!!!!!!!!!!
Post Reply