General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old April 26th, 2003, 07:44 AM
bassplayer bassplayer is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 3 bassplayer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Please help

I have 2 questions:
1) How do you add together all the numbers in a column?
2) How do you get just the most recent addition to a column e.g. the last number in the id field.
Thanks for your help.

Reply With Quote
  #2  
Old April 26th, 2003, 08:23 AM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
Hi Bassplayer,

Please refer to the forum rules before posting any messages. Your subject does not follow our guidelines.

As for your first question, can you elaborate a little further. I'm having a hard-time understanding... If you're looking to add all the values in a certain column (within a database), you can use the SUM() function which will return the result of the addition.

Your second question: You can retrieve the last inserted ID using mysql_insert_id(), which will return the last id inserted into an auto-incrementing field (most likely your primary key).

Hope that helps...
__________________
____________________________________________
Developer Shed Weekly Writer | DevArticles Forum Moderator
Build Your Own KlipFolio Klip With PHP
FrankManno.com - Under Construction
Design Interactive Group - Under Construction

Reply With Quote
  #3  
Old April 26th, 2003, 09:03 AM
bassplayer bassplayer is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 3 bassplayer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Sorry about that, I should have read the rules. Your answers for both my questions are exactly what I was looking for. I was looking to add all the values in a certain column.
Thanks a lot.

Reply With Quote
  #4  
Old April 26th, 2003, 09:57 AM
bassplayer bassplayer is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 3 bassplayer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I've been trying to use mysql_insert_id and haven't managed to get it working. I am trying to insert the value of the last entry into the id column into another column of my database. I have used
$query = "INSERT INTO $table (n) VALUES (mysql_insert_id())";
It's probably a silly mistake but please help me out.

Reply With Quote
  #5  
Old May 4th, 2003, 01:25 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
Quote:
Originally posted by bassplayer
I've been trying to use mysql_insert_id and haven't managed to get it working. I am trying to insert the value of the last entry into the id column into another column of my database. I have used
$query = "INSERT INTO $table (n) VALUES (mysql_insert_id())";
It's probably a silly mistake but please help me out.


Try storing the value of the mysql_insert_id() into a variable and then use that variable in your next insert:

Code:
$last_insert = mysql_insert_id();
$query = "INSERT INTO $table (n) VALUES ($last_insert)";


HTH!

Reply With Quote
  #6  
Old May 6th, 2003, 09:15 AM
bEeZm bEeZm is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: usa
Posts: 5 bEeZm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to bEeZm Send a message via Yahoo to bEeZm
Or... it's kinda lengthy but it works:

PHP Code:
 mysql_pconnect("dbhost","dbuser","dbpass"); 
mysql_select_db("dbname"); 
$result mysql_query("select * from TABLENAME order by id DESC limit 1");
while(
$r=mysql_fetch_array($result))
{     
    
$id=$r["id"];



Then just use:
$query = "INSERT INTO $table (n) VALUES ($id)";

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Please help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway