I have 2 scripts that are very similar (location.php and event.php).
location.php executes perfectly however event.php does not.
The SQL in event.php works fine when I run it directly from the database.
My question is why and how I can fix it, I thought it could be an escape character issue for MySQL >php for line 7 of event.php.
Alternatively is it an issue with using user-variables through the php interface?
I have included 'projectDatabase.php' in case there is some connection setup issue I have missed but other scripts using this connection have worked fine.
location.php:
1 <?php
2 require_once 'projectDatabase.php';
3
4 $dbinstance = new projectDatabase();
5
6 $query = "UPDATE items SET latitude = ((RAND() * (51.3805 - 51.3770)) + 51.3770), longitude = ((RAND() * (-2.3320 - -2.3240)) + -2.3240), update_ts = NOW()";
7
8 $dbinstance->db->query($query);
9
10 ?>
event.php:
1 <?php
2 require_once 'projectDatabase.php';
3
4 $dbinstance = new projectDatabase();
5
6 $query = "SELECT @now := NOW();
7 UPDATE catches c JOIN (SELECT item_id, MAX(update_ts) AS ts FROM items GROUP BY item_id) AS i ON c.item_id = i.item_id SET c.spawn_time = i.ts WHERE c.catch_time > @now - INTERVAL 15 MINUTE;
8 UPDATE items SET latitude = ((RAND() * (51.3805 - 51.3770)) + 51.3770), longitude = ((RAND() * (-2.3320 - -2.3240)) + -2.3240), update_ts = @now;";
9
10
11 $dbinstance->db->query($query);
12
13 ?>
projectDatabase.php:
1 <?php
2 class projectDatabase {
3 private $dbHost = 'localhost';
4 private $dbUsername = 'XXXXXXXXX';
5 private $dbPassword = 'XXXXXXXXX';
6 private $dbName = 'XXXXXXXXX';
7
8 function __construct(){
9 if(!isset($this->db)){
10 $link = new mysqli($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbName);
11 if($link->connect_error){
12 die("Failed to connect with MySQL: " . $link->connect_error);
13 }else{
14 $this->db = $link;
15 }
16 }
17 }
18 ?>
The problem is, there's no instance variable named $db
in projectDatabase
class. You need to create one public
instance variable named $db
in projectDatabase
class like this:
class projectDatabase {
public $db;
private $dbHost = 'localhost';
...
}