TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-16-2008, 06:33 PM   #1 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default 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?
__________________
no signature set
sarmenhb is offline  
Reply With Quote
Old 11-16-2008, 06:53 PM   #2 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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..
__________________
Tanax is offline  
Reply With Quote
The Following User Says Thank You to Tanax For This Useful Post:
sarmenhb (11-17-2008)
Old 11-16-2008, 07:42 PM   #3 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

XD ;) Tanax when is the advanced programmer under your name coming
i think it's time hihi :)
codefreek is offline  
Reply With Quote
The Following User Says Thank You to codefreek For This Useful Post:
Tanax (11-16-2008)
Old 11-16-2008, 08:28 PM   #4 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

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

}

?>
Enfernikus is offline  
Reply With Quote
The Following 2 Users Say Thank You to Enfernikus For This Useful Post:
codefreek (11-16-2008), Tanax (11-16-2008)
Old 11-16-2008, 08:44 PM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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
__________________
Tanax is offline  
Reply With Quote
Old 11-18-2008, 08:35 PM   #6 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

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.
Enfernikus is offline  
Reply With Quote
Old 11-18-2008, 10:16 PM   #7 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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).
__________________
Tanax is offline  
Reply With Quote
Old 11-18-2008, 11:06 PM   #8 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

PHP Code:
 $fetchType = ($assoc == true) ? MYSQL_ASSOC MYSQL_NUM
Enfernikus is offline  
Reply With Quote
The Following User Says Thank You to Enfernikus For This Useful Post:
Tanax (11-19-2008)
Old 11-19-2008, 12:06 AM   #9 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Ahyee, I will try this asap!
__________________
Tanax is offline  
Reply With Quote
Old 11-19-2008, 03:23 AM   #10 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default

yeah this could be pretty useful :)
zxt3st is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:15 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design