Преглед изворни кода

Updated to PHP 7 postgres lib

Thomas Buck пре 5 година
родитељ
комит
41cbaf9940
3 измењених фајлова са 34 додато и 39 уклоњено
  1. 1
    2
      README.md
  2. 1
    0
      config.example.php
  3. 32
    37
      weight.php

+ 1
- 2
README.md Прегледај датотеку

@@ -1,8 +1,7 @@
1 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 5
 Copy the included config.example.php to config.php and set the contents accordingly.
6 6
 
7 7
 Everything else should pretty much be self-explanatory.
8
-

+ 1
- 0
config.example.php Прегледај датотеку

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

+ 32
- 37
weight.php Прегледај датотеку

@@ -46,9 +46,6 @@
46 46
 function print_json_error($s) {
47 47
     echo '{';
48 48
     echo '  "error": "' . $s;
49
-    if (mysql_errno()) {
50
-        echo ' (' . mysql_error() . ')';
51
-    }
52 49
     echo '"';
53 50
     echo '}';
54 51
 }
@@ -61,9 +58,6 @@ function print_html($s) {
61 58
     echo '    </head>';
62 59
     echo '    <body>';
63 60
     echo '        <p>' . $s . '</p>';
64
-    if (mysql_errno()) {
65
-        echo '<p>MySQL Error: "' . mysql_error() . '"</p>';
66
-    }
67 61
     echo '        <a href="index.html">Back to Main-Page...</a>';
68 62
     echo '    </body>';
69 63
     echo '</html>';
@@ -80,6 +74,7 @@ function print_error($s) {
80 74
 include('config.php');
81 75
 
82 76
 if ((!isset($sql_host))
77
+        || (!isset($sql_port))
83 78
         || (!isset($sql_username))
84 79
         || (!isset($sql_password))
85 80
         || (!isset($sql_database))) {
@@ -87,32 +82,32 @@ if ((!isset($sql_host))
87 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 90
     exit(1);
95 91
 }
96 92
 
97 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 96
 $sql .= ');';
102
-$result = mysql_query($sql);
97
+$result = pg_query($db, $sql);
103 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 100
     exit(1);
106 101
 }
107 102
 
108 103
 $sql = 'CREATE TABLE IF NOT EXISTS weight_data (';
109 104
 $sql .= 'id int NOT NULL,';
110
-$sql .= 'date DATETIME,';
105
+$sql .= 'date TIMESTAMP,';
111 106
 $sql .= 'weight DECIMAL(5,2)';
112 107
 $sql .= ');';
113
-$result = mysql_query($sql);
108
+$result = pg_query($db, $sql);
114 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 111
     exit(1);
117 112
 }
118 113
 
@@ -120,9 +115,9 @@ if (isset($_POST['username'])
120 115
         && isset($_POST['date'])
121 116
         && isset($_POST['time'])
122 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 122
     $datetime = $_POST['date'] . $_POST['time'];
128 123
     $timestamp = strtotime($datetime);
@@ -131,23 +126,23 @@ if (isset($_POST['username'])
131 126
         exit(1);
132 127
     }
133 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 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 136
     } else {
142 137
         print_error('Added new data for user "' . $_POST['username'] . '" to DB!');
143 138
     }
144 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 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 146
     } else {
152 147
         print_error('Added new user "' . $_POST['username'] . '" to DB!');
153 148
     }
@@ -204,25 +199,25 @@ if (isset($_POST['username'])
204 199
 EOF;
205 200
 } else {
206 201
     $sql = 'SELECT id, name FROM weight_users ORDER BY id ASC';
207
-    $result = mysql_query($sql);
202
+    $result = pg_query($db, $sql);
208 203
     if (!$result) {
209
-        print_error('Error fetching users from database!');
204
+        print_error('Error fetching users from database: ' . pg_result_error($result));
210 205
         exit(1);
211 206
     }
212 207
     $data = array();
213
-    while ($row = mysql_fetch_array($result)) {
208
+    while ($row = pg_fetch_assoc($result)) {
214 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 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 214
             exit(1);
220 215
         }
221 216
         $cur = array();
222 217
         $cur['name'] = $row['name'];
223 218
         $cur['id'] = $row['id'];
224 219
         $cur['data'] = array();
225
-        while ($row2 = mysql_fetch_array($result2)) {
220
+        while ($row2 = pg_fetch_assoc($result2)) {
226 221
             $elem = array();
227 222
             $elem['date'] = $row2['date'];
228 223
             $elem['weight'] = $row2['weight'];

Loading…
Откажи
Сачувај