Solved

Mysql select is not working inside a user defined php function

Posted on 2011-09-14
2
343 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 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 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