Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Mysql select is not working inside a user defined php function

Posted on 2011-09-14
2
Medium Priority
?
347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 8

Accepted Solution

by:
Rik-Legger earned 2000 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 2000 total points
ID: 36537542
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
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 count occurrences of each item in an array.
Suggested Courses

636 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