Solved

Mysql select is not working inside a user defined php function

Posted on 2011-09-14
2
335 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
Comment Utility
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
Comment Utility
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now