Page 1 of 1

I know this is right /MYSQL query results/PHP ECHO

Posted: Fri Dec 26, 2003 5:28 am
by ptysell
I am having a problem with passing the results of a MYSQL query to a PHP function and i know for a fact that i have it correct, because it works in one case but not in a second identical case(copy and pasted code). I have three files, index.php, menu.php, and contentmenu.php. index.php includes bother of the other two files and i know that they are bothing being includeed and thier respective functions are being called based on static HTML output.

I will post the jist of my files, excluding info that is not nessessary.

index.php

Code: Select all

include("../assets/php/menu/menu.php");
include("../assets/php/menu/content_subsection_menu.php");

$currentSectionID = getSectionID();

$subsection_query = "SELECT * FROM subsection WHERE id = '$currentSectionID'";

$subsection_results = dbquery($subsection_query);

genCurrentSubMenu($siteURL, $TESTCSS, $currentSection, $currentSubSection, $subsection_results);
//THIS WORKS EXACTLY AS IT SHOULD

genSubContentMenu($siteURL, $TESTCSS, $currentSection, $currentSubSection, $subsection_results);
//THIS DOES NOT WORK AT ALL
menu.php

Code: Select all

function genCurrentSubMenu($siteURL, $TESTCSS, $currentSection, $currentSubSection, $subsection_results)
	{
	HTML
		while($subsection_data = mysql_fetch_array($subsection_results))
{
echo "$subsection_data[subsection]";
}
}
content_subsection_menu.php

Code: Select all

function genSubContentMenu($siteURL, $TESTCSS, $currentSection, $currentSubSection, $subsection_results)
	{
HTML
		while($subsection_data = mysql_fetch_array($subsection_results))
{
echo "$subsection_data[subsection]";
}
}
The firstone outputs correctly, outputing the text "subsection"

now the 2nd one prints nothing, not even an error code. When i try to echo w/o fetching the array, it gives me a reasource ID # 12 or something.

I see no reason that this should not work. I have many other things that doe not work. I am able to echo other variables that are passed to the function. I just cant seam to get this to work.

what i might do is to write a function that is independent of the HTML code around it, and that jsut echos the data, and then i could jsut call that, but i would rather have it work this way, and i know that this is a run on sentence, and i know there are alot of spe;lling errors, but it is really late and i am really tired.

Hope that this made since.

Thanks

Posted: Fri Dec 26, 2003 8:04 pm
by ptysell
if i comment out the gencurrentsubmenu, the 2nd one works fine, but thehy are exactly the same and there is no error in gencurrentsubmenu. i really need help.

Posted: Fri Dec 26, 2003 8:22 pm
by DuFF
Well your code looks fine to me. Only 1 small error I can see that probably isn't the problem but you should have:

Code: Select all

<?php
echo $subsection_data['subsection']; 
?>
instead of

Code: Select all

<?php
echo "$subsection_data[subsection]"; 
?>
If you are getting back a Resource ID error then chances are that you are passing the MySQL query right to it without retrieving arrays from it. Since it works in the first part and not the second I don't really see how this could be the problem though :? . Could you post an example of the code you are passing into the function thats not working?

Posted: Fri Dec 26, 2003 8:59 pm
by ptysell
i have tried the ['akaswas'] and that does not help. That was the first thing that i tried. The thing is that i have HTML around the echo so it looks something like

Code: Select all

echo "<a href"http://wahtever/$subsection_data[subsection]">";
I am not really sure what code you are asking me to post. The only difference between the two files is the HTML around the $subsection_data[whatever]

I literally copied and pasted the function($var, $var, $var) and the while statement.

Posted: Fri Dec 26, 2003 10:39 pm
by ptysell
not sure if this helps, but in my index file if i

Code: Select all

$TESTVAR = subsection_results;
genCurrentSubMenu($siteURL, $TESTCSS, $currentSection, $currentSubSection, $subsection_results); 
//THIS WORKS EXACTLY AS IT SHOULD 

genSubContentMenu($siteURL, $TESTCSS, $currentSection, $currentSubSection, $TESTVAR);
It still does not work. I am not sure what to do. Am i not allowed to pass MYSQL results to more then one function? I do not see why i could not do that.

The fact that when i set a new variable equal to it and noting happens make me supishious of the function but it is identical so i have no idea.

ANY HELP?

thanks

Posted: Fri Dec 26, 2003 10:49 pm
by infolock
try this and see what happens :

Code: Select all

$subsection_data = mysql_fetch_array($subsection_results);
print_r($subsection_data);

that should at least give you an idea of what arrays it is publishing.

then, you could test it out like this :

Code: Select all

// $subsection_data = mysql_fetch_array($subsection_results);
// print_r($subsection_data);

$sub_section_array = array();

while ($subsection_data = mysql_fetch_array($subsection_results))
{
   $subsection_array[] = $subsection_data['subsection'];
}

// print it out 

for($i=0; $i<=count($subsection_array); $i++)
{
   echo $subsection_array[$i];
   echo '<br />';
}


as far as what code to post, how are you calling the function? could you post how you are calling the function?

Posted: Fri Dec 26, 2003 11:02 pm
by ptysell
Well the functions are being called in the index.php file exactly as i have posted above in the first post.

Posted: Fri Dec 26, 2003 11:13 pm
by infolock
oops, i guess i missed that part :P

did you try what i suggested though?

Posted: Sat Dec 27, 2003 3:19 am
by Weirdan
ptysell wrote: Am i not allowed to pass MYSQL results to more then one function? I do not see why i could not do that.
You are allowed to do this. But keep in mind than each mysql_fetch_* call advances the internal pointer of the result recordset, so if you have fetched all the data from recordset in the while loop its internal pointer is at the end of recordset. So second loop executes exactly 0 times ;)
Either prefetch the data into some array or use [php_man]mysql_data_seek[/php_man](0); before the second loop.

Posted: Sat Dec 27, 2003 4:18 am
by ptysell
mysql_data_seek() worked like a charm, thanks.

i can now move on to my search page then i am done......

i am sure that i will be back, i am moving from perl/ flat files to php/mysql.

i try to do a lot of things that i acnt so ia am sure that i will be back.