I'm building a home RESTful web service to manage a que of users to use computers.
To do this I have, amongst other classes, a dbConnector class to manage queries to MySQL database and a getSetInfo class to join or leave the que.
I have a table in MySQL called queList with a field for each user and the first field is the computer's ID. To join a que for a computer, I set the user's field for a given computer ID to the que length + 1.
------Problem------------
When setting a user's place in the que for a computer via a MySQL query, it appears that the mysql query alters the value of the variable holding the queLength +1 in the getSetInfo class' function called setPlaceInQue() (see below).
For the life of me I cannot work out why. Any help would be much appreciated! I've commented the line in the code where the problem pops up.
Thanks so much!
-----dbConnector Code---------
Code: Select all
<?php
////////////////////////////////////////////////////////////////////////////////////////
// Class: DbConnector
// Purpose: Connect to a database, MySQL version
///////////////////////////////////////////////////////////////////////////////////////
class dbConnector {
var $theQuery;
var $link;
function getSettings(){
//System variables
//Database variables
$settings['dbhost'] = 'localhost';
$settings['dbusername'] = 'ksks';
$settings['dbpassword'] = 'pp1222';
$settings['dbname'] = 'compDB';
return $settings;
}
//*** Function: DbConnector, Purpose: Connect to the database ***
function dbConnector(){
// Load settings from parent class
$settings = $this->getSettings();
// Get the main settings from the array we just loaded
$host = $settings['dbhost'];
$db = $settings['dbname'];
$user = $settings['dbusername'];
$pass = $settings['dbpassword'];
// Connect to the database
$this->link = mysql_connect($host, $user, $pass);
mysql_select_db($db);
register_shutdown_function(array(&$this, 'close'));
}
//*** Function: query, Purpose: Execute a database query ***
function query($query) {
$this->theQuery = $query;
return mysql_query($query, $this->link);
}
//*** Function: getQuery, Purpose: Returns the last database query, for debugging ***
function getQuery() {
return $this->theQuery;
}
//*** Function: getNumRows, Purpose: Return row count, MySQL version ***
function getNumRows($result){
return mysql_num_rows($result);
}
//*** Function: fetchArray, Purpose: Get array of query results ***
function fetchArray($result) {
return mysql_fetch_array($result);
}
//*** Function: fetchAssocArrayVertical, Purpose: Get assoc array of query results while ***
function fetchAssocArrayVertical($query = '', $keyName = '', $valName = '') {
$result = $this->query($query);
$verticalAssocArray = array();
while($row = mysql_fetch_array($result)){
$key = $row[$keyName];
$val = $row[$valName];
$verticalAssocArray[$key] = $val;
}
return $verticalAssocArray;
}
//*** Function: close, Purpose: Close the connection ***
function close() {
//mysql_close($this->link);
mysql_close($this->link);
}
}
?>Code: Select all
//////////////////////////////////////////
// Purpose: Jump into the que for a computer
// Ref:
//////////////////////////////////////////
function joinQue($targetComp = '', $userName = ''){
$validator = new validator();
if ($validator->isValidUserName($userName) == true && $validator->isLanComp($targetComp) == true){
$compID = $this->getCompID($targetComp);
$queSpot = $this->getQueLength($targetComp)+1; //This returns the right result
$this->setPlaceInQue($targetComp, $userName, $queSpot);//This feeds in the right $queSpot value
return true;
}
else{
return ('Invalid LAN IP,CompName, or userName supplied to joinQue()');
}
}
//////////////////////////////////////////
// Purpose: Sets place in que
// Ref:
//////////////////////////////////////////
function setPlaceInQue($targetComp = '', $userName = '', $place = ''){
$validator = new validator();
if ($validator->isValidUserName($userName) == true && $validator->isLanComp($targetComp) == true){
$targetCompID = $this->getCompID($targetComp);
$dbConnector = new dbConnector();
$dbConnector->query("UPDATE queList SET $userName = '$place' WHERE compID = '$targetCompID'");//PROBLEM!:This seems to change the value of $place to the length of the que +2.
$dbConnector->close();
return true;
}
else{
return ('Invalid LAN IP,CompName, or userName supplied to setPlaceInQue()');
}
}