Code: Select all
get_data($query, "");Mark
Moderator: General Moderators
Code: Select all
get_data($query, "");Code: Select all
function get_data($query, $fieldnames){
$db = new ps_DB;
$db->query($query);
$db->next_record();
if ($fieldnames){
$i=0;
$data_array=array();
while ($i < $db->num_rows()){
$row=array();
foreach ($fieldnames as $field){
$row[$field]=$db->f("$field");
}
$db->next_record();
array_push($data_array, $row);
$i++;
}
return $data_array;
}
}Code: Select all
if(!defined("DB_MYSQL")) {
define("DB_MYSQL",1);
// Global defines for connection //
define("DB_HOST", "localhost");
define("DB_NAME", "test");
define("DB_USER", "root");
define("DB_PWD", "");
// DB Object //
class ps_DB {
var $lid = 0; // Link ID for database connection
var $qid = 0; // Query ID for current query
var $row; // Current row in query result set
var $record = array(); // Current row record data
var $error = ""; // Error Message
var $errno = ""; // Error Number
var $pageLen = 10; // Result paging length
// Make a persistent connection to the DB, and returns DB lid //
function connect() {
if ($this->lid == 0) {
$this->lid = mysql_pconnect(DB_HOST,DB_USER,DB_PWD);
if (!$this->lid) {
$this->mysqlError("connect(" . DB_HOST . "," . DB_USER . ",PASSWORD) failed.");
}
if (!@mysql_select_db(DB_NAME,$this->lid)) {
$this->mysqlError("Cannot connect to database ".DB_NAME);
return 0;
}
}
return $this->lid;
}
// Connects to DB, runs query, and sets up the query id for the class. //
function query($q) {
if (empty($q)) {
return 0;
}
if (!$this->connect()) {
return 0;
}
if ($this->qid) {
@mysql_free_result($this->qid);
$this->qid = 0;
}
$this->qid = @mysql_query($q, $this->lid);
$this->row = 0;
$this->errno = mysql_errno();
$this->error = mysql_error();
if (!$this->qid) {
$this->mysqlError("Invalid SQL: ".$q);
}
return $this->qid;
}
// Return next record in result set //
function next_record() {
if (!$this->qid) {
$this->mysqlError("next_record called with no query pending.");
return 0;
}
$this->record = @mysql_fetch_array($this->qid);
$this->row += 1;
$this->errno = mysql_errno();
$this->error = mysql_error();
$stat = is_array($this->record);
return $stat;
}
// A function to return to the start of the recordset //
function rewind() {
$this->row = 0;
return (mysql_data_seek ($this->qid, $this->row));
}
// Return field Value //
function f($field_name) {
return stripslashes($this->record[$field_name]);
}
// Print field value //
function p($field_name) {
print stripslashes($this->record[$field_name]);
}
// Returns the number of rows in query //
function num_rows() {
if ($this->lid) {
return @mysql_numrows($this->qid);
}
else {
return 0;
}
}
// Retrieves the last insert ID from an AUTO_INCREMENTED insert
function insert_id() {
return mysql_insert_id($this->lid);
}
// mySQL error message display //
function mysqlError($msg) {
$this->error = @mysql_error($this->lid);
$this->errno = @mysql_errno($this->lid);
printf("<html>\n<head>\n<title>MySQL Error</title>\n<body>\n\n", $title);
print("<h3>MySQL Error</h3><br>\n");
printf("%s:<br>%s (%s)<br>\n", $msg, $this->errno, $this->error);
print("</body>\n</html>\n");
exit;
}
}
}