tags:

views:

782

answers:

5

Hi friends i develop a project, in that i display the values from mysql database using PHP , in that i need to set a unique id[Not manually] for each row that i fetched from database.Can anyone help me please. Thanks in Advance.

+2  A: 

Take a look at this article for how to create an auto incremented field to generate unique ID for the record:

http://www.plus2net.com/sql_tutorial/mysql_auto_increment.php

Basicly you use AUTO_INCREMENT attribute on the column.

This

CREATE TABLE animals (
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     name CHAR(30) NOT NULL,
     PRIMARY KEY (id)
 );

INSERT INTO animals (name) VALUES
    ('dog'),('cat'),('penguin'),
    ('lax'),('whale'),('ostrich');

SELECT * FROM animals;

Will create this output:

+----+---------+
| id | name    |
+----+---------+
|  1 | dog     |
|  2 | cat     |
|  3 | penguin |
|  4 | lax     |
|  5 | whale   |
|  6 | ostrich |
+----+---------+
Espo
is there anything inbuild like rowid or something else ?
Sakthivel
No, there is no such thing in mysql
Espo
+2  A: 

How to automatically generate ids has already been answered. Additional info: If you want to see which id was inserted for a row, use mysql_insert_id()

soulmerge
A: 

Alternatively, you should already have a primary key or compound key, get a hash of these using the md5 function and store in memory. However, creating an auto incrementing field or a UUID field in the database would be the most preferable option.

Tim
A: 

HI, Even i have come across the same situation, where I need to display the row id. To implement this, i have created a separated table, where I Control the table with Stored Procedure, before it refills, I will delete all the records and reinserts with the latest Ranking systems. I am successful in this and implemented in http://cricandcric.com/C&Guess/login.php

James
A: 

Will a random floating point number work?

SELECT RAND( ) AS AltKey, Column1, Column2, ColumnN FROM Table1

And if you need an integer:

SELECT CONVERT( RAND( ) * 1000000, UNSIGNED ) AS AltKey, Column1, Column2, ColumnN FROM Table1
Salman A