Avatar of tdillon80
tdillon80
 asked on

php simple function has odd behavior

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

PHP

Avatar of undefined
Last Comment
tdillon80

8/22/2022 - Mon
Dave Baldwin

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.
ASKER CERTIFIED SOLUTION
Sandeep Kothari

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
tdillon80

ASKER
How would I do that Kshna?
Dave Baldwin

$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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
OmniUnlimited

Sorry, forgot the final semicolon:

 global $database_EBW_Conn, $EBW_Conn;
Dave Baldwin

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 Kothari

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()



Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Dave Baldwin

@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

tdillon80

ASKER
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

tdillon80

ASKER
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.
Your help has saved me hundreds of hours of internet surfing.
fblack61
tdillon80

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

Thanks guys.