Re: [PHP-DB] finding ID's

2002-02-18 Thread DL Neil

Ok Jonathan,
[I've put the answer back on the list - there are others who can help, and may well 
get back to you faster than
I]

> Ok I get the auto_increment stuff... Just one more question. Let's say I
> want to show 25 rows per page. I want the script to generate links to
> the next page, and so on. How do I make it show from the highest ID to
> 25 rows down? There is no auto_decrement Here's an example:
>
> ID
>
> 1
> 4
> 5
> 8
> 10
> 11
> 12
> 15
> 17
> 18
>
>
> I want to show the 5 newest rows (have the highest ID's).


AUTO_INCREMENT (and your 'AUTO_DECREMENT) only has relevance when a row is being added 
to the table. When a
SELECT is performed, the value is returned, fixed, and numeric, just as if it were any 
other integer and/or one
that you had loaded as a literal value.

It does not have any effect on SELECTs. Thus if you SELECT * FROM tblNm, you will not 
necessarily retrieve the
rows in ascending sequence of the id field! Did you already realise this? To retrieve 
the data 'in order' you
will need to add an extra clause:

SELECT * FROM tblNm ORDER BY id;


That said, perhaps you have two questions here:

- sequence the results of a SELECT in inverse order to the AUTO_INCREMENT/id field 
value by using the ORDER BY
id DESC. [RTFM: 3.3.4.4  Sorting Rows] BTW Chapter 3 of the manual is a useful 
tutorial/user guide that covers
many of these topics/offers a functional introduction.

- how can a long resultset be split up and displayed on several web pages using a 
PREV/NEXT page link facility?
This answer revolves around the LIMIT clause [RTFM: 6.4.1  SELECT Syntax]

SELECT select_expression,...
  [LIMIT [offset,] rows]

The first page is populated with a query saying LIMIT 0, 25, and the next LIMIT 25, 50 
(or that 24 and 49?)
BTW I was working off your earlier quote of "25" cf the later of "5"

This combination of HTML, PHP, and MySQL is not discussed in the MySQL manual, but 
there are a number of
tutorial articles available on the various PHP support sites. If you need to get 
started/make contacts, visit
the MySQL and/or PHP home pages and follow their links, or Google-it. There are also 
some prepared
classes/scripts available for the downloading which will save you the coding time 
(unless you want to learn for
yourself). You will also find this in any book covering PHP and MySQL - a most 
worthwhile investment if you're
going to 'walk amongst us' for a while!

Regards,
=dn



> > (comments interposed, below)
> >
> >> Try using something like
> >> $getlist = mysql_query("SELECT id FROM yourdb",$dbconnectetc);
> >> $numrows = mysql_num_rows($getlist);
> >> echo "$numrows\n";
> >>
> >> This will give you the number of records in your db.
> >> If you use autoincrement for the id field your will not have to
> >> worry about your ++.
> >>
> >> If you delete any records numrows will always be correct.
> >
> > =this is correct, but COUNT(*) is optimised/more efficient (RTFM:
> > 6.3.7  Functions for Use with GROUP BY
> > Clauses)
> >
> >> 
> >> First off, sorry for the newbie question... :(.
> >>
> >> I want to be able to query the database and find the record with the
> >> highest ID value. Example... each row ideally has an incremented
> >> integer
> >> ID (1, 2, 3, 4...) but I am running into problems when I try and delete
> >> a row (let's say row 2). My PHP currently selects all of the rows and
> >> formulates the ID off of that... This I found out is bad because when I
> >> delete row 2 the query says there are 3 rows so my PHP will try to make
> >> the ID = 4. I just need the code to find the highest ID so I can ++ it.
> >> Sorry again for the easy question!
> >
> > =no need to apologise, we all have to start somewhere.
> >
> > =It is an FAQ. By asking it you indicate that you don't (yet)
> > understand the philosophy of the AUTO_INCREMENT
> > facility. It is there to provide an ID for new rows of data, not to be
> > a 'count' of the rows. If you consider
> > that this ID may be used as a key into this table's data from another
> > table (foreign key), then you will realise
> > that changing ID values to reflect intermediate deletions is less than
> > logical. (RTFM: 3.5.9  Using
> > AUTO_INCREMENT, although it doesn't seem to get into this point - the
> > annotated comments are worth a read
> > though). I've just a had a quick look to see where the manual discusses
> > the 'philosophy' and have come up
> > empty - perhaps someone else can steer you right, if you need more.
> >
> > =It is worth reading through PHP's large collection of built-in MySQL_
> > functions. There are specific functions
> > that will return various 'numbers of rows' to the script to suit
> > various situations.



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP-DB] finding ID's

2002-02-17 Thread DL Neil

Hi Jon, and Howard,
(comments interposed, below)

> Try using something like
> $getlist = mysql_query("SELECT id FROM yourdb",$dbconnectetc);
> $numrows = mysql_num_rows($getlist);
> echo "$numrows\n";
>
> This will give you the number of records in your db.
> If you use autoincrement for the id field your will not have to
> worry about your ++.
>
> If you delete any records numrows will always be correct.

=this is correct, but COUNT(*) is optimised/more efficient (RTFM: 6.3.7  Functions for 
Use with GROUP BY
Clauses)

> 
> First off, sorry for the newbie question... :(.
>
> I want to be able to query the database and find the record with the
> highest ID value. Example... each row ideally has an incremented integer
> ID (1, 2, 3, 4...) but I am running into problems when I try and delete
> a row (let's say row 2). My PHP currently selects all of the rows and
> formulates the ID off of that... This I found out is bad because when I
> delete row 2 the query says there are 3 rows so my PHP will try to make
> the ID = 4. I just need the code to find the highest ID so I can ++ it.
> Sorry again for the easy question!

=no need to apologise, we all have to start somewhere.

=It is an FAQ. By asking it you indicate that you don't (yet) understand the 
philosophy of the AUTO_INCREMENT
facility. It is there to provide an ID for new rows of data, not to be a 'count' of 
the rows. If you consider
that this ID may be used as a key into this table's data from another table (foreign 
key), then you will realise
that changing ID values to reflect intermediate deletions is less than logical. (RTFM: 
3.5.9  Using
AUTO_INCREMENT, although it doesn't seem to get into this point - the annotated 
comments are worth a read
though). I've just a had a quick look to see where the manual discusses the 
'philosophy' and have come up
empty - perhaps someone else can steer you right, if you need more.

=It is worth reading through PHP's large collection of built-in MySQL_ functions. 
There are specific functions
that will return various 'numbers of rows' to the script to suit various situations.

=Regards,
=dn


=Regards,
=dn



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP-DB] finding ID's

2002-02-17 Thread Howard Picken

Hi Jon

Try using something like 

$getlist = mysql_query("SELECT id FROM yourdb",$dbconnectetc);
$numrows = mysql_num_rows($getlist);
echo "$numrows\n";

This will give you the number of records in your db.
If you use autoincrement for the id field your will not have to 
worry about your ++.

If you delete any records numrows will always be correct.

HTH

Howard


First off, sorry for the newbie question... :(.

I want to be able to query the database and find the record with the 
highest ID value. Example... each row ideally has an incremented integer 
ID (1, 2, 3, 4...) but I am running into problems when I try and delete 
a row (let's say row 2). My PHP currently selects all of the rows and 
formulates the ID off of that... This I found out is bad because when I 
delete row 2 the query says there are 3 rows so my PHP will try to make 
the ID = 4. I just need the code to find the highest ID so I can ++ it. 
Sorry again for the easy question!

-Jon Gales
http://www.macmerc.com



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php