Jump to content

OOP mysql select.


Domcsore

Recommended Posts

Hi there, i'm to develop a script that will select my data from my database using OOP but instead of going calling the table from the straight from the query e.g

 

SELECT * FROM users

 

I have it so the variable is in the function name like:

 

public function dbselect($table, $rows = "*", $where = null, $order = null){
		$q = 'SELECT '.$rows.' FROM '.$table;
		if($where != null){
			$q .= ' WHERE '.$where;
		}
		if($order != null){
			$q .= ' ORDER BY '.$order;
		}
		$query = @mysql_query($q);
}

 

but I cannot seem to come up with a way to call the data. Normally as you know you would have the

$row = mysql_fetch_array($query)

and then you could call it by using

$row['username']

but as it will change it wont have the same column names all the time.

 

I was trying to come up with a way to call them with arrays but I cannot figure it out. Any ideas? Cheers

 

Dom Sore

 

(Sorry if it doesn't make much sense, im confused about it haha)

 

 

Link to comment
https://forums.phpfreaks.com/topic/196221-oop-mysql-select/
Share on other sites

Sorry, it is in a class. The code for it so far is:

<?php
class globalconf{
/** DEFINE VARIABLES **/
public $ini;

/** CONSTRUCT FUNCTION **/
public function __construct(){

	/** PARSING THE INI **/
	$this->ini = parse_ini_file('config.ini');

	/** CONNECT TO DATABASE **/
	$con = @mysql_connect($this->ini['dbHost'],$this->ini['dbUser'],$this->ini['dbPass']);
	if(!$con){
		die('Could not connect to host: '.mysql_error());
	}else{
		$selectdb = @mysql_select_db($this->ini['dbName'],$con);
		if(!$selectdb){
			die('Could not connect to database: '.mysql_error());
		}
	}
}

/** DATABASE FUNCTIONS START HERE **/
public function dbselect($table, $rows = "*", $where = null, $order = null){
		$q = 'SELECT '.$rows.' FROM '.$table;
		if($where != null){
			$q .= ' WHERE '.$where;
		}
		if($order != null){
			$q .= ' ORDER BY '.$order;
		}
		$query = @mysql_query($q);		
}	
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/196221-oop-mysql-select/#findComment-1030440
Share on other sites

After the line 'public $ini;'

Put 'private $query;'

 

and the last line of the dbselect function change from

$query = @mysql_query($q); to $this->query = @mysql_query($q);

 

Now you can create another function like so:

public function fetch_array()
{
  return mysql_fetch_array($this->query);
}

 

This will return the results you after  ;D

Link to comment
https://forums.phpfreaks.com/topic/196221-oop-mysql-select/#findComment-1030441
Share on other sites

you need to make sure the fetch_array function is in the class.

 

also i good idea to output the SQL instead on executing it to check it right.

 

Might be an idea to remove the 'at' at the end of mysql_query(), if the SQL is correct then it wont produce an error.

 

Link to comment
https://forums.phpfreaks.com/topic/196221-oop-mysql-select/#findComment-1030444
Share on other sites

I have tried this:

 

<?php
class globalconf{
/** DEFINE VARIABLES **/
public $ini;
private $query;
private $queryresult;

/** CONSTRUCT FUNCTION **/
public function __construct(){

	/** PARSING THE INI **/
	$this->ini = parse_ini_file('config.ini');

	/** CONNECT TO DATABASE **/
	$con = @mysql_connect($this->ini['dbHost'],$this->ini['dbUser'],$this->ini['dbPass']);
	if(!$con){
		die('Could not connect to host: '.mysql_error());
	}else{
		$selectdb = @mysql_select_db($this->ini['dbName'],$con);
		if(!$selectdb){
			die('Could not connect to database: '.mysql_error());
		}
	}
}

/** DATABASE FUNCTIONS START HERE **/
public function dbselect($table, $rows = "*", $where = null, $order = null){
		$q = 'SELECT '.$rows.' FROM '.$table;
		if($where != null){
			$q .= ' WHERE '.$where;
		}
		if($order != null){
			$q .= ' ORDER BY '.$order;
		}
		$this->query = @mysql_query($q);

		while ($this->queryresult = mysql_fetch_assoc($this->query))
		{
			echo $this->queryresult;	
		}
}
}
?>

 

 

but now im getting the output:

 

ArrayArray

 

haha this is a struggle.

Link to comment
https://forums.phpfreaks.com/topic/196221-oop-mysql-select/#findComment-1030472
Share on other sites

Also, writing an OOP class is a somewhat advanced programming topic. If you are not comfortable enough with which mysql_ functions to use to fetch a row from a result set and know that you have an array at that point, you are not ready to be writing an OOP class.

Link to comment
https://forums.phpfreaks.com/topic/196221-oop-mysql-select/#findComment-1030488
Share on other sites

Unless you're writing this for your own educational needs, I suggest not reinventing the wheel.

 

I second that. You don't want to bother your client (or future maintainers) with half-baked code. Use well-tested and fully documented components or leave it all together.

Link to comment
https://forums.phpfreaks.com/topic/196221-oop-mysql-select/#findComment-1030646
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.