Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1770 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

Insert into database help?

Thread title: Insert into database help?
Closed Thread    
    Thread tools Search this thread Display Modes  
08-04-2008, 06:07 PM
#1
Caden is offline Caden
Status: Junior Member
Join date: Jul 2006
Location: Denver, Colorado
Expertise:
Software:
 
Posts: 66
iTrader: 0 / 0%
 

Caden is on a distinguished road

Send a message via AIM to Caden Send a message via MSN to Caden

  Old  Insert into database help?

OK I have this form that inserts stuff into a database so I can later display the information I insert. And I order it by newest, the code I use for that is

Code:
$queryRecent = "SELECT name FROM table ORDER BY name DESC LIMIT 0,10";
Is this wrong or right? What I want it to do is display the newest one I inserted on top, because it's a Top 10 Recent Updates section. It inserts everything I do, but sometimes doesn't display it before the older ones I've inserted which is the whole point of this script.

But anyway, when I looked in phpMyAdmin in my table, it had some of the stuff I entered into it, but also a bunch of empty fields as well. What could be the problem?

08-04-2008, 06:13 PM
#2
Jako is offline Jako
Jako's Avatar
Status: Jakowenko.com
Join date: Jun 2005
Location: Michigan
Expertise:
Software:
 
Posts: 2,199
iTrader: 3 / 100%
 

Jako is on a distinguished road

  Old

I've been just getting into PHP lately, so I'm not 100% sure on this, but this is what I use on one of my queries.

PHP Code:
$image_id "SELECT * FROM images ORDER BY i_id DESC LIMIT 1"
This is sorting my table by the i_id, newest one being on the top and I'm limiting the results to 1, which is the newest entry.

So for yours maybe try

PHP Code:
$queryRecent "SELECT name FROM table ORDER BY id DESC LIMIT 10"

08-05-2008, 01:41 PM
#3
ditch182 is offline ditch182
Status: Junior Member
Join date: Jul 2008
Location: NC
Expertise:
Software:
 
Posts: 70
iTrader: 0 / 0%
 

ditch182 is on a distinguished road

  Old

Your SQL statement is not selecting the 10 newest entries. It is selecting the last 10 entries when ordered by "name". That's a subtle difference I know, but if "name" is a string then you'll get the last 10 in descending alphabetical order. Try ordering by id. Also, your LIMIT clause is technically correct, but you don't need the 0. 0 is the default offset, so LIMIT 0,10 is the same as LIMIT 10.

08-05-2008, 06:41 PM
#4
Jako is offline Jako
Jako's Avatar
Status: Jakowenko.com
Join date: Jun 2005
Location: Michigan
Expertise:
Software:
 
Posts: 2,199
iTrader: 3 / 100%
 

Jako is on a distinguished road

  Old

Originally Posted by ditch182 View Post
Your SQL statement is not selecting the 10 newest entries. It is selecting the last 10 entries when ordered by "name". That's a subtle difference I know, but if "name" is a string then you'll get the last 10 in descending alphabetical order. Try ordering by id. Also, your LIMIT clause is technically correct, but you don't need the 0. 0 is the default offset, so LIMIT 0,10 is the same as LIMIT 10.
Did you read anything I typed?

08-11-2008, 02:24 PM
#5
ditch182 is offline ditch182
Status: Junior Member
Join date: Jul 2008
Location: NC
Expertise:
Software:
 
Posts: 70
iTrader: 0 / 0%
 

ditch182 is on a distinguished road

  Old

I didn't mean to step on your toes Jako. I was just trying to give Caden some more information as to why his query wasn't working. What you wrote is absolutely correct, and you are both a gentleman and a scholar.

10-13-2008, 12:11 PM
#6
shilpamg is offline shilpamg
Status: Member
Join date: Aug 2008
Location:
Expertise:
Software:
 
Posts: 101
iTrader: 0 / 0%
 

shilpamg is on a distinguished road

  Old

Originally Posted by Jako View Post
I've been just getting into PHP lately, so I'm not 100% sure on this, but this is what I use on one of my queries.

PHP Code:
$image_id "SELECT * FROM images ORDER BY i_id DESC LIMIT 1"
This is sorting my table by the i_id, newest one being on the top and I'm limiting the results to 1, which is the newest entry.

So for yours maybe try

PHP Code:
$queryRecent "SELECT name FROM table ORDER BY id DESC LIMIT 10"
this is the right query.

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed