Connecting Tech Pros Worldwide Forums | Help | Site Map

Query Function Array

Member
 
Join Date: Jul 2007
Posts: 73
#1: Jul 11 '08
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:

http://bytes.com/forum/thread632487.html

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($qry, $this->database_link) or die ("Error: ".mysql_error());
$returnArray = array();
$i = 0;
while ($row = mysql_fetch_array($result, MYSQL_BOTH));
if($row)
$returnArray[$i++] = $row;
mysql_free_result($result);
return $returnArray;
}
[/PHP]


I'm using the following code to access the function:
[PHP]
$parent_array = $db->query("SELECT * FROM table WHERE parent='Y' AND username='$username'");
[/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's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#2: Jul 12 '08

re: Query Function Array


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).
Member
 
Join Date: Jul 2007
Posts: 73
#3: Jul 12 '08

re: Query Function Array


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.
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#4: Jul 12 '08

re: Query Function Array


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/).
Member
 
Join Date: Jul 2007
Posts: 73
#5: Jul 12 '08

re: Query Function Array


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.
Reply