Query Function Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jeigh
    New Member
    • Jul 2007
    • 73

    Query Function Array

    Been using PHP for a little while now but have never really looked into using functions and such thinking I didn't really need them (coupled with me be lazy). I've started a new project and have decided to get a handle on them and this tutorial:



    Has helped me quite a lot, I've made some modifications and it for the most part everything seems to be working however this one problem popping up is doing my head in I just can't seem to figure it out. I'm attempting to use the following function:

    [PHP]
    function query($qry)
    {
    if(!isset($this->database_link) ) $this->connect();
    $result = mysql_query($qr y, $this->database_lin k) or die ("Error: ".mysql_error() );
    $returnArray = array();
    $i = 0;
    while ($row = mysql_fetch_arr ay($result, MYSQL_BOTH));
    if($row)
    $returnArray[$i++] = $row;
    mysql_free_resu lt($result);
    return $returnArray;
    }
    [/PHP]


    I'm using the following code to access the function:
    [PHP]
    $parent_array = $db->query("SELEC T * FROM table WHERE parent='Y' AND username='$user name'");
    [/PHP]

    and I've tried echoing some results using the following:
    [PHP]
    echo $parent_array[1];
    echo $parent_array['title'];
    [/PHP]

    But it is just showing up blank, probably somthing simple I'm missing but I've looked over that article many times and tried searching around but I'm coming up with nothing. I've narrowed down the problem as much as I can, I've tested other functions contained within the same file and they work fine, I've also tested the same query not using the function and arrays and it is also working.

    Cheers for any help guys.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Jeigh.

    Your script is probably generating an error. Enable debug messages to find out what's going on (http://bytes.com/forum/thread629295.html).

    Comment

    • Jeigh
      New Member
      • Jul 2007
      • 73

      #3
      Thanks for that link, I was unaware you could turn Error Reporting on for just one page and with this new host I'm with (planning to switch to GoDaddy very soon) dosn't seem to allow me access to the php.ini file from the control panel. This helps a lot.

      The error message I got was:

      Notice: Undefined index: 1 in (address) on line 26

      Line 26 being:

      $title = $parent_array[1];

      Never encountered this error before and couldn't manage to find any helpful information through google.

      Thanks again.

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        That just means that $parent_array[1] has no value. Probably means query() returned no results.

        Try removing the semicolon from the end of your while loop in the query() method.

        Incidentally, you can turn on error reporting in your .htaccess file (http://perishablepress.com/press/200...-via-htaccess/).

        Comment

        • Jeigh
          New Member
          • Jul 2007
          • 73

          #5
          Always ends up being the simplest thing haha, I'm getting some results now. The script is still throwing up some wierd errors but through all my testing the codes become very messy so I assume I'll be able to sort all that out.

          Thanks for all the help, I appreciate it.

          Comment

          Working...