TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   help with oop (http://www.talkphp.com/general/3449-help-oop.html)

sarmenhb 10-06-2008 01:47 AM

help with oop
 
what i'm trying to do with my class is instead of writeing

while($row = mysql_fetch_assoc($query)) {

echo $row['field'];

}

i'm trying to write my own function that will do this but i dont know or am kind of confuses to how i can output more fields(from sql) and if i did write a function how will i end the while loop with a paranthesis

here is my class

Code:

<?php

class dbconnect {

public $host;
public $username;
public $password;
public $database;
public $table;
public $query;


#===================================
#    Database Connection
#===================================
function connectdb($in_host,$in_username,$in_password,$in_database) {

$conn = mysql_connect($this->host = $in_host,$this->username = $in_username,$this->password=$in_password) or die(mysql_error());
mysql_select_db($this->database=$in_database)or die(mysql_error());

}

#=========================================
#    sql query
#=========================================

function query($in_query) {

$output = mysql_query($this->query=$in_query)or die(mysql_error());
return $output;



}


#=========================================
#    Alternate to mysql_fetch_assoc
#=========================================


function fetch($this->query($in_query),$field)) {

while($row = mysql_fetch_assoc($this->query($in_query)) {


echo $row['$field'];

}


}

if you have a similar class can i take a look at it?

sketchMedia 10-06-2008 03:39 PM

Ok, one or two things wrong with the class.

PHP Code:

$output mysql_query($this->query=$in_query)or die(mysql_error());
return 
$output

Pointless variable, just uses up memory albeit not alot but it all adds up:
PHP Code:

return mysql_query($this->query=$in_query)or die(mysql_error()); 

Would produce the same effect.

Also:
PHP Code:

function fetch($this->query($in_query),$field)) {

while(
$row mysql_fetch_assoc($this->query($in_query)) {


echo 
$row['$field'];


You cant do that within function parameters and also if it did work, it would be useless as you call the same function within the body of the function.

Also each time your while loop loops, you are calling a query and getting a new resource, which will loop infinitely over the top entry in the table.
This will accomplish the same thing without looping infinitely
PHP Code:

    function fetch($in_query$field) {
        
$res $this->query($in_query);
        while(
$row mysql_fetch_assoc($res)) {
            echo 
$row[$field] ;
        }
    } 

As to your actual question, do you mean something like this:
PHP Code:

    function fetch($in_query) {
        
$res $this->query($in_query);
        
$retArr = array();
        while(
$row mysql_fetch_assoc($res)) {
            
$retArr[] = $row;
        }
    return 
$retArr;
    } 

That fetches all the rows from the result set.


All times are GMT. The time now is 04:33 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0