473,387 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

MySQL/PHP problem

Jim
I'm using PHP & MySQL to create a simple guestbook. I've created my
table and I'm able to load my information in as usual. I would like it
to display the latest entry first though. I set an id to each entry
that is auto-incremented. The idea seems real easy in theory:

1. Create a loop that starts at the last entry and goes until it
finishes the first.
2. Each time around display the entire entry.

My question is, how do I find the last entry if I don't know what it
is? For example, right now I have three entries and I can display them
starting with the first. I know there are three so I set my counter to
start at 0 and end on 2 (0,1,2) now if I want to reverse it and start
with the last, how do I get the last id?

Any help would be appreciated.
--
Cheers,

Jim
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 17 '05 #1
7 3161
*Jim wrote:
I'm using PHP & MySQL to create a simple guestbook. I've created my
table and I'm able to load my information in as usual. I would like it
to display the latest entry first though. I set an id to each entry
that is auto-incremented. The idea seems real easy in theory:

1. Create a loop that starts at the last entry and goes until it
finishes the first.
2. Each time around display the entire entry.

My question is, how do I find the last entry if I don't know what it is?
For example, right now I have three entries and I can display them
starting with the first. I know there are three so I set my counter to
start at 0 and end on 2 (0,1,2) now if I want to reverse it and start
with the last, how do I get the last id?

SELECT * FROM guestbook SORT BY COUNTER DESC

--
Thomas

SELECT date FROM wife WHERE bitching = '0' AND sex = '1'
Jul 17 '05 #2
store the entries by maintaing a column for timestamp. later select
the entries on desc order of time. This will show the latest entries on
top every time...

Jul 17 '05 #3
On Fri, 17 Dec 2004 00:15:43 +0100, Thomas <ne*********@nospam.netcom.no>
wrote:
*Jim wrote:
I'm using PHP & MySQL to create a simple guestbook. I've created my
table and I'm able to load my information in as usual. I would like it
to display the latest entry first though. I set an id to each entry
that is auto-incremented. The idea seems real easy in theory:

1. Create a loop that starts at the last entry and goes until it
finishes the first.
2. Each time around display the entire entry.

My question is, how do I find the last entry if I don't know what it is?
For example, right now I have three entries and I can display them
starting with the first. I know there are three so I set my counter to
start at 0 and end on 2 (0,1,2) now if I want to reverse it and start
with the last, how do I get the last id?


SELECT * FROM guestbook SORT BY COUNTER DESC


That'd be ORDER BY, not SORT BY. And then LIMIT 1, since the OP only wants one
row. And ordering by the ID might not even give the latest entry; depends when
it was committed (and whether you consider the latest by initial insertion vs.
when it was committed to the database).

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #4
*Andy Hassall wrote:
*Thomas wrote:

SELECT * FROM guestbook SORT BY COUNTER DESC


That'd be ORDER BY, not SORT BY. And then LIMIT 1, since the OP only
wants one
row.

Doh! Most certainly ORDER BY!


--
Thomas

SELECT date FROM wife WHERE bitching = '0' AND sex = '1'
Jul 17 '05 #5
"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:5c********************************@4ax.com...
On Fri, 17 Dec 2004 00:15:43 +0100, Thomas <ne*********@nospam.netcom.no>
wrote:
*Jim wrote:
I'm using PHP & MySQL to create a simple guestbook. I've created my
table and I'm able to load my information in as usual. I would like it
to display the latest entry first though. I set an id to each entry
that is auto-incremented. The idea seems real easy in theory:

1. Create a loop that starts at the last entry and goes until it
finishes the first.
2. Each time around display the entire entry.

My question is, how do I find the last entry if I don't know what it is? For example, right now I have three entries and I can display them
starting with the first. I know there are three so I set my counter to
start at 0 and end on 2 (0,1,2) now if I want to reverse it and start
with the last, how do I get the last id?
SELECT * FROM guestbook SORT BY COUNTER DESC


That'd be ORDER BY, not SORT BY. And then LIMIT 1, since the OP only

wants one row. And ordering by the ID might not even give the latest entry; depends when it was committed (and whether you consider the latest by initial insertion vs. when it was committed to the database).

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool


Well, since he doesn't mention anything about 'editing' posted entries and
the id is auto_incremented then wouldn't the latest entry be the highest id
number? Second, does he want to show only one entry at at time or a pagefull
at a time? Using LIMIT 1 with a loop would require many database accesses
and page reloads (or extra javascript) to look through the guestbook.

SELECT * FROM guestbook ORDER BY counter DESC
would give him all the entries from last to first...

or:

--code--
// set defaults if none received
$gb_start = isset($_REQUEST['start']) ? $_REQUEST['start'] : 0;
$gb_end = 10;
$gb_msg = '';
$gb_link = '';

$gb_query = "SELECT * FROM guestbook ORDER BY counter DESC LIMIT $start,
$end";
$gb_result = mysql_query($gb_query,$dbc); //$dbc is the database connection

if (!$gb_result)
{
die('Error performing query. '.mysql_errno($dbc).': '.mysql_error($dbc));
}

$gb_max_entry = mysql_num_rows($gb_result);
if ($gb_max_entry < 10)
{
$gb_msg = 'End of guestbook.';

}

for($loop = 0; $loop <= $gb_max_entry; $loop++)
{
$gb_entry = mysql_fetch_array($gb_result);
echo "<p>$gb_entry[date]</p>";
echo "<p>$gb_entry[name]</p>";
echo "<p>$gb_entry[message]</p>";
}

if ($gb_msg == '')
{
$gb_start += $gb_max_entry;
$gb_link = "<br><br><p
align='center'>http://www.your.domain/guestbook.php?start=$gb_start</p>";
}
else
{
echo "<br><br><p align='center'>$gb_msg</p>";
}
-- end of code --

This would go through all the entries backwards until none were left,
providing a link to the next page each time.

Norm
---
FREE Avatar Hosting at www.easyavatar.com


Jul 17 '05 #6

"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:5c********************************@4ax.com...
On Fri, 17 Dec 2004 00:15:43 +0100, Thomas <ne*********@nospam.netcom.no>
wrote:
*Jim wrote:
I'm using PHP & MySQL to create a simple guestbook. I've created my
table and I'm able to load my information in as usual. I would like it
to display the latest entry first though. I set an id to each entry
that is auto-incremented. The idea seems real easy in theory:

1. Create a loop that starts at the last entry and goes until it
finishes the first.
2. Each time around display the entire entry.

My question is, how do I find the last entry if I don't know what it is? For example, right now I have three entries and I can display them
starting with the first. I know there are three so I set my counter to
start at 0 and end on 2 (0,1,2) now if I want to reverse it and start
with the last, how do I get the last id?
SELECT * FROM guestbook SORT BY COUNTER DESC


That'd be ORDER BY, not SORT BY. And then LIMIT 1, since the OP only

wants one row. And ordering by the ID might not even give the latest entry; depends when it was committed (and whether you consider the latest by initial insertion vs. when it was committed to the database).


I think the OP was only resorting to the "one at a time" idea since he was
getting it in ascending order and didn't know any other way of reversing the
list, so the proper statement would be (untested):

SELECT * FROM guestbook ORDER BY counter DESC

(I believe :) )
Jul 17 '05 #7
JV
Jim wrote:
I'm using PHP & MySQL to create a simple guestbook. I've created my
table and I'm able to load my information in as usual. I would like it
to display the latest entry first though. I set an id to each entry
that is auto-incremented. The idea seems real easy in theory:

1. Create a loop that starts at the last entry and goes until it
finishes the first.
2. Each time around display the entire entry.

My question is, how do I find the last entry if I don't know what it is?
For example, right now I have three entries and I can display them
starting with the first. I know there are three so I set my counter to
start at 0 and end on 2 (0,1,2) now if I want to reverse it and start
with the last, how do I get the last id?

Any help would be appreciated.


heh counter thats funny... dont use counter.. please :)

mysql_query returns resourse thats array of records

uses the mysql_fetch_row or mysql_fetch_array as listed below

the rows will already be sorted into correct order by the DB query
actual working code follows
1: dbconnect() is a simple function that logs into the db with set
username and password returning the resource handle
2: indx is an autoincrementing value in the table.

sorting by index will sort by entry order :D

--START CODE--

if ( ($dbl = dbconnect()) == DBCONNERROR){
echo "<h2>Guest book is currently unavailable for viewing. Please
try again later.</h2>\n";
} else {
$author = ""; $dtime=""; $message="";
$query = "SELECT * FROM GuestBook ORDER BY indx DESC";
$result = mysql_query($query);

if (!($result)){
echo "Error reading Guestbook<br>";
print_r($result);
} else {
$posts = mysql_num_rows($result);

while ($record = mysql_fetch_row($result)) {
$enid = $record[0];
$author = $record[2];
$dtime = $record[3];
$message = $record[4];
$pub = $record[1];

$by = "<b>By</b> : <i>$author</i><br>\n";
$dt = "<b>Date</b> : <i>$dtime</i><br>\n";
$ms = "<q>$message</q>\n";

$entry = "<hr><p>";
$entry .= "$by";
$entry .= "$dt";
$entry .= "$ms</p>";

echo $entry;
}
}
mysql_close($dbl);
}
--END CODE--

as i said this code works on actual site.

mysql creation statement for table GuestBook is :
CREATE TABLE `GuestBook` (
`indx` int(11) NOT NULL auto_increment,
`public` enum('yes','no') NOT NULL default 'yes',
`author` varchar(50) NOT NULL default '',
`tstamp` varchar(100) NOT NULL default '',
`message` blob NOT NULL,
PRIMARY KEY (`indx`)
) TYPE=MyISAM;

with this table and the preceding code you should be up in no time :D

hth
JV
Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: JL | last post by:
Platform: Linux Red Hat RHEL 3 (and red hat 9) Installed MySQL from source. As a matter of fact, installed all LAMPS from source, and the mysql socket file was arranged in a place other than...
0
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary...
3
by: Kirk Soodhalter | last post by:
Hi, This started as a phpmyadmin problem, but has somehow morphed into a mysql problem. I don't know how to fix it. I am posting the conversation from a php newsgroup since it started there. ...
0
by: Plymouth Acclaim | last post by:
Hi guys, We have a problem with Dual AMD64 Opteron/MySQL 4.0.18/Mandrake 10 for a very high volume site. We are evaluating the performance on our new server AMD64 and it seems it's slow compared...
1
by: Alex Hunsley | last post by:
I am trying to install the DBD::mysql perl module. However, it claims I need mysql.h: cpan> install DBD::mysql CPAN: Storable loaded ok Going to read /home/alex/.cpan/Metadata Database was...
1
by: smsabu2002 | last post by:
Hi, I am facing the build problem while installing the DBD-MySql perl module (ver 2.9008) using both GCC and CC compilers in HP-UX machine. For the Build using GCC, the compiler error is...
1
by: jrs_14618 | last post by:
Hello All, This post is essentially a reply a previous post/thread here on this mailing.database.myodbc group titled: MySQL 4.0, FULL-TEXT Indexing and Search Arabic Data, Unicode I was...
110
by: alf | last post by:
Hi, is it possible that due to OS crash or mysql itself crash or some e.g. SCSI failure to lose all the data stored in the table (let's say million of 1KB rows). In other words what is the worst...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
10
by: Caffeneide | last post by:
I'm using a php script which performs three xml queries to other three servers to retrieve a set of ids and after I do a query to mysql of the kind SELECT * FROM table WHERE id IN ('set of ids');...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.