-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
executable file
·180 lines (127 loc) · 4.73 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
// Conexão direta com banco SQL Server (sem mencionar nome do banco)
// Clecio Antao - 25/03/2019 // PAVILION / editando 2
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>Clecio</title>
</head>
<body>
<?php
// CONEXÃO BANCO SQL SERVER
$serverName = "192.168.0.10,1433";
//$serverName = "PAVILION\CURSO2";
$connOptions = array("UID" => "sa", "PWD" => "Proteu690201");
$conn = sqlsrv_connect($serverName, $connOptions);
// TESTA ACESSO E DRIVERS DO BANCO
if ($conn === false) {
die(print_r(sqlsrv_errors(), true));
}
if ($client_info = sqlsrv_client_info($conn)) { //nao seguir a dica do netbeans
echo 'Conectou! <br>';
foreach ($client_info as $key => $value) {
echo $key . ": " . $value . "<br />";
}
} else {
echo "Erro.<br />";
}
$server_info = sqlsrv_server_info($conn);
if ($server_info) {
foreach ($server_info as $key => $value) {
echo $key . ": " . $value . "<br />";
}
} else {
die(print_r(sqlsrv_errors(), true));
}
// FAZ CONEXÃO COM SYS.DATABASES
$sql_a = "SELECT name FROM sys.databases where database_id > 4";
$stmt_a = sqlsrv_query($conn, $sql_a);
if ($stmt_a === false) {
die(print_r(sqlsrv_errors(), true));
}
$dados1 = filter_input_array(INPUT_POST, FILTER_DEFAULT);
var_dump($dados1);
?>
<!-- FORMULARIO SELECIONAR O BANCO-->
<br> Selecione o banco <br>
<form action="" method="post" name="bancos" >
<select name = "select_banco" onchange="document.forms['bancos'].submit();">
<?php
// ciclo para percorrer os elementos de um array
$i = 0;
while ($row = sqlsrv_fetch_array($stmt_a, SQLSRV_FETCH_ASSOC)) {
$i++;
?>
<option value="<?php echo $row['name']; ?>" <?php echo isset($_POST['select_banco']) && $_POST['select_banco'] == $row['name'] ? ' selected="selected"' : ''; ?>><?php echo $row['name']; ?> </option>
<?php
}
?>
</select>
</form>
<?php
$dados2 = filter_input_array(INPUT_POST, FILTER_DEFAULT);
var_dump($dados2);
//if (isset($_POST['select_banco'])) {
// echo $_POST['select_banco'];
//}
// FAZ CONEXÃO COM BANCO SELECIONADO
//$sql_use = "USE " . $_POST['select_banco']; // . $bancosel;
$sql_use = "USE CADASTRO";
$stmt_use = sqlsrv_query($conn, $sql_use);
if ($stmt_use == false) {
echo '<br> Não conectou banco selecionado...<br>';
die(print_r(sqlsrv_errors(), true));
}
// FAZ SELECT PARA LER TABELAS DO BANCO
$sql_table = "select * from INFORMATION_SCHEMA.TABLES";
$stmt_table = sqlsrv_query($conn, $sql_table);
if ($stmt_table == false) {
echo '<br> Não conectou com banco.<br>';
}
// LISTA AS TABELAS
?>
<br> Selecione a tabela <br>
<form action="" method="post" name="tabelas" >
<select name = "select_tabela" onchange="document.forms['tabelas'].submit();">
<?php
while ($row = sqlsrv_fetch_array($stmt_table, SQLSRV_FETCH_ASSOC)) {
// echo $row['name'] . "<br />";
$i++;
?>
<option value="<?php echo $row['TABLE_NAME']; ?>" <?php echo isset($_POST['select_tabela']) && $_POST['select_tabela'] == $row['TABLE_NAME'] ? ' selected="selected"' : ''; ?>><?php echo $row['TABLE_NAME']; ?> </option>
<?php } ?>
</select>
</form>
<?php
// LISTAR CONTEUDO DAS TABELAS SELECIONADAS
//select * from information_schema.columns where table_name = 'amigos'
//$sql_table_sel = "select * from " . $dados2['select_tabela'];
$sql_table_sel = "select * from information_schema.columns where table_name = '" . $dados2['select_tabela'] . "'";
$stmt_table_sel = sqlsrv_query($conn, $sql_table_sel);
echo $sql_table_sel;
if ($stmt_table_sel == false) {
echo '<br> Não conectou com a tabela.<br>';
}
?>
<table border=1>
<tr>
<td>COLUNA</td>
<td>TIPO</td>
</tr>
<?php
while ($row = sqlsrv_fetch_array($stmt_table_sel, SQLSRV_FETCH_ASSOC)) {
?>
<tr>
<td><?php echo $row['COLUMN_NAME']; ?></td>
<td><?php echo $row['DATA_TYPE']; ?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>