Re: Updating the SQL key value



Toby A Inkster wrote:
bill wrote:

The problem may come in using a renumber function which should take the steps in their current order and reassign the "order" key, assigning each set an "order" that is the prior "order" + 10.

Something like:

<?php
$db = new PDO(/* connection settings */);
$db->query("ALTER TABLE foobar ADD tempcol integer;");
$update = $db->prepare("UPDATE foobar SET tempcol=? WHERE order=?;");
$count = 0;
foreach ($db->query("SELECT order FROM foobar ORDER BY order;") as $row)
{
$count+=10;
$update->execute($count, $row['order']);
}
$db->query("UPDATE foobar SET order=tempcol;");
$db->query("ALTER TABLE foobar DROP tempcol;");
unset($count, $update);
?>

Lovely idea. For my small number a PHP array probably works better, but I do like this approach and will archive it.

One additional question: does the foreach loop terminate because the query return false ?

bill
.



Relevant Pages

  • Re: User-scripting within PHP
    ... available for PHP? ... runkit is really weird and somewhat dangerous. ... Toby A Inkster BSc ARCS ...
    (comp.lang.php)
  • Re: Get unique combos from arrays
    ... Back to the real world of PHP 5 though. ... Toby A Inkster BSc ARCS ... Perl6 does indeed seem to have some really nice features... ...
    (comp.lang.php)
  • Re: help : apostrophe/single quotation mark
    ... If you don't fix these problems your site is open to being cracked by ... Toby A Inkster BSc ARCS ... and found out that it is part of PHP 4.3. ...
    (comp.lang.php)
  • Re: Date string manipulation
    ... What version of PHP are you using. ... with strtotime seems to work fine here with PHP 5.2.5. ... Toby A Inkster BSc ARCS ...
    (comp.lang.php)
  • Re: is sql just for hobbiest?
    ... Yahoo is also close to 100% PHP, and that doesn't seem to be holding it ... Toby A Inkster BSc ARCS ...
    (comp.lang.php)