Home > Uncategorized > Dump any SQL table in PHP

Dump any SQL table in 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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
$class = "";
 
$sql_command = "SELECT * FROM `users`";
 
function dumpSqlTable($servername,$username, $password,$dbname, $sql_command, $class ) {
 
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$cmd = $conn->prepare($sql_command);
$cmd->execute();
$res = $cmd->fetchAll();
 
$keys = array_keys($res[0]);
$col_count = count($keys);
$num_col = $len / 2;
 
echo "<table class=\"$class\">";
echo "<tr>";
$keys = array_keys($res[0]);
 
// Print out the column names
for ($i = 0; $i < $col_count; $i+=2) {
  echo "<th>";
  echo $keys[$i];
  echo "</th>";
}
echo "</tr>";
 
$row_count = count($res);
 
//Print out the rows
for ($i = 0; $i < $row_count; $i++) {
  echo "<tr>";
  for ($j = 0; $j < $col_count; $j++) {
    echo "<td>";
    echo $res[$i][$j];
    echo "</td>"; 
  }
  echo "</tr>";
}
echo "</table";
}
 
dumpSqlTable($servername, $username, $password, $dbname, $sql_command, $class);
 
?>
</body>
</html>
Categories: Uncategorized Tags:
  1. No comments yet.
  1. No trackbacks yet.


eight − 5 =