Solved

Mysql select is not working inside a user defined php function

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP strings vs array errors 13 32
Currency in SQL? 2 30
How to make a good PHP + MySQL + JS pagination system? 3 32
Use Select Query to Return Results as a Form 9 23
Creating and Managing Databases with phpMyAdmin in cPanel.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

860 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