tableorder.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
function order($mysqli, $table, &$error = false){
$mysqli->autocommit(FALSE);
if (!$result = $mysqli->query("SELECT * FROM `".$table."`")) {
if($error !== false){
$error = "Error: ".mysqli_connect_error();
}
$mysqli->rollback();
return false;
exit();
} else {
if ($result = $mysqli->query("SELECT DATABASE() AS db")) {
$row = $result->fetch_assoc();
$db = $row["db"];
$query = "SELECT k.column_name
FROM information_schema.table_constraints t
JOIN information_schema.key_column_usage k
USING(constraint_name,table_schema,table_name)
WHERE t.constraint_type='PRIMARY KEY'
AND t.table_schema='".$db."'
AND t.table_name='".$table."'";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
$primary = $row["column_name"];
}
} else {
if($error !== false){
$error = "Something went wrong with Query: $query :: ". mysqli_connect_error();
}
$mysqli->rollback();
return false;
exit();
}
$result = $mysqli->query("SELECT * FROM `".$table."`");
$x = 1;
while($q = $result->fetch_assoc()){
$query = "UPDATE `".$table."` SET `".$primary."`='".$x."' WHERE `".$primary."`='".$q[$primary]."'";
if(!$mysqli->query($query)){
if($error !== false){
$error = "Something went wrong with query: $query :: ".mysqli_connect_error();
}
$mysqli->rollback();
return false;
exit();
}
$x++;
}
$count = $result->num_rows;
$mysqli->query("ALTER TABLE `".$table."` AUTO_INCREMENT =".$count);
} else {
if($error !== false){
$error = "Something went wrong by selecting the Table :: ".mysqli_connect_error();
}
$mysqli->rollback();
return false;
exit();
}
}
$mysqli->commit();
return true;
}
?>
function order($mysqli, $table, &$error = false){
$mysqli->autocommit(FALSE);
if (!$result = $mysqli->query("SELECT * FROM `".$table."`")) {
if($error !== false){
$error = "Error: ".mysqli_connect_error();
}
$mysqli->rollback();
return false;
exit();
} else {
if ($result = $mysqli->query("SELECT DATABASE() AS db")) {
$row = $result->fetch_assoc();
$db = $row["db"];
$query = "SELECT k.column_name
FROM information_schema.table_constraints t
JOIN information_schema.key_column_usage k
USING(constraint_name,table_schema,table_name)
WHERE t.constraint_type='PRIMARY KEY'
AND t.table_schema='".$db."'
AND t.table_name='".$table."'";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
$primary = $row["column_name"];
}
} else {
if($error !== false){
$error = "Something went wrong with Query: $query :: ". mysqli_connect_error();
}
$mysqli->rollback();
return false;
exit();
}
$result = $mysqli->query("SELECT * FROM `".$table."`");
$x = 1;
while($q = $result->fetch_assoc()){
$query = "UPDATE `".$table."` SET `".$primary."`='".$x."' WHERE `".$primary."`='".$q[$primary]."'";
if(!$mysqli->query($query)){
if($error !== false){
$error = "Something went wrong with query: $query :: ".mysqli_connect_error();
}
$mysqli->rollback();
return false;
exit();
}
$x++;
}
$count = $result->num_rows;
$mysqli->query("ALTER TABLE `".$table."` AUTO_INCREMENT =".$count);
} else {
if($error !== false){
$error = "Something went wrong by selecting the Table :: ".mysqli_connect_error();
}
$mysqli->rollback();
return false;
exit();
}
}
$mysqli->commit();
return true;
}
?>