SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A custom function question.

    Hi,

    I'm trying to write a function that grabs certain fields from a database query.

    PHP Code:
    function QueryVars($start$end)
    {
        
    $question = array();
        for(
    $i=$start $i <= $end$i++)
        {
        
    $question[$i] = $row["quest_".$i];
        }
        return 
    $question;
    }

    $sql "QUERY";//QUERY is a sub for my actual SQL
    $result mysql_query($sql);
    $row mysql_fetch_assoc($result);

    $question QueryVars(121);
    var_dump($question); 
    When I dump out that array I get all NULL values.
    array(21) { [1]=> NULL [2]=> NULL [3]=> NULL [4]=> NULL [5]=> NULL [6]=> NULL [7]=> NULL [8]=> NULL [9]=> NULL [10]=> NULL [11]=> NULL [12]=> NULL [13]=> NULL [14]=> NULL [15]=> NULL [16]=> NULL [17]=> NULL [18]=> NULL [19]=> NULL [20]=> NULL [21]=> NULL }
    Can I get some help on what I'm doing wrong?
    Thanks!
    No, I REALLY dislike having to use Joomla.

  2. #2
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because $row is undefined within the QueryVars function. You need another parameter to the function and pass in your $row variable.
    PHP questions? RTFM
    MySQL questions? RTFM

  3. #3
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A function is it's own little world, nothing from outside can be seen or changed from within. Only way to see it is to send it in as a parameter or set the variable as global (setting as parameter is prefered though)

    PHP Code:
    function QueryVars($start$end$row

        
    $question = array(); 
        for(
    $i=$start $i <= $end$i++) 
        { 
        
    $question[$i] = $row["quest_".$i]; 
        } 
        return 
    $question


    $sql "QUERY";//QUERY is a sub for my actual SQL 
    $result mysql_query($sql); 
    $row mysql_fetch_assoc($result); 

    $question QueryVars(121$row); 
    var_dump($question); 
    chrome is a wrapper that combines a browser with spyware

  4. #4
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks to both of you.
    No, I REALLY dislike having to use Joomla.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •