Current location: Hot Scripts Forums » Programming Languages » PHP » increase as sequential


increase as sequential

Reply
  #1 (permalink)  
Old 07-13-08, 05:54 AM
zodehala zodehala is offline
Wannabe Coder
 
Join Date: Mar 2007
Posts: 191
Thanks: 0
Thanked 0 Times in 0 Posts
increase as sequential

suppose that i have database called name db and user table

user table

Code:
No user
========
1
1
1
1
i wanna increase value as sequential
Code:
No user
========
1
2
3
.
.
(NO is not AUTO_INCREMENT)


so i write following
PHP Code:

$link mysql_connect("localhost","root","123") or die ( mysql_errno()."<br/>".mysql_error());

$db =  mysql_select_db("db");
$row mysql_num_rows(mysql_query("SELECT * FROM user"$link));

for(
$i=0$i <= $row;  $i++){
    
mysql_query ("UPDATE user SET no = $i) or die ( mysql_errno()."<br/>".mysql_error());

but it does not insert sequential number into cells ( it inserts same value into all cells) why ?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 07-13-08, 06:02 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
Because it'll update ALL rows if you don't specify a WHERE clause.

EDIT:

I think if you go to your PhpMyAdmin, and delete the no field, and then add it again using the auto_increment option, it'll automatically add the IDs.

Last edited by Nico; 07-13-08 at 06:20 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 07-13-08, 06:28 AM
zodehala zodehala is offline
Wannabe Coder
 
Join Date: Mar 2007
Posts: 191
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Nico View Post
Because it'll update ALL rows if you don't specify a WHERE clause.

EDIT:

I think if you go to your PhpMyAdmin, and delete the no field, and then add it again using the auto_increment option, it'll automatically add the IDs.
i tried it using "WHERE" too but it does not


and using auto_increment

http://img515.imageshack.us/img515/7349/snap1oi1.gif
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 07-14-08, 11:15 PM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
Can you show us the table structure?
What type of field is the "no" one?

Like Nico said... you a WHERE clause.

Lex
__________________
01010000 01001000 01010000
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 07-15-08, 08:52 AM
nashruddin nashruddin is offline
Newbie Coder
 
Join Date: Jun 2008
Location: Indonesia
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
modify your code like this:

PHP Code:

$result mysql_query("SELECT * FROM user");


$new_id 1;

while(
$row mysql_fetch_assoc($result)) {
    
mysql_query("UPDATE user SET no = $new_id WHERE no = " $row['no']);
    
$new_id++;

Hope that's work
__________________
PHP, Ajax and Javascript code library by Nash
http://www.nashruddin.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 07-15-08, 03:48 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
The thing is all rows have the same ID (or "no"). And you're using a query without LIMIT, WHERE no = no. So that'll do pretty much the same as his code did.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 07-15-08, 04:51 PM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
How many records do you have... to much to do manually?
I think you need to get all the records into an array to process...
Then remove all records...
Then insert all the records from the array...

Might be dangers to do if not done correctly the first time

Lex
__________________
01010000 01001000 01010000
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 07-16-08, 04:28 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
What exactly are you trying to do?

Looking at the image you posted tells me you already have an AUTO_INCREMENT field, and therefor already have an increasing, sequential value.
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 07-16-08, 08:08 PM
phpdoctor's Avatar
phpdoctor phpdoctor is offline
Code Guru
 
Join Date: Feb 2007
Location: New Zealand
Posts: 767
Thanks: 4
Thanked 2 Times in 2 Posts
What? i didnt see the image for some reason...
It says you already have a auto-increment field?

I dont see the problem either... could you show us the table structure?

Lex
__________________
01010000 01001000 01010000
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


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

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
need a php script for showing sequential images kitaabghar Script Requests 4 06-19-08 01:37 AM
Speed increase needed staticfire Job Offers & Assistance 1 01-18-08 12:48 PM
sequential image display mangled Script Requests 2 05-28-06 08:12 AM
loop function and increase numbers nassau PHP 43 10-13-04 10:50 AM
Need to Increase Your Sales? London General Advertisements 0 07-12-04 05:24 PM


All times are GMT -5. The time now is 12:12 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.