-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmySQL2JsonDataTable.php
More file actions
64 lines (61 loc) · 1.49 KB
/
Copy pathmySQL2JsonDataTable.php
File metadata and controls
64 lines (61 loc) · 1.49 KB
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
<?php
function GetArrayFromTableColumns($mySQLresult)
{
$cols = array();
$fields = mysql_num_fields($mySQLresult);
for($i=0; $i < $fields; $i++) {
$name = mysql_field_name($mySQLresult, $i);
$mySQLtype = mysql_field_type($mySQLresult, $i);
switch(strtoupper($mySQLtype)) {
case "INTEGER":
case "INT":
case "SMALLINT":
case "TINYINT":
case "MEDIUMINT":
case "BIGINT":
case "DECIMAL":
case "NUMERIC":
case "FLOAT":
case "DOUBLE":
$type = "number";
break;
case "DATETIME":
$type = "datetime";
break;
default:
$type = "Value";
}
$cols[] = array('label' => $name, 'type' => $type);
}
return $cols;
}
function GetmArrayFromTableRows($mySQLresult)
{
$rows = array();
$fields = mysql_num_fields($mySQLresult);
while($myrow = mysql_fetch_assoc($mySQLresult)) {
$row = array();
for($i=0; $i < $fields; $i++) {
$mySQLtype = mysql_field_type($mySQLresult, $i);
$mySQLfieldname = mysql_field_name($mySQLresult, $i);
switch(strtoupper($mySQLtype)) {
case "DATETIME":
$row[] = array('v' => 'Date(' . date('Y,n,d,H,i,s', strtotime($myrow[$mySQLfieldname])).')');
break;
default:
$row[] = array('v' => $myrow[$mySQLfieldname]);
}
}
$rows[] = array('c' => $row);
}
return $rows;
}
function GetJSONfrommySQL($mySQLresult){
$table = array(
'cols' => (GetArrayFromTableColumns($mySQLresult)),
'rows' => (GetmArrayFromTableRows($mySQLresult))
);
return json_encode($table);
}
// echo GetJSONfrommySQL($result);
?>