Ask a Question related to PHP Development, Design and Development.
-
Québec #1
rnumbering a table numbered 14 7 9 14 (newbe like)
Hi,
I have been fooling around trying to learn mySql and I have a table wich
ID's are not in continuous numbers: 1 4 8 13.
How do we renumber a table as 1 2 3 4 5 etc?
Jean Pierre
--
--
[url]http://web.jeanpierredaviau.com[/url]
Québec Guest
-
Can't make bullets, numbered lists or indent
When editing a page in Contribute 3, and I'm not in a table, all the editing icons and menu items seem to work except bullets, numbered lists,... -
PDF Numbered Bookmarks Similar to Word Document Map
When converting from .doc to .pdf using PDFMaker from within Microsoft Word, is it possible to automatically create numbered bookmarks similar to... -
Numbered Pages
I have seen the light. I can create multiple paged documents in Acrobat 6.0 and print them out. My desire now is to have Acrobat number these pages... -
How to make a file where the pages are numbered correctly?
I'm supposed to print a small "book", page size A5, 40 pages. How can I arrange the pages easily so that they are in the right place when you... -
Batch import multiple .Tif as numbered frames
select the 210 cast members, then select castToTime function from, I think the modify menu. The member sequence wil be inserted over 210 frames,... -
Andy Hassall #2
Re: rnumbering a table numbered 14 7 9 14 (newbe like)
On Wed, 8 Sep 2004 13:23:26 -0400, "Québec" <Once@WasEno.ugh> wrote:
First off - why do you want to do this? If they're IDs, then gaps aren't>I have been fooling around trying to learn mySql and I have a table wich
>ID's are not in continuous numbers: 1 4 8 13.
>
>How do we renumber a table as 1 2 3 4 5 etc?
significant.
If you still want to, then you can't from MySQL, so do it from PHP with a
loop.
<?php
$conn = mysql_connect()
or die(mysql_error());
mysql_select_db('test')
or die(mysql_error());
mysql_query('drop table if exists t')
or die(mysql_error());
mysql_query('create table t (id int unsigned not null primary key)')
or die(mysql_error());
foreach (array(1,4,8,13) as $v)
mysql_query(sprintf('insert into t values (%d)', $v))
or die(mysql_error());
$res = mysql_query('select id from t order by id')
or die(mysql_error());
while ($row = mysql_fetch_assoc($res))
print $row['id'] . '<br>';
print "<hr>";
mysql_query('lock table t');
$res = mysql_query('select id from t order by id')
or die(mysql_error());
$id = 1;
while ($row = mysql_fetch_assoc($res))
mysql_query(
sprintf('update t set id=%d where id=%d', $id++, $row['id']));
$res = mysql_query('select id from t order by id')
or die(mysql_error());
while ($row = mysql_fetch_assoc($res))
print $row['id'] . '<br>';
?>
Outputs:
1
4
8
13
---------------
1
2
3
4
But note that this script is woefully incomplete, because presumably you'll
have foreign keys referencing this table, which would all have to be updated as
well. Which is a pretty good reason for not doing it.
--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Andy Hassall Guest
-
Québec #3
Re: rnumbering a table numbered 14 7 9 14 (newbe like)
Yes.
Thank you.
Jean
"Andy Hassall" <andy@andyh.co.uk> a écrit dans le message de
news:99quj0lu6f4s6gr6ranqp70hfi1b7agdie@4ax.com...you'll> On Wed, 8 Sep 2004 13:23:26 -0400, "Québec" <Once@WasEno.ugh> wrote:
>>> >I have been fooling around trying to learn mySql and I have a table wich
> >ID's are not in continuous numbers: 1 4 8 13.
> >
> >How do we renumber a table as 1 2 3 4 5 etc?
> First off - why do you want to do this? If they're IDs, then gaps aren't
> significant.
>
> If you still want to, then you can't from MySQL, so do it from PHP with a
> loop.
>
> <?php
> $conn = mysql_connect()
> or die(mysql_error());
> mysql_select_db('test')
> or die(mysql_error());
>
> mysql_query('drop table if exists t')
> or die(mysql_error());
>
> mysql_query('create table t (id int unsigned not null primary key)')
> or die(mysql_error());
>
> foreach (array(1,4,8,13) as $v)
> mysql_query(sprintf('insert into t values (%d)', $v))
> or die(mysql_error());
>
> $res = mysql_query('select id from t order by id')
> or die(mysql_error());
> while ($row = mysql_fetch_assoc($res))
> print $row['id'] . '<br>';
>
> print "<hr>";
>
> mysql_query('lock table t');
>
> $res = mysql_query('select id from t order by id')
> or die(mysql_error());
>
> $id = 1;
> while ($row = mysql_fetch_assoc($res))
> mysql_query(
> sprintf('update t set id=%d where id=%d', $id++, $row['id']));
>
> $res = mysql_query('select id from t order by id')
> or die(mysql_error());
> while ($row = mysql_fetch_assoc($res))
> print $row['id'] . '<br>';
> ?>
>
> Outputs:
>
> 1
> 4
> 8
> 13
> ---------------
> 1
> 2
> 3
> 4
>
> But note that this script is woefully incomplete, because presumablyupdated as> have foreign keys referencing this table, which would all have to be> well. Which is a pretty good reason for not doing it.
>
> --
> Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
> <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Québec Guest
-
Alvaro G Vicario #4
Re: rnumbering a table numbered 14 7 9 14 (newbe like)
*** Québec wrote/escribió (Wed, 8 Sep 2004 13:23:26 -0400):
I guess you only want to do something as simple as displaying line numbers> I have been fooling around trying to learn mySql and I have a table wich
> ID's are not in continuous numbers: 1 4 8 13.
>
> How do we renumber a table as 1 2 3 4 5 etc?
on screen:
$res=mysql_query('SELECT * FROM table');
$line=0;
while($row=mysql_fetch_assoc($res)){
$line++;
print $line . ': ' . $row['field1'] . ', ' . $row['field2'] . '<br>';
}
If your application design depends on having consecutive IDs in your
database, you'd better redesign it from scratch.
--
-- Álvaro G. Vicario - Burgos, Spain
-- Thank you for not e-mailing me your questions
--
Alvaro G Vicario Guest
-
Québec #5
Re: rnumbering a table numbered 14 7 9 14 (newbe like)
Emptying a data base set everything to zero and keep the database structure?
Jean Pierre
Québec Guest
-
Québec #6
Re: rnumbering a table numbered 14 7 9 14 (newbe like)
I will just try it now on a foo table
Thanks
Québec Guest



Reply With Quote

