Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize Online / SQLize Online  /  SQLtest Online

A A A
Login    Share code      Blog   FAQ

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

Copy Format Clear
create table tabl ( id serial, sort bigserial, pos_changed_at timestamp null ); INSERT INTO tabl (sort) VALUES ( extract(epoch from now())*10000 ), ( extract(epoch from now() + interval '5 min')*10000 ), ( extract(epoch from now() + interval '10 min')*10000 ); SELECT * FROM tabl;

Stuck with a problem? Got Error? Ask AI support!

Copy Clear
Copy Format Clear
<?php $query = "SELECT sort FROM tabl ORDER BY sort;"; $stmt = $pdo->prepare($query); $insert = "INSERT INTO tabl (sort) VALUES (:sort)"; $sth = $pdo->prepare($insert); for ($i = 1; $i <= 20; $i++) { $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); $sort = $rows[0]["sort"] + round(($rows[1]["sort"] - $rows[0]["sort"]) / 2); $sth->execute(["sort" => $sort]); } $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); //var_dump($rows); while (key($rows) !== null) { $a = current($rows)['sort']; $b = next($rows)['sort']; echo "$b - $a = " . $b - $a . PHP_EOL; }
Copy Clear