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.
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();
?>
<?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();
?>
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
How would I do that Kshna?
$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.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Sorry, forgot the final semicolon:
global $database_EBW_Conn, $EBW_Conn;
global $database_EBW_Conn, $EBW_Conn;
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();
?>
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()
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()
@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();
?>
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
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);
}
?>
<?php
$sectionurl = "toys";
$itemurl = "rubber-duckie";
require_once('inc/reviews.php');
getReviews($sectionurl, $itemurl);
?>
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.
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.
ASKER
Splitting the points only because kshna gave the response first but OmniUnLimited actually presented it.
Thanks guys.
Thanks guys.