473,386 Members | 1,748 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,386 software developers and data experts.

last inserted row number.

155 100+
hi all !!

i got my results like that

1 user0 55
2 user1 53
3 user2 49
4 user2 48
5 user3 47 <------- last inserted row
6 user4 46

how do i get to echo the position of the last inserted row?

the result need to be "last inserted row is 5"!!


thank you all!!
Jun 12 '09 #1
20 3474
Markus
6,050 Expert 4TB
@canabatz
There are many possible ways, it depends on how you're storing this data. Give us some more information. Also, wouldn't the last inserted row by #6 ?
Jun 12 '09 #2
prabirchoudhury
162 100+
1. not very much clear abt asking.
2. we could get just inserted using

Expand|Select|Wrap|Line Numbers
  1. after executing insert statement call
  2. $last_inserted_id = mysql_insert_id();
  3.  
  4.  
or explain specific in details


:)
Jun 13 '09 #3
canabatz
155 100+
before

count | username | bid-amount
1 user0 55
2 user2 49
3 user2 48
4 user4 46

after

count | username | bid-amount
1 user0 55
2 user2 49
3 user2 48
4 user4 47 <--- last inserted
5 user4 46


bid amount going with order by bid_amount DESC ,so amount 47 was last inserted ,the amount 46 count was 4 ,and after the insert of amount 47 ,amount 46 go down to 5 count!!

im displaying to the user in what place is bid amount are!! ,and this is what im trying to get.

thanks for your replay :)

i hope you got me better now.
Jun 13 '09 #4
prabirchoudhury
162 100+
Option A
1. i dont think it a good idea to update table data for every insert.
2. it is easier to get the details for particular bit (would be a bit)
Expand|Select|Wrap|Line Numbers
  1.  
  2. table bit
  3. ---------------------
  4. | bit_id| details
  5. ---------------------
  6. |1001 | car
  7. |1002 | gold
  8. --------------------
  9.  
  10. add a fild bit_id in your described table
  11.  
  12.  
  13.  
3. when you wanna display current biting status for a particular bit_id, get the query of
Expand|Select|Wrap|Line Numbers
  1. ORDER BY `bid-amount` DESC 
  2.  
option B
4. otherwise you have to check the data in bid-amount for a bit_id(that you doent have) and rearrange with updates.

5. i would prefer option A

:))
Jun 13 '09 #5
Markus
6,050 Expert 4TB
Again, you haven't explained how you're storing the data.

But, the last inserted is going to be (providing there was only one insert) array_length - 2.
Jun 13 '09 #6
canabatz
155 100+
the count is running and doing the count dynamic ,no change in the database

only the id is changing in the database the id is on auto increment.
when im displaying the results it is going with bid_amount desc ,so the highst bid_amount is allways at the top!!

now if second user place a bid and it is not in the top(the bid amount is less then the first user, then the count is just displaying in what line he his!!

all i need is to show the user in what position he his ,like:

echo hello $username ."you are in $position position";

i hope this time you get what i need!!

thanks
Jun 13 '09 #7
waqasahmed996
160 100+
i think your problem will be solved using simply

Expand|Select|Wrap|Line Numbers
  1. ORDER BY `bid_amount` DESC 
  2.  
Jun 13 '09 #8
prabirchoudhury
162 100+
1. insert a new bid . (without count) [but no duplicate bit-amount.]

Expand|Select|Wrap|Line Numbers
  1. 2. select latest details
  2.  
  3. $sql= "select * from table ORDER BY bit-amount DESC";
  4. $csr = mysql_query($sql);
  5. $count = 1;
  6. while ($row=mysql_fetch_array($csr)){
  7.  
  8. update count with $count where id = '".$row[id]."' ";
  9.  
  10. $count++
  11. }
  12.  
  13.  
  14.  
then it would update the current bit status


:)
Jun 13 '09 #9
canabatz
155 100+
i got a table returning all the data from the database ,one row showing the bid_amount!!


all i want is to tell the user in what position he his!

the table have a dynamic count with count++ !!


all i want is to tell the user the row number he is!! thats it!!

i cannot get that!
thank you!
Jun 13 '09 #10
n8kindt
221 100+
before

count | username | bid-amount
1 user0 55
2 user2 49
3 user2 48
4 user4 46

after

count | username | bid-amount
1 user0 55
2 user2 49
3 user2 48
4 user4 47 <--- last inserted
5 user4 46
the title of this post is misleading. you're not looking for the last inserted id but rather the current rank of a user who placed a bid.

what i would do is make a query that sorts the users by bid amount, retrieve the results using mysql_fetch_assoc and then loop thru the results.

something like
Expand|Select|Wrap|Line Numbers
  1. $target_username = "user4"; //username we need to match
  2. $username_matched = false; //we have not matched a username yet
  3. $q = "SELECT * FROM `bid-history` ORDER BY `bid-amount` ASC";
  4. $r = mysql_query($r);
  5. $count = 1; //set current
  6. while($w = mysql_fetch_assoc($r) && !$username_matched){
  7.   if($w['username']==$target_username ){
  8.    $username_matched=true;
  9.    $user_position = $count;
  10.    $user_bidamount = $w['bid-amount'];
  11.   }
  12.  $count++;
  13.  
  14.  
  15. }
  16. echo "User \"$target_username\" is number $user_position in line with a bid amount of $$user_bidamount";
  17.    //output: User "user4" is number 4 in line with a bid amount of $47
  18.  
Jun 13 '09 #11
canabatz
155 100+
where does it get the last bid_amount from?

there is bids from the same user again and again!!

your code is exactly what i need but need some fixes to fit!!

thank you!!!
Jun 13 '09 #12
n8kindt
221 100+
if you look, i set it up so that the while loop only runs when $username_matched equals false. when the user is matched the variable is set to true thus killing the loop
Jun 13 '09 #13
canabatz
155 100+
Expand|Select|Wrap|Line Numbers
  1. $q = "SELECT * FROM `bidding_details` where bid_id=$bid_id ORDER BY id desc limit 1";
i want to limit the SELECT to find only the id of the last inserted id!

now i want to start count++ and see if the $username match the highest ID,

if there is a match between the $username and the Highst ID then display
the $count!!

thanx!
Jun 13 '09 #14
canabatz
155 100+
this code is working for me ,but i want it to display only the last inserted row ,position ,now i can display all the rows and the position ,but i need to show the user only his last bid position ,please help me figure this one out!!

Expand|Select|Wrap|Line Numbers
  1. $q = "(SELECT * FROM `bidding_details` where bid_id=$bid_id and sortbid = '1' order by bid_price desc) union all (SELECT * FROM `bidding_details` where bid_id=$bid_id and sortbid = '0' order by bid_price desc) order by sortbid desc, bid_price desc";
  2. $result=mysql_query($q) or die('Failed selecting applications: ' . mysql_error());
  3. $num1=mysql_num_rows($result);
  4. $rown = 0; 
  5. while($row = mysql_fetch_assoc($result)) 
  6.     $rown++; 
  7.     echo $row['bid_price']. ' is pos: ' . $rown . '.<br>'; 
  8.  
  9. }
thank you
Jun 14 '09 #15
n8kindt
221 100+
@canabatz
ok, you're going to need to add a column to your table and designate it as a timestamp. when you create a new column in phpmyadmin it will give you the option of designating it as a timestamp and the mysql server will generate the current timestamp for you when a new row is inserted-just like the auto increment method you use for your primary key. you can also manually generate it in php using time() (or mktime() if not using the current date). once that is set up correctly, just sort your results by the timestamp field.
Jun 14 '09 #16
canabatz
155 100+
this code i posted is giving me this result

6.00 is pos: 1.
5.00 is pos: 2.
4.05 is pos: 3.
4.04 is pos: 4.
4.03 is pos: 5.
4.02 is pos: 6.

4.05 is the last bid inserted ,the id of this bid is the highest bid !

what im try to solve is to display to the user this:

your bid price of 4.05 is at position 3.

i dont want to display all the results from all the users! only the last inserted!!

thanx
Jun 14 '09 #17
canabatz
155 100+
please ,please ,someone?

this is the only thing i got left to fix to open my site!!
Jun 14 '09 #18
Dormilich
8,658 Expert Mod 8TB
what about assigning a user id to the bid?
Jun 14 '09 #19
canabatz
155 100+
there is ,reg_id ,username ,bid_price , bid_date ,an email .

i want the code to do like that:

select from the table for all the result.

start COUNT from the top ORDER by BID_PRICE DESC.

find the SESSION_USERNAME and look if the highest ID of the last bid_price entered by him

if the Highest ID == session_username stop count!

echo $count $username!

thats it!!
Jun 15 '09 #20
canabatz
155 100+
i searched all the web and none have any idea how to solve this!!

can some one find a way to get the effected row numbe from a select query?

take it as a challenge ,maybe it will help others !!

please help ,than you!!
Jun 18 '09 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ---------------------- tblPerson tblPersonSoftware ...
4
by: Carlos San Miguel | last post by:
Hi, I'm going to be using Oracle with a customer. I will access Oracle with ODBC. All the tables have an Identity column, this is the ID for the record. I need to know how I can get the value of...
1
by: George Hill | last post by:
Hi I am trying to import several master detail records from files to ms sql server. I have orders file and order_items file that has several rows for each order. If I insert programmatically...
6
by: Maurizio Faini | last post by:
I have a little problem. there is a way to get last id inserted into db or i have to make a new query? I explain better my question: in vbscript using sqlserver2000 i can use this code: ...
20
by: Guru | last post by:
Hi I have a table which contains number of rows. I want to fetch the last 5 records from the table. I know for the first 'n' records we can use FETCH FIRST n ROWS ONLY command. But i want to...
3
by: Mark | last post by:
I'm using ASP.Net to accress a database, what I need to do is get the fields out of the very last record in the db. How do I do this? Actually I'm after the primary key, titled 'AdID' it'll tell...
10
by: MaRCeLO PeReiRA | last post by:
Hi guys, I am in troubles with a SERIAL field. I have five tables. A parent table and four child tables. When I do the INSERT in the parent table, I have an ID (generated) by the sequence...
2
by: Daz | last post by:
Hi. I would like to know how to obtain a reference to (or at least, element type of) the last node which was inserted into the document. I am using an event listener to listen for dom inserts,...
2
by: Jim in Arizona | last post by:
I forgot what the SQL command was to return the key of the last inserted record. Does anyone happen to know what it is? Thanks, Jim --
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.