Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

sql and increment

 
   Database Help (Home) -> PHP SQL RSS
Next:  Let Us Get MOre Traffic And Sales 56202  
Author Message
Damien

External


Since: Aug 11, 2008
Posts: 2



(Msg. 1) Posted: Mon Aug 11, 2008 2:12 pm
Post subject: sql and increment
Archived from groups: alt>php>sql (more info?)

Hello

I have a table with a incremented field at each adding request.
So let's say I have 10 records in the table, numbered from 1 to 10.
Now let's say an user deleted the 7th record.
So far, when I add a new record, it's numbered is 11 (the table is ORDER BY; and the new number is defined as 'last field'+1.
But I would like the new record to replace the 7th that was deleted.
How can I achieve this?
Test each record, if empty then place the new record there, or is there an easier and more intelligent way to do so?

Regards

Damien

 >> Stay informed about: sql and increment 
Back to top
Login to vote
Captain Paralytic

External


Since: Jan 14, 2008
Posts: 245



(Msg. 2) Posted: Mon Aug 11, 2008 2:12 pm
Post subject: Re: sql and increment [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 11 Aug, 13:12, Damien wrote:
> Hello
>
> I have a table with a incremented field at each adding request.
> So let's say I have 10 records in the table, numbered from 1 to 10.
> Now let's say an user deleted the 7th record.
> So far, when I add a new record, it's numbered is 11 (the table is ORDER BY; and the new number is defined as 'last field'+1.
> But I would like the new record to replace the 7th that was deleted.
> How can I achieve this?
> Test each record, if empty then place the new record there, or is there an easier and more intelligent way to do so?
>
> Regards
>
> Damien
It's a strange thing to want to do. Usually one wants to ensure that
no new data can ever have the same id number as older data.

SELECT
a.id+1 next_free_id
FROM numlist a
LEFT JOIN numlist b ON b.id = a.id+1
WHERE b.id IS NULL
LIMIT 1

will tell you the next free ID. Obviously you'll need to wrap the
process up in a transaction or lock tables to avoid race conditions.

Alternatively, you could use INSERT INTO ... SELECT and add your other
fields as constants in the SELECT portion after the next_free_id
column.

 >> Stay informed about: sql and increment 
Back to top
Login to vote
J.O. Aho

External


Since: Dec 01, 2003
Posts: 190



(Msg. 3) Posted: Mon Aug 11, 2008 8:56 pm
Post subject: Re: sql and increment [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Damien wrote:
> Hello
>
> I have a table with a incremented field at each adding request.
> So let's say I have 10 records in the table, numbered from 1 to 10.
> Now let's say an user deleted the 7th record.
> So far, when I add a new record, it's numbered is 11 (the table is ORDER
> BY; and the new number is defined as 'last field'+1.
> But I would like the new record to replace the 7th that was deleted.
> How can I achieve this?
> Test each record, if empty then place the new record there, or is there
> an easier and more intelligent way to do so?

As Captain said, this isn't anything you want to do, if you want a row
numbering, I suggest you use a new column for that and each time something is
inserted/deleted you make a check in that column to see if there is empty spaces.



--

//Aho
 >> Stay informed about: sql and increment 
Back to top
Login to vote
Damien

External


Since: Aug 11, 2008
Posts: 2



(Msg. 4) Posted: Tue Aug 12, 2008 11:59 am
Post subject: Re: sql and increment [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

J.O. Aho wrote:
> Damien wrote:
>> Hello
>>
>> I have a table with a incremented field at each adding request.
>> So let's say I have 10 records in the table, numbered from 1 to 10.
>> Now let's say an user deleted the 7th record.
>> So far, when I add a new record, it's numbered is 11 (the table is
>> ORDER BY; and the new number is defined as 'last field'+1.
>> But I would like the new record to replace the 7th that was deleted.
>> How can I achieve this?
>> Test each record, if empty then place the new record there, or is
>> there an easier and more intelligent way to do so?
>
> As Captain said, this isn't anything you want to do, if you want a row
> numbering, I suggest you use a new column for that and each time
> something is inserted/deleted you make a check in that column to see
> if there is empty spaces.

Thank for the advices.
I did that because...
I haven't yet found out how, in php, include a page wich number is in a
database. I call the page from the number of the page, and so far, I
have a case of which calls an include('page_num'). so I have so far a
code with case of num=1.. until 20.. (I assume I don't want to add more
than 20 fields).

It all right so far as num is a number, but isn't working when num is a
$num value...
I guess this might be the problem here, solving the include problem and
simply use an autoincrement column in the base for each added record.

Thanks anyway

>
>
>
 >> Stay informed about: sql and increment 
Back to top
Login to vote
Paul Lautman

External


Since: Oct 28, 2007
Posts: 128



(Msg. 5) Posted: Tue Aug 12, 2008 11:59 am
Post subject: Re: sql and increment [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Damien wrote:
>
> Thank for the advices.
> I did that because...
> I haven't yet found out how, in php, include a page wich number is in
> a database. I call the page from the number of the page, and so far, I
> have a case of which calls an include('page_num'). so I have so far a
> code with case of num=1.. until 20.. (I assume I don't want to add
> more than 20 fields).
>
> It all right so far as num is a number, but isn't working when num is
> a $num value...
> I guess this might be the problem here, solving the include problem
> and simply use an autoincrement column in the base for each added
> record.
Well I hope that makes sense to you, 'cos it makes none to me!
 >> Stay informed about: sql and increment 
Back to top
Login to vote
J.O. Aho

External


Since: Dec 01, 2003
Posts: 190



(Msg. 6) Posted: Tue Aug 12, 2008 4:12 pm
Post subject: Re: sql and increment [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Damien wrote:

> I did that because...
> I haven't yet found out how, in php, include a page wich number is in a
> database.

I'm not sure wha you store in your database, but assume you have an ID number
(auto incremented) and another column with text.

"SELECT text_column FROM table_name WHERE id_number = {$num}"

This query will give you the text, if there is a such id number in the table,
if it's deleted you won't get any rows at all.

If you don't get any rows, display an error message that the page ain't
anymore, otherwise display the data as you wanted it to be displayed.


> I call the page from the number of the page, and so far, I
> have a case of which calls an include('page_num'). so I have so far a
> code with case of num=1.. until 20.. (I assume I don't want to add more
> than 20 fields).

$file_name=str_replace('..','',$_REQUEST['num']).'.php';

if(file_exsits($file_anme)) {
include($file_name);
} else {
echo "no such page"
}

This will accept any kind of file name, numeric or just characters or any mix,
addind the .php and include if it's located in the same directory as the file
which the code is in, if no file found then an error message is displayed.


> It all right so far as num is a number, but isn't working when num is a
> $num value...

If you are thinkign about the auto incremented column in the database, no you
can only have numbers in it.


> I guess this might be the problem here, solving the include problem and
> simply use an autoincrement column in the base for each added record.

Why not just store the data in the database? eval() should fix problems with
php in the stored data.

--

//Aho
 >> Stay informed about: sql and increment 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
PHPtriad v2.2.1 removal - When I fist started using apache/php/mysql I used phptriad [a wonderful piece of kit] to get me up and running. I've used this ever since. I'm think inow of 'upgrading' to laters versions of all three applications, and have found various tutorials/Ho...

MySQL regular expression to match a imploded item - Hello I need to use MySQL's REGEXP (POSIX compliant) to search registries where one field is an imploded set of integer values separated with pipes "|". I need to match one of these imploded values directly on a sql select. $sql = "SELECT...

Weird mysql_connect problem - Hello. My mysql_connect just started to give me following error today, Fatal error: Call to undefined function: mysql_connect() in ..../database_functions/db_functions.inc.php on line 11. So it seems that my php no longer finds php-mysql module...

MySQL - question about displaying data - I am working an some pages that have database content on them. I'm sorry that I don't have any versions of it online yet, but it isn't hard to explain. The site lists restaurants with contact information. All of the information is kept in a MySQL..

How can i Optimize sql Query ? - Hi, I'd like to optimize this query: Code: SELECT * FROM `links` WHERE active = "1" AND mainweight != 0 ORDER BY Rand()*(1/mainweight) LIMIT 5 I have a database of links wich has 3 000 rows. I'd like to select weighted random links from ...
   Database Help (Home) -> PHP SQL All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]