Solved

Mysql select is not working inside a user defined php function

Posted on 2011-09-14
2
338 Views
Last Modified: 2013-12-13
Hello, I am trying to get some code working where I use recursion to output all the elements of a database for use with a navigation bar.  The structure of the database is simply "record_id", "parent", "sections".  each record will have a parent which will either be another record in the table or "root".  For the time being I just am trying to get it to output as an unordered list.  The issue I'm and having is that inside my function the connection info comes up as undefined.  But on the line just before I call the function it is defined.  I didn't see anything online about select statements not working within user defined functions.  Also, when I take the function out of the function it works perfectly fine (but only for 1 execution).  Any help would be appreciated.

This is my function call
<?php 
	echo $database_NavTest; //outputs proper name of database
	echo $NavTest; //outputs Resource id #3
	query("root"); 
?>

Open in new window


This is my function
<?php 
function query($a){
	echo $NavTest;  //undefined
	echo $a; //outputs root
	$root_rsNav = $a;
	mysql_select_db($database_NavTest, $NavTest); //Both $NavTest and $database_NavTest are undefined 
	$query_rsNav = sprintf("SELECT * FROM Nav WHERE Nav.parent = %s", GetSQLValueString($root_rsNav, "text"));
	$rsNav = mysql_query($query_rsNav, $NavTest) or die(mysql_error()); //$NavTest is undefined
	$row_rsNav = mysql_fetch_assoc($rsNav);
	$totalRows_rsNav = mysql_num_rows($rsNav);
	echo $totalRows_rsNav;
	if($totalRows_rsNav > 0){?> 
		<ul>
		<?php do{?>
        	<li><?php echo $row_rsNav['sections']; ?></li>
            <?php query($row_rsNav['sections']); ?>
        <?php }while($row_rsNav = mysql_fetch_assoc($rsNav)); ?>
		</ul>
	<?php }
} ?>

Open in new window


I'm blotting out the real information but my connection string goes like this:

<?php
$hostname_NavTest = "********";
$database_NavTest = "*********";
$username_NavTest = "********";
$password_NavTest = "********";
$NavTest = mysql_connect($hostname_NavTest, $username_NavTest, $password_NavTest) or trigger_error(mysql_error(),E_USER_ERROR); 
?>

Open in new window

0
Comment
Question by:andrewaiello
  • 2
2 Comments
 
LVL 8

Accepted Solution

by:
Rik-Legger earned 500 total points
ID: 36537539
You should make them 'global'.

<?php
function query($a){
global $hostname_NavTest, $database_NavTest, $username_NavTest, $NavTest;

...
}
?>

Also try experimenting with constants.
0
 
LVL 8

Assisted Solution

by:Rik-Legger
Rik-Legger earned 500 total points
ID: 36537542
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question