logSql($sql); $result = mysql_query ($sql,self::$link); if (mysql_errno(self::$link) != 0) { echo "returnArray2dAssocTF

"; echo $sql . "
"; echo "

" . mysql_errno(self::$link) . ": " . mysql_error(self::$link) . "
\n"; exit; } $intNumColumns = mysql_num_fields($result) - 1; //make zero base $intNumRecords = mysql_num_rows($result) - 1; //make zero base //Get the table-column names for ($i=0; $i <= $intNumColumns; $i++) { $arrTableFieldStructure[$i][0] = mysql_field_table($result, $i) . "." . mysql_field_name($result, $i); } if ($intNumRecords > -1) { //Put the data into the array //$arrData[0][0]; //Error preventor $intRecord = -1; //initialize row counter while ($row = mysql_fetch_array($result, MYSQL_NUM)) { $intRecord++; //increment the counter for ($intColumn = 0;$intColumn <= $intNumColumns;$intColumn++) { $arrData[$arrTableFieldStructure[$intColumn][0]][$intRecord] = $row[$intColumn]; } } return $arrData; } else { return -1; } } /** * return a 2d associative array with the db field names as the first element * * @param string $sql * @return array $arrData on success or -1 on failure */ public function returnArray2dAssocF($sql) {//returnArray2dAssociative1stElementField2ndElementOrdinal would be a better name //$this->logSql($sql); $result = mysql_query ($sql,self::$link); if (mysql_errno(self::$link) != 0) { echo "returnArray2dAssocF

"; echo $sql . "
"; echo "

" . mysql_errno(self::$link) . ": " . mysql_error(self::$link) . "
\n"; exit; } $intNumColumns = mysql_num_fields($result) - 1; $intNumRecords = mysql_num_rows($result) - 1; //Get the column names for ($i=0; $i <= $intNumColumns; $i++) { $arrTableFieldStructure[$i][0] = mysql_field_name($result, $i); } if ($intNumRecords > -1) { //Put the data into the array //$arrData[0][0]; //Error preventor $intRecord = -1; //initialize row counter while ($row = mysql_fetch_array($result, MYSQL_NUM)) { $intRecord++; //increment the counter for ($intColumn = 0;$intColumn <= $intNumColumns;$intColumn++) { $arrData[$arrTableFieldStructure[$intColumn][0]][$intRecord] = $row[$intColumn]; } } return $arrData; } else { return -1; } } /** * return a zero based 2d array numbers as the first element * * @param string $sql * @return array $arrData on success or -1 on failure */ public function returnArray2dOrdinal($sql) {//returnArray2dAssociative1stElementOrdinal2ndElementOrdinal would be a better name //$this->logSql($sql); $result = mysql_query ($sql,self::$link); if (mysql_errno(self::$link) != 0) { echo "returnArray2dOrdinal

"; echo $sql . "
"; echo "

" . mysql_errno(self::$link) . ": " . mysql_error(self::$link) . "
\n"; exit; } $intNumColumns = mysql_num_fields($result) - 1; //make zero base //echo "intNumColumns=" . $intNumColumns . "
"; $intNumRecords = mysql_num_rows($result) - 1; //make zero base //echo "intNumRecords=" . $intNumRecords . "
"; if ($intNumRecords > -1) { //Put the data into the array $arrData[0][0]; //Error preventor $intRecord = -1; //initialize row counter while ($row = mysql_fetch_array($result, MYSQL_NUM)) { $intRecord++; //increment the counter for ($intColumn = 0;$intColumn <= $intNumColumns;$intColumn++) { $arrData[$intColumn][$intRecord] = $row[$intColumn]; //echo "arrTempData[" . $intColumn . "][" . $intRecord . "]=" . $arrTempData[$intColumn][$intRecord] . "
"; } //echo "
"; } return $arrData; } else { return -1; } } /** * Execute SQL with number of rows affected returned * * @param string $sql * @return integer indicating the number of records affected by the update */ public function executeSql($sql) {//executeSqlReturnRowsAffected would be a better name $this->logSql($sql); $result = mysql_query ($sql,self::$link); if (mysql_errno(self::$link) != 0) { echo "executeSql
\n
\n"; echo $sql . "
\n"; echo "

" . mysql_errno(self::$link) . ": " . mysql_error(self::$link) . "
\n"; exit; } $intNumRowsAffected = mysql_affected_rows(self::$link); return $intNumRowsAffected; } /** * Execute SQL with id of last insert returned * * @param string $sql * @return integer indicating the id field of the newly inserted record */ public function executeSqlInsertID($sql) {//executeSqlReturnInsertID would be a better name // note: the id field in the database must be of type int, not bigint or any other type // also, this only works for simple insert queries, not other types $this->logSql($sql); $result = mysql_query ($sql,self::$link); if (mysql_errno(self::$link) != 0) { echo "executeSqlInsertID
\n
\n"; echo $sql . "
\n"; echo "

" . mysql_errno(self::$link) . ": " . mysql_error(self::$link) . "
\n"; exit; } return mysql_insert_id(self::$link); } /** * return a zero based integer of how many recordes a sql query will return * * @param string $sqlTemp * @return integer $intNumRecords indicating the number of records returned by the query */ public function returnRecordCount($sql) { if ($this->debug == true) { echo $sql . "
\n"; } //$this->logSql($sql); $result = mysql_query ($sql,self::$link); if (mysql_errno(self::$link) != 0) { echo "returnRecordCount

"; echo $sql . "
"; echo "

" . mysql_errno(self::$link) . ": " . mysql_error(self::$link) . "
\n"; exit; } $intNumRecords = mysql_num_rows($result) - 1; //make zero base return $intNumRecords; } /** * this accepts a select query for two columns only and returns a 1 dimensional * array with the value of column one as the array key and the value of column two as the array value * An example of where this is useful would be a table called "settings" * id name value * 1 site_title Amazing Website * 2 contact_email amazing@website.com * * @param string $sql statement returning only two columns. Extra columns will be ignored. * @return array $arrData on success or -1 on failure. */ public function returnArray1dKeyValuePairs($sql) {//returnArray1stElementFieldAsKey2ndElementFieldAsValue would be a better name // accepts a sql statement that returns only two columns // column one is returned as the array key and column two as the array value // if more than two coulmns are returned everything but the first two are ignored if ($this->debug == true) { echo $sql . "
\n"; } //$this->logSql($sql); $result = mysql_query ($sql,self::$link); if (mysql_errno(self::$link) != 0) { echo "returnArray1dKeyValuePairs

"; echo $sql . "
"; echo "

" . mysql_errno(self::$link) . ": " . mysql_error(self::$link) . "
\n"; exit; } $intNumRecords = mysql_num_rows($result); if ($intNumRecords > 0) { //Put the data into the array $arrData = array(); //Error preventor while ($row = mysql_fetch_array($result, MYSQL_NUM)) { $arrData[$row[0]] = $row[1]; } return $arrData; } else { return -1; } } /** * return a 2d array with the fisrt dimension the record ordinal and 2nd * dimension an array with "table.field" equals value * * @param string $sql * @return array $arrData on success or -1 on failure */ public function returnArray2dAssocTFRecordFirst($sql) {//returnArray2dAssociative1stElementOrdinal2ndElementTableDotField would be a better name //echo "TFR1 ".microtime()."
\n"; //$this->logSql($sql); $result = mysql_query ($sql,self::$link); //these echos were to demonstrate a 3-9 second delay in this mysql call //when the same query is run directly on the server in phpMyAdmin it takes only .1 seconds //echo "TFR2 ".microtime()."
\n"; if (mysql_errno(self::$link) != 0) { echo "returnArray2dAssocTFRecordFirst

"; echo $sql . "
"; echo "

" . mysql_errno(self::$link) . ": " . mysql_error(self::$link) . "
\n"; exit; } $intNumColumns = mysql_num_fields($result) - 1; //make zero base $intNumRecords = mysql_num_rows($result) - 1; //make zero base //Get the table-column names for ($i=0; $i <= $intNumColumns; $i++) { $arrTableFieldStructure[$i][0] = mysql_field_table($result, $i) . "." . mysql_field_name($result, $i); } if ($intNumRecords > -1) { //Put the data into the array $intRecord = -1; //initialize row counter while ($row = mysql_fetch_array($result, MYSQL_NUM)) { $intRecord++; //increment the counter for ($intColumn = 0;$intColumn <= $intNumColumns;$intColumn++) { $arrData[$intRecord][$arrTableFieldStructure[$intColumn][0]] = $row[$intColumn]; } } return $arrData; } else { return -1; } } /** * return a 2d array with the fisrt dimension the record ordinal and 2nd * dimension an array with "table.field" equals value * * @param string $sql * @return array $arrData on success or -1 on failure */ public function returnArray2dAssocFRecordFirst($sql) {//returnArray2dAssociative1stElementOrdinal2ndElementField would be a better name //echo "TFR1 ".microtime()."
\n"; //$this->logSql($sql); $result = mysql_query ($sql,self::$link); //these echos were to demonstrate a 3-9 second delay in this mysql call //when the same query is run directly on the server in phpMyAdmin it takes only .1 seconds //echo "TFR2 ".microtime()."
\n"; if (mysql_errno(self::$link) != 0) { echo "returnArray2dAssocFRecordFirst

"; echo $sql . "
"; echo "

" . mysql_errno(self::$link) . ": " . mysql_error(self::$link) . "
\n"; exit; } $intNumColumns = mysql_num_fields($result) - 1; //make zero base $intNumRecords = mysql_num_rows($result) - 1; //make zero base //Get the table-column names for ($i=0; $i <= $intNumColumns; $i++) { $arrTableFieldStructure[$i][0] = mysql_field_name($result, $i); } if ($intNumRecords > -1) { //Put the data into the array $intRecord = -1; //initialize row counter while ($row = mysql_fetch_array($result, MYSQL_NUM)) { $intRecord++; //increment the counter for ($intColumn = 0;$intColumn <= $intNumColumns;$intColumn++) { $arrData[$intRecord][$arrTableFieldStructure[$intColumn][0]] = $row[$intColumn]; } } return $arrData; } else { return -1; } } /** * return the MySQL result resource from mysql_query * that can be passed to one of the mysql_fetch_* functions * * @param string $sql * @return array $arrData on success or -1 on failure */ public function returnMysqlResult($sql/*,$php_mysql_function_name = ""*/) { //$this->logSql($sql); $result = mysql_query ($sql,self::$link); if (mysql_errno(self::$link) != 0) { echo "returnMysqlResult

"; echo $sql . "
"; echo "

" . mysql_errno(self::$link) . ": " . mysql_error(self::$link) . "
\n"; exit; } /* //potential for specialized returns //not implemented switch ($php_mysql_function_name) { case "mysql_fetch_array": return $result; break; case "mysql_fetch_assoc": return $result; break; case "mysql_fetch_object": return $result; break; default: return $result; break; }*/ return $result; } /** * return an array of mysql_fetch_object objects each representing a record from the db * * @param string $sql * @return array $arrData on success or -1 on failure */ public function returnArrayOfMysqlRecordObjects($sql) { //$this->logSql($sql); $result = mysql_query ($sql,self::$link); if (mysql_errno(self::$link) != 0) { echo "returnArrayOfMysqlRecordObjects

"; echo $sql . "
"; echo "

" . mysql_errno(self::$link) . ": " . mysql_error(self::$link) . "
\n"; exit; } $arrRecordObjects = array(); while ($recordObject = mysql_fetch_object($result)) { //var_dump($recordObject); $arrRecordObjects[] = $recordObject; } $arrayCount = count($arrRecordObjects); if ($arrayCount > 0) { return $arrRecordObjects; } else { return null; } } /** * return an array of mysql_fetch_assoc arrays each representing a record from the db * * @param string $sql * @return array $arrData on success or -1 on failure */ public function returnArrayOfMysqlAssociativeArrays($sql) { ////$this->logSql($sql); $result = mysql_query ($sql,self::$link); if (mysql_errno(self::$link) != 0) { echo "returnArrayOfMysqlRecordObjects

"; echo $sql . "
"; echo "

" . mysql_errno(self::$link) . ": " . mysql_error(self::$link) . "
\n"; exit; } $arrAssocArray = array(); while ($recordObject = mysql_fetch_assoc($result)) { //var_dump($recordObject); $arrAssocArray[] = $recordObject; } $arrayCount = count($arrAssocArray); if ($arrayCount > 0) { return $arrAssocArray; } else { return null; } } /** * accepts a SQL string (along with reading some _COOKIE and _SERVER vars) and logs it * * @param string $sql * @return nothing */ public function logSql($sql) { $sql = " INSERT INTO `sqlLog` SET `accountId` = '".mysql_real_escape_string($_COOKIE["vcms_aid"])."', `username` = '".mysql_real_escape_string($_COOKIE["vcms_user"])."', `userId` = '".mysql_real_escape_string($_COOKIE["vcms_id"])."', `ip` = '".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."', `referrer` = '".mysql_real_escape_string($_SERVER['HTTP_REFERER'])."', `sql` = '".mysql_real_escape_string($sql)."'"; //echo $sql . "
"; //exit; $result = mysql_query($sql); if (mysql_errno(self::$link) != 0) { echo "logSql

"; echo $sql . "
"; echo "

" . mysql_errno(self::$link) . ": " . mysql_error(self::$link) . "
\n"; exit; } } } ?>