TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   anyone have a function for these (http://www.talkphp.com/absolute-beginners/3616-anyone-have-function-these.html)

sarmenhb 11-16-2008 06:33 PM

anyone have a function for these
 
I'm trying to figure out how i can put these tasks into a function so i cant shorten my coding. here is what I've tried

Code:

function fetch($query,$field) {
$qResult = mysql_query($query);
while($row = mysql_fetch_assoc($qResult) {

return $row['$field'];

}
}

what have u tried?

Tanax 11-16-2008 06:53 PM

php Code:
public function loadFetch($assoc = FALSE, $sql = NULL) {
           
            unset($this->query_fetch);
            empty($this->query_fetch);
       
            if($sql == NULL)
            {
               
                if(isset($this->query_result))
                {
                   
                    if($assoc == FALSE)
                    {
                       
                        while($fetch = mysql_fetch_array($this->query_result))
                        {
                           
                            $this->query_fetch[] = $fetch;
                           
                        }
                       
                        if(!$this->query_fetch)
                        {
                           
                            throw new Exception('An error occured while fetching the array.' . mysql_error());
                           
                        }
                       
                        else return $this;
                       
                    }
                   
                    elseif($assoc == TRUE)
                    {
                       
                        while($fetch = mysql_fetch_array($this->query_result, MYSQL_ASSOC))
                        {
                           
                            $this->query_fetch[] = $fetch;
                           
                        }
                       
                        if(!$this->query_fetch)
                        {
                           
                            throw new Exception('An error occured while fetching the array.' . mysql_error());
                           
                        }
                       
                        else return $this;
                       
                    }
                   
                    else throw new Exception('First parameter can only be TRUE or FALSE.');
                   
                }
               
                else throw new Exception('No query has been executed.');
               
            }
           
            else
            {
               
                if($assoc == FALSE)
                {
                   
                    while($fetch = mysql_fetch_array($sql))
                    {
                       
                        $this->query_fetch[] = $fetch;
                       
                    }
                   
                    if(!$this->query_fetch)
                    {
                       
                        throw new Exception('An error occured while fetching the array.');
                       
                    }
                   
                    else return $this;
                   
                }
               
                elseif($assoc == TRUE)
                {
                   
                    while($fetch = mysql_fetch_array($sql, MYSQL_ASSOC))
                    {
                       
                        $this->query_fetch[] = $fetch;
                       
                    }
                   
                    if(!$this->query_fetch)
                    {
                       
                        throw new Exception('An error occured while fetching the array.');
                       
                    }
                   
                    else return $this;
                   
                }
               
                else throw new Exception('First parameter can only be TRUE or FALSE.');
               
            }
           
        }

You will have to edit it though so it works with your core.
This is what I use anyways..

codefreek 11-16-2008 07:42 PM

XD ;) Tanax ^^ when is the advanced programmer under your name coming ^^
i think it's time hihi :)

Enfernikus 11-16-2008 08:28 PM

The MYSQL_ASSOC constant in PHP is really just an integer that equates to one, a good chunk of that code you've written there could be greatly reduced with the use of a ternary operator. Unless I'm horribly mistaken ( I could be, I haven't tested this ) this should work.

PHP Code:

<?php

function loadFetch$assoc false$sql null )
{

    unset( 
$this->query_fetch );
    empty( 
$this->query_fetch );

    
$sql = ( is_null($sql) && isset($this->query_result) ) ? $this->query_result $sql;
    
$fetchType = ( $assoc ) ? 0;

    if ( !empty(
$sql) )
    {
        while ( 
$fetch mysql_fetch_array($sql$fetchType) )
        {
            
$this->query_result[] = $fetch;
        }

        if ( !
is_array($this->query_fetch) )
        {
            throw new 
Exception"An error occured while fetching the array" mysql_error() );
        } else {
            return 
$this;
        }
    }

}

?>


Tanax 11-16-2008 08:44 PM

That looks ridiculously easier and better xD!

Though, I'm unsure about this:
PHP Code:

$fetchType = ( $assoc ) ? 0

I think it has to be:

PHP Code:

$fetchType = ( $assoc ) ? MYSQL_ASSOC ''

or??

Thanks for the input on my function :-)

Enfernikus 11-18-2008 08:35 PM

Well on a var_dump of MYSQL_ASSOC it produced an integer of 1, so 0 and not filling it in at all would be the next best assumption ( Yes yes I know... Any kind of input is different from absolutely no input ) so maybe the what you've put up is probably the safer bet.

Tanax 11-18-2008 10:16 PM

Ye, I've actually already implented this in my class.

Problem is:
if I choose assoc as FALSE, then the function will be
PHP Code:

mysql_fetch_array($this->query_result0); 

and that doesn't work, because if we don't want a CONST there, we shouldn't place ANYTHING there, including we should REMOVE the "," after the query result.

Also, the 1 didn't work either. I had to do this:
PHP Code:

$fetchType = ($assoc == true) ? MYSQL_ASSOC ''

but as I said earlier.. the '' don't work either.. it's just that if no constant will be used, you shouldn't use the 2nd parameter in the fetch_array function..

Any ideas how to solve this?

Btw: My code works as long as the assoc is true(which in most of my cases, if not all, is set to true).

Enfernikus 11-18-2008 11:06 PM

PHP Code:

 $fetchType = ($assoc == true) ? MYSQL_ASSOC MYSQL_NUM


Tanax 11-19-2008 12:06 AM

Ahyee, I will try this asap!

zxt3st 11-19-2008 03:23 AM

yeah this could be pretty useful :)


All times are GMT. The time now is 11:35 AM.

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