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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ptysell
Forum Commoner
Posts: 34
Joined: Tue Dec 02, 2003 9:27 pm
Location: USC

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

Post 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
ptysell
Forum Commoner
Posts: 34
Joined: Tue Dec 02, 2003 9:27 pm
Location: USC

Post 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.
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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?
ptysell
Forum Commoner
Posts: 34
Joined: Tue Dec 02, 2003 9:27 pm
Location: USC

Post 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.
ptysell
Forum Commoner
Posts: 34
Joined: Tue Dec 02, 2003 9:27 pm
Location: USC

Post 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
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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?
ptysell
Forum Commoner
Posts: 34
Joined: Tue Dec 02, 2003 9:27 pm
Location: USC

Post by ptysell »

Well the functions are being called in the index.php file exactly as i have posted above in the first post.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

oops, i guess i missed that part :P

did you try what i suggested though?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
ptysell
Forum Commoner
Posts: 34
Joined: Tue Dec 02, 2003 9:27 pm
Location: USC

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