php simple function has odd behavior

tdillon80
tdillon80 used Ask the Experts™
on
I'm at my ends wit with this. This is a simple stupid function that just doesn't want to work. I simplified it incredibly for the sake of the question. The first code snippet is of a function that should produce 82, but does not. The second code snippet has the function lines (2 of them) commented out and the code runs just fine.

Please help me figure out what I'm doing wrong.

Thanks.
<?php
// THIS DOES NOT PRODUCE 82
function getReviews() {
	mysql_select_db($database_EBW_Conn, $EBW_Conn);
	$query_rsReviews = "SELECT count(r.review_id) as cnt, pp.itemname, pp.itemid, ra.percent_approved FROM product_price pp, reviews r, ratings_aggregated ra WHERE pp.entity_id = r.entity_id AND r.entity_id = ra.entity_id AND pp.itemsection = 'toys' AND pp.itemurl = 'rubber-duckie' AND r.status_id = 1 GROUP BY pp.itemname";
	$rsReviews = mysql_query($query_rsReviews, $EBW_Conn);
	$row_rsReviews = mysql_fetch_assoc($rsReviews);
	$totalRows_rsReviews = mysql_num_rows($rsReviews);
		if ($totalRows_rsReviews > 0) {
			do {
				echo "4 ";
				echo ($row_rsReviews['cnt']); // 82
			} while ($row_rsReviews = mysql_fetch_assoc($rsReviews));
		}
	mysql_free_result($rsReviews);	
}
	
getReviews();
?>

Open in new window

<?php
// THIS DOES PRODUCE 82
//function getReviews() {
	mysql_select_db($database_EBW_Conn, $EBW_Conn);
	$query_rsReviews = "SELECT count(r.review_id) as cnt, pp.itemname, pp.itemid, ra.percent_approved FROM product_price pp, reviews r, ratings_aggregated ra WHERE pp.entity_id = r.entity_id AND r.entity_id = ra.entity_id AND pp.itemsection = 'toys' AND pp.itemurl = 'rubber-duckie' AND r.status_id = 1 GROUP BY pp.itemname";
	$rsReviews = mysql_query($query_rsReviews, $EBW_Conn);
	$row_rsReviews = mysql_fetch_assoc($rsReviews);
	$totalRows_rsReviews = mysql_num_rows($rsReviews);
		if ($totalRows_rsReviews > 0) {
			do {
				echo "4 ";
				echo ($row_rsReviews['cnt']); // 82
			} while ($row_rsReviews = mysql_fetch_assoc($rsReviews));
		}
	mysql_free_result($rsReviews);	
//}
	
	
getReviews();
?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
I don't see anything obviously wrong with the first example.  Add just the connection info ($database_EBW_Conn, $EBW_Conn) to the beginning of the first example and run it as it is to see if it works.  I suspect it will work and that your problem may be in the code you haven't shown.
Project Lead
Commented:
well ... you have to declare the global variables $database_EBW_Conn, $EBW_Conn inside the function to make it work..

Author

Commented:
How would I do that Kshna?
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
$database_EBW_Conn is the name of the database and $EBW_Conn is the results of a mysql_connect() function.  They are frequently in a separate 'include' file that sets up the connection and selects the database for you.
You have to declare global variables:

function getReviews() {
      global $database_EBW_Conn, $EBW_Conn
      mysql_select_db($database_EBW_Conn, $EBW_Conn);
      $query_rsReviews = "SELECT count(r.review_id) as cnt, pp.itemname, pp.itemid, ra.percent_approved FROM product_price pp, reviews r, ratings_aggregated ra WHERE pp.entity_id = r.entity_id AND r.entity_id = ra.entity_id AND pp.itemsection = 'toys' AND pp.itemurl = 'rubber-duckie' AND r.status_id = 1 GROUP BY pp.itemname";
      $rsReviews = mysql_query($query_rsReviews, $EBW_Conn);
      $row_rsReviews = mysql_fetch_assoc($rsReviews);
      $totalRows_rsReviews = mysql_num_rows($rsReviews);
            if ($totalRows_rsReviews > 0) {
                  do {
                        echo "4 ";
                        echo ($row_rsReviews['cnt']); // 82
                  } while ($row_rsReviews = mysql_fetch_assoc($rsReviews));
            }
      mysql_free_result($rsReviews);      
}
      
Sorry, forgot the final semicolon:

 global $database_EBW_Conn, $EBW_Conn;
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
It should look like this.
<?php
$dbhost = "yourhost";    // Your database server
$dbuser = "youruser";      // Your db username
$dbpass = "yourpassword";      // Your db password
$database_EBW_Conn = "dbname";      // Your database name
$EBW_Conn = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$EBW_Conn) die ("Could not Connect to MySQL server.");

// THIS DOES NOT PRODUCE 82
function getReviews() {
        mysql_select_db($database_EBW_Conn, $EBW_Conn);
        $query_rsReviews = "SELECT count(r.review_id) as cnt, pp.itemname, pp.itemid, ra.percent_approved FROM product_price pp, reviews r, ratings_aggregated ra WHERE pp.entity_id = r.entity_id AND r.entity_id = ra.entity_id AND pp.itemsection = 'toys' AND pp.itemurl = 'rubber-duckie' AND r.status_id = 1 GROUP BY pp.itemname";
        $rsReviews = mysql_query($query_rsReviews, $EBW_Conn);
        $row_rsReviews = mysql_fetch_assoc($rsReviews);
        $totalRows_rsReviews = mysql_num_rows($rsReviews);
                if ($totalRows_rsReviews > 0) {
                        do {
                                echo "4 ";
                                echo ($row_rsReviews['cnt']); // 82
                        } while ($row_rsReviews = mysql_fetch_assoc($rsReviews));
                }
        mysql_free_result($rsReviews);  
}
        
getReviews();
?>

Open in new window

Sandeep KothariProject Lead

Commented:
its simple...

if all your varaible are decalred outside the function in some config file..

than use

global $database_EBW_Conn, $EBW_Conn;

as your first line in the function getReviews()



Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
@OmniUnlimited, the lack of the global declaration may be why the first version doesn't work.  Maybe this will work for a test.  If it does, he should put the global declaration in the function in the original code.
<?php
$dbhost = "yourhost";    // Your database server
$dbuser = "youruser";      // Your db username
$dbpass = "yourpassword";      // Your db password
$database_EBW_Conn = "dbname";      // Your database name
$EBW_Conn = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$EBW_Conn) die ("Could not Connect to MySQL server.");

// THIS DOES NOT PRODUCE 82
function getReviews() {
        global $database_EBW_Conn, $EBW_Conn;
        mysql_select_db($database_EBW_Conn, $EBW_Conn);
        $query_rsReviews = "SELECT count(r.review_id) as cnt, pp.itemname, pp.itemid, ra.percent_approved FROM product_price pp, reviews r, ratings_aggregated ra WHERE pp.entity_id = r.entity_id AND r.entity_id = ra.entity_id AND pp.itemsection = 'toys' AND pp.itemurl = 'rubber-duckie' AND r.status_id = 1 GROUP BY pp.itemname";
        $rsReviews = mysql_query($query_rsReviews, $EBW_Conn);
        $row_rsReviews = mysql_fetch_assoc($rsReviews);
        $totalRows_rsReviews = mysql_num_rows($rsReviews);
                if ($totalRows_rsReviews > 0) {
                        do {
                                echo "4 ";
                                echo ($row_rsReviews['cnt']); // 82
                        } while ($row_rsReviews = mysql_fetch_assoc($rsReviews));
                }
        mysql_free_result($rsReviews);  
}
        
getReviews();
?>

Open in new window

Author

Commented:
So the global declaration definitely fixed the function. Thanks for everybody's help. Now onto part 2.

I am calling this function from a second file, see code snippet but it isn't working. I imagine I'm over looking something small again.

Thanks in advance



<?php
require_once('../Connections/EBW_Conn.php'); 


function getReviews($sectionurl, $itemurl) {
	global $database_EBW_Conn, $EBW_Conn;
	mysql_select_db($database_EBW_Conn, $EBW_Conn);
	$query_rsReviews = "SELECT count(r.review_id) as cnt, pp.itemname, pp.itemid, ra.percent_approved FROM product_price pp, reviews r, ratings_aggregated ra WHERE pp.entity_id = r.entity_id AND r.entity_id = ra.entity_id AND pp.itemsection = '" . $sectionurl ."' AND pp.itemurl = '" . $itemurl ."' AND r.status_id = 1 GROUP BY pp.itemname";
	$rsReviews = mysql_query($query_rsReviews, $EBW_Conn);	
			$row_rsReviews = mysql_fetch_assoc($rsReviews);
			$totalRows_rsReviews = mysql_num_rows($rsReviews);
			if ($totalRows_rsReviews > 0) {
				do { 	
					echo $row_rsReviews['cnt'];
				} while ($row_rsReviews = mysql_fetch_assoc($rsReviews));	
			}
	mysql_free_result($rsReviews);
	}
?>

Open in new window

<?php 
$sectionurl = "toys"; 
$itemurl = "rubber-duckie";

require_once('inc/reviews.php'); 

getReviews($sectionurl, $itemurl); 
?>

Open in new window

Author

Commented:
I got it to work.
I changed the 'require_once' to 'include' inside the file calling the function.
Inside my file containing the function I changed the path of the connection file to be relative to the file calling the function.

Author

Commented:
Splitting the points only because kshna gave the response first but OmniUnLimited actually presented it.

Thanks guys.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial