Domcsore Posted March 23, 2010 Share Posted March 23, 2010 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 More sharing options...
maca134 Posted March 23, 2010 Share Posted March 23, 2010 The last line of the function needs to be something like $this->query = @mysql_query($q); Then $row = mysql_fetch_array($this->query) will work if the line is inside a function in the same class as the function 'dbselect' Link to comment https://forums.phpfreaks.com/topic/196221-oop-mysql-select/#findComment-1030438 Share on other sites More sharing options...
Domcsore Posted March 23, 2010 Author Share Posted March 23, 2010 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 More sharing options...
maca134 Posted March 23, 2010 Share Posted March 23, 2010 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 Link to comment https://forums.phpfreaks.com/topic/196221-oop-mysql-select/#findComment-1030441 Share on other sites More sharing options...
Domcsore Posted March 23, 2010 Author Share Posted March 23, 2010 Hmn i'm still having trouble but I think this is on my behalf, i'm getting the query error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in... cheers for the advice though =) Link to comment https://forums.phpfreaks.com/topic/196221-oop-mysql-select/#findComment-1030443 Share on other sites More sharing options...
maca134 Posted March 23, 2010 Share Posted March 23, 2010 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 More sharing options...
Domcsore Posted March 23, 2010 Author Share Posted March 23, 2010 Oh, I just types the db name incorrectly in my ini file . However, this works, sort of, im only getting and output of "Array" none of the data is being outputed Dom Sore Link to comment https://forums.phpfreaks.com/topic/196221-oop-mysql-select/#findComment-1030449 Share on other sites More sharing options...
maca134 Posted March 23, 2010 Share Posted March 23, 2010 thats is because the function mysql_fetch_array() outputs an array. Try var_dump($output) and see what happens Link to comment https://forums.phpfreaks.com/topic/196221-oop-mysql-select/#findComment-1030453 Share on other sites More sharing options...
Domcsore Posted March 23, 2010 Author Share Posted March 23, 2010 I have it working but I still have to use return $this->query['columnheader']; I'm trying to get that ['columnheader'] to be inputed automatically? Dom Sore Link to comment https://forums.phpfreaks.com/topic/196221-oop-mysql-select/#findComment-1030462 Share on other sites More sharing options...
maca134 Posted March 23, 2010 Share Posted March 23, 2010 mysql_fetch_assoc is the function you require http://php.net/manual/en/function.mysql-fetch-assoc.php Link to comment https://forums.phpfreaks.com/topic/196221-oop-mysql-select/#findComment-1030463 Share on other sites More sharing options...
Domcsore Posted March 23, 2010 Author Share Posted March 23, 2010 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 More sharing options...
KevinM1 Posted March 23, 2010 Share Posted March 23, 2010 PHP already has a couple of built-in OOP database interfaces - mysqli and pdo. Unless you're writing this for your own educational needs, I suggest not reinventing the wheel. Link to comment https://forums.phpfreaks.com/topic/196221-oop-mysql-select/#findComment-1030475 Share on other sites More sharing options...
Domcsore Posted March 23, 2010 Author Share Posted March 23, 2010 I didn't know this haha, I just started writing with OOP yesterday, silly me. Cheers! Link to comment https://forums.phpfreaks.com/topic/196221-oop-mysql-select/#findComment-1030479 Share on other sites More sharing options...
trq Posted March 23, 2010 Share Posted March 23, 2010 I didn't know this haha, I just started writing with OOP yesterday, silly me. Cheers! I wouldn't exactly (or at all actually) call that OOP, but you have to start somewhere. Link to comment https://forums.phpfreaks.com/topic/196221-oop-mysql-select/#findComment-1030482 Share on other sites More sharing options...
PFMaBiSmAd Posted March 23, 2010 Share Posted March 23, 2010 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 More sharing options...
Domcsore Posted March 23, 2010 Author Share Posted March 23, 2010 No I understand this, but the problem I have is still unsolved... I kind of got muddled up in what I was trying to say. What I was trying to do was turn the array key into an integer. Link to comment https://forums.phpfreaks.com/topic/196221-oop-mysql-select/#findComment-1030497 Share on other sites More sharing options...
ignace Posted March 23, 2010 Share Posted March 23, 2010 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.