is it possible?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

this line...

Code: Select all

get_data($query, "");
...sends the query to another function, i would really like to see that function if you can find it.

Mark
specialchild
Forum Newbie
Posts: 9
Joined: Tue Nov 25, 2003 3:01 am
Location: Soho, London, UK

Post by specialchild »

sorry was having a retard moment just then, here you go:

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;
	}
}
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

now the query is being sent to a class called ps_DB

Could you post that class?

Jeez this is getting complicated

Mark
specialchild
Forum Newbie
Posts: 9
Joined: Tue Nov 25, 2003 3:01 am
Location: Soho, London, UK

Post by specialchild »

sorry about this, ireally appreciate the help:

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;
		}
	}
}
Post Reply