Browse Source

Updated to PHP 7 postgres lib

Thomas Buck 5 years ago
parent
commit
41cbaf9940
3 changed files with 34 additions and 39 deletions
  1. 1
    2
      README.md
  2. 1
    0
      config.example.php
  3. 32
    37
      weight.php

+ 1
- 2
README.md View File

1
 # Weight-Track
1
 # Weight-Track
2
 
2
 
3
-Simple PHP SQL JavaScript weight tracker. Depends on the old PHP (5) MySQL library.
3
+Simple PHP SQL JavaScript weight tracker. Depends on the PHP Postgres library.
4
 
4
 
5
 Copy the included config.example.php to config.php and set the contents accordingly.
5
 Copy the included config.example.php to config.php and set the contents accordingly.
6
 
6
 
7
 Everything else should pretty much be self-explanatory.
7
 Everything else should pretty much be self-explanatory.
8
-

+ 1
- 0
config.example.php View File

1
 <?php
1
 <?php
2
 
2
 
3
 $sql_host = 'localhost';
3
 $sql_host = 'localhost';
4
+$sql_port = '5432';
4
 $sql_username = 'root';
5
 $sql_username = 'root';
5
 $sql_password = '';
6
 $sql_password = '';
6
 $sql_database = 'weight-test';
7
 $sql_database = 'weight-test';

+ 32
- 37
weight.php View File

46
 function print_json_error($s) {
46
 function print_json_error($s) {
47
     echo '{';
47
     echo '{';
48
     echo '  "error": "' . $s;
48
     echo '  "error": "' . $s;
49
-    if (mysql_errno()) {
50
-        echo ' (' . mysql_error() . ')';
51
-    }
52
     echo '"';
49
     echo '"';
53
     echo '}';
50
     echo '}';
54
 }
51
 }
61
     echo '    </head>';
58
     echo '    </head>';
62
     echo '    <body>';
59
     echo '    <body>';
63
     echo '        <p>' . $s . '</p>';
60
     echo '        <p>' . $s . '</p>';
64
-    if (mysql_errno()) {
65
-        echo '<p>MySQL Error: "' . mysql_error() . '"</p>';
66
-    }
67
     echo '        <a href="index.html">Back to Main-Page...</a>';
61
     echo '        <a href="index.html">Back to Main-Page...</a>';
68
     echo '    </body>';
62
     echo '    </body>';
69
     echo '</html>';
63
     echo '</html>';
80
 include('config.php');
74
 include('config.php');
81
 
75
 
82
 if ((!isset($sql_host))
76
 if ((!isset($sql_host))
77
+        || (!isset($sql_port))
83
         || (!isset($sql_username))
78
         || (!isset($sql_username))
84
         || (!isset($sql_password))
79
         || (!isset($sql_password))
85
         || (!isset($sql_database))) {
80
         || (!isset($sql_database))) {
87
     exit(1);
82
     exit(1);
88
 }
83
 }
89
 
84
 
90
-$db = mysql_connect($sql_host, $sql_username, $sql_password);
91
-mysql_select_db($sql_database);
92
-if (mysql_errno()) {
93
-    print_error('Database Error');
85
+$conn_string = 'host=' . $sql_host . ' port=' . $sql_port . ' dbname=' . $sql_database;
86
+$conn_string = $conn_string . ' user=' . $sql_username . ' password=' . $sql_password;
87
+$db = pg_connect($conn_string);
88
+if (!$db) {
89
+    print_error('Database Error: ' . pg_last_error($db));
94
     exit(1);
90
     exit(1);
95
 }
91
 }
96
 
92
 
97
 $sql = 'CREATE TABLE IF NOT EXISTS weight_users (';
93
 $sql = 'CREATE TABLE IF NOT EXISTS weight_users (';
98
-$sql .= 'id int NOT NULL AUTO_INCREMENT,';
99
-$sql .= 'name varchar(255),';
100
-$sql .= 'PRIMARY KEY (id)';
94
+$sql .= 'id SERIAL,';
95
+$sql .= 'name varchar';
101
 $sql .= ');';
96
 $sql .= ');';
102
-$result = mysql_query($sql);
97
+$result = pg_query($db, $sql);
103
 if (!$result) {
98
 if (!$result) {
104
-    print_error('Error (re-) creating database table for users!');
99
+    print_error('Error (re-) creating database table for users: ' . pg_result_error($result));
105
     exit(1);
100
     exit(1);
106
 }
101
 }
107
 
102
 
108
 $sql = 'CREATE TABLE IF NOT EXISTS weight_data (';
103
 $sql = 'CREATE TABLE IF NOT EXISTS weight_data (';
109
 $sql .= 'id int NOT NULL,';
104
 $sql .= 'id int NOT NULL,';
110
-$sql .= 'date DATETIME,';
105
+$sql .= 'date TIMESTAMP,';
111
 $sql .= 'weight DECIMAL(5,2)';
106
 $sql .= 'weight DECIMAL(5,2)';
112
 $sql .= ');';
107
 $sql .= ');';
113
-$result = mysql_query($sql);
108
+$result = pg_query($db, $sql);
114
 if (!$result) {
109
 if (!$result) {
115
-    print_error('Error (re-) creating database table for weights!');
110
+    print_error('Error (re-) creating database table for weights: ' . pg_result_error($result));
116
     exit(1);
111
     exit(1);
117
 }
112
 }
118
 
113
 
120
         && isset($_POST['date'])
115
         && isset($_POST['date'])
121
         && isset($_POST['time'])
116
         && isset($_POST['time'])
122
         && isset($_POST['weight'])) {
117
         && isset($_POST['weight'])) {
123
-    $sql = 'INSERT INTO weight_data(id, date, weight) VALUES (';
124
-    $sql .= mysql_real_escape_string(str_replace('user_', '', $_POST['username']));
125
-    $sql .= ', ';
118
+    $sql = 'INSERT INTO weight_data(id, date, weight) VALUES (\'';
119
+    $sql .= pg_escape_string(str_replace('user_', '', $_POST['username']));
120
+    $sql .= '\', ';
126
 
121
 
127
     $datetime = $_POST['date'] . $_POST['time'];
122
     $datetime = $_POST['date'] . $_POST['time'];
128
     $timestamp = strtotime($datetime);
123
     $timestamp = strtotime($datetime);
131
         exit(1);
126
         exit(1);
132
     }
127
     }
133
     $mysqltime = date("Y-m-d H:i:s", $timestamp);
128
     $mysqltime = date("Y-m-d H:i:s", $timestamp);
134
-    $sql .= '"' . $mysqltime . '", ';
129
+    $sql .= '\'' . $mysqltime . '\', \'';
135
 
130
 
136
-    $sql .= mysql_real_escape_string($_POST['weight']) . ')';
131
+    $sql .= pg_escape_string($_POST['weight']) . '\')';
137
 
132
 
138
-    $result = mysql_query($sql);
133
+    $result = pg_query($db, $sql);
139
     if (!$result) {
134
     if (!$result) {
140
-        print_error('Error adding new data for user "' . $_POST['username'] . '" to DB! ' . $sql);
135
+        print_error('Error adding new data for user "' . $_POST['username'] . '" to DB: ' . $pg_result_error($result));
141
     } else {
136
     } else {
142
         print_error('Added new data for user "' . $_POST['username'] . '" to DB!');
137
         print_error('Added new data for user "' . $_POST['username'] . '" to DB!');
143
     }
138
     }
144
 } else if (isset($_POST['username'])) {
139
 } else if (isset($_POST['username'])) {
145
-    $sql = 'INSERT INTO weight_users(name) VALUES ("';
146
-    $sql .= mysql_real_escape_string($_POST['username']);
147
-    $sql .= '")';
148
-    $result = mysql_query($sql);
140
+    $sql = 'INSERT INTO weight_users(name) VALUES (\'';
141
+    $sql .= pg_escape_string($_POST['username']);
142
+    $sql .= '\')';
143
+    $result = pg_query($db, $sql);
149
     if (!$result) {
144
     if (!$result) {
150
-        print_error('Error adding new user "' . $_POST['username'] . '" to DB!');
145
+        print_error('Error adding new user "' . $_POST['username'] . '" to DB: ' . pg_result_error($result));
151
     } else {
146
     } else {
152
         print_error('Added new user "' . $_POST['username'] . '" to DB!');
147
         print_error('Added new user "' . $_POST['username'] . '" to DB!');
153
     }
148
     }
204
 EOF;
199
 EOF;
205
 } else {
200
 } else {
206
     $sql = 'SELECT id, name FROM weight_users ORDER BY id ASC';
201
     $sql = 'SELECT id, name FROM weight_users ORDER BY id ASC';
207
-    $result = mysql_query($sql);
202
+    $result = pg_query($db, $sql);
208
     if (!$result) {
203
     if (!$result) {
209
-        print_error('Error fetching users from database!');
204
+        print_error('Error fetching users from database: ' . pg_result_error($result));
210
         exit(1);
205
         exit(1);
211
     }
206
     }
212
     $data = array();
207
     $data = array();
213
-    while ($row = mysql_fetch_array($result)) {
208
+    while ($row = pg_fetch_assoc($result)) {
214
         $sql2 = 'SELECT date, weight FROM weight_data ';
209
         $sql2 = 'SELECT date, weight FROM weight_data ';
215
-        $sql2 .= 'WHERE id = "' . $row['id'] . '" ORDER BY date ASC';
216
-        $result2 = mysql_query($sql2);
210
+        $sql2 .= 'WHERE id = \'' . $row['id'] . '\' ORDER BY date ASC';
211
+        $result2 = pg_query($db, $sql2);
217
         if (!$result2) {
212
         if (!$result2) {
218
-            print_error('Error fetching data for user ' . $row['id'] . ' "' . $row['name'] . '"!');
213
+            print_error('Error fetching data for user ' . $row['id'] . ' "' . $row['name'] . '": ' . pg_result_error($result2));
219
             exit(1);
214
             exit(1);
220
         }
215
         }
221
         $cur = array();
216
         $cur = array();
222
         $cur['name'] = $row['name'];
217
         $cur['name'] = $row['name'];
223
         $cur['id'] = $row['id'];
218
         $cur['id'] = $row['id'];
224
         $cur['data'] = array();
219
         $cur['data'] = array();
225
-        while ($row2 = mysql_fetch_array($result2)) {
220
+        while ($row2 = pg_fetch_assoc($result2)) {
226
             $elem = array();
221
             $elem = array();
227
             $elem['date'] = $row2['date'];
222
             $elem['date'] = $row2['date'];
228
             $elem['weight'] = $row2['weight'];
223
             $elem['weight'] = $row2['weight'];

Loading…
Cancel
Save