Comparing Function Result

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Comparing Function Result

Post by AliasBDI »

I am using comparing an integer with a function result and it seems to not be working as I need it. My function works fine. It echos a 'mysql_num_rows()' from a query. If the query returns with no records, the function echos '0'. So later in the page I am comparing the function result with a numeric value. Here is the condition:

Code: Select all

if (totalComments($row_conDETAILS['conID'])>0){ echo "true"; }
.

My function right now is echoing any number over 1 (indicating that there is at least one record matching in the select query). So when I check to see if it is greater than 0, it should be true. However, it is never true no matter how many records are in the query result. Any ideas?

I assumed that maybe it was not working because I might be comparing two different kinds of datatype variables (like in ASP), but I didn't think that PHP did that.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

are you returning the value of mysql_num_rows() in the function or just echoing it?
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

I'm echoing it. Here is my function code:

Code: Select all

$query_totalComLIST = "SELECT * FROM con_confessionscomm WHERE conID=$val";
$totalComLIST = mysql_query($query_totalComLIST, $econtrolt411) or die(mysql_error());
$row_totalComLIST = mysql_fetch_assoc($totalComLIST);
if ($row_totalComLIST!=''){
	$total = mysql_num_rows($totalComLIST);
	echo $total;
} else {
	echo 0;
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

AliasBDI wrote:I'm echoing it. Here is my function code:

Code: Select all

$query_totalComLIST = "SELECT * FROM con_confessionscomm WHERE conID=$val";
$totalComLIST = mysql_query($query_totalComLIST, $econtrolt411) or die(mysql_error());
$row_totalComLIST = mysql_fetch_assoc($totalComLIST);
if ($row_totalComLIST!=''){
	$total = mysql_num_rows($totalComLIST);
	echo $total;
} else {
	echo 0;
}
Get rid of the echo and use return...

Code: Select all

<?php
function myfunction() {
    $query_totalComLIST = "SELECT * FROM con_confessionscomm WHERE conID=$val";
    $totalComLIST = mysql_query($query_totalComLIST, $econtrolt411) or die(mysql_error());
    $row_totalComLIST = mysql_fetch_assoc($totalComLIST);
    if ($row_totalComLIST!=''){
	return mysql_num_rows($totalComLIST);
    } else {
	return 0;
    }
}
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

why are you even fetching the results if your not using them?

Code: Select all

<?php
function myfunction() {
    $query_totalComLIST = "SELECT * FROM con_confessionscomm WHERE conID=$val";
    $totalComLIST = mysql_query($query_totalComLIST, $econtrolt411) or die(mysql_error());
    return mysql_num_rows($totalComLIST);
}
?>
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

I am using echo instead of return becuase return will simply not output anything. I did replace my function with your more simplified version, Jcart. I didn't know that you could simply output the total rows without first assigning it to a variable. Here is my function now:
.

Code: Select all

function totalComments($val) {
    $econtrolt411 = mysql_pconnect("foo", "foo", "foo") or trigger_error(mysql_error(),E_USER_ERROR);
	mysql_select_db("ect411", $econtrolt411);
	$query_totalComLIST = "SELECT * FROM con_confessionscomm WHERE conID=$val";
    $totalComLIST = mysql_query($query_totalComLIST, $econtrolt411) or die(mysql_error());
    echo mysql_num_rows($totalComLIST);
}
Here is the issue now, when I set a condition using the function, it echos the function out and does not use it in the condition.

Code: Select all

if (totalComments($row_conDETAILS['conID'])>0){ echo "foo"; }
This might help. In ASP, I can call a function like this and have it replace the function with the result. And the function does not have to echo anything. The trick is have the function name the same as the variable you assign the function result to. So in php it would look like this:

Code: Select all

function totalComments($val) {
    $econtrolt411 = mysql_pconnect("foo", "foo", "foo") or trigger_error(mysql_error(),E_USER_ERROR);
	mysql_select_db("ect411", $econtrolt411);
	$query_totalComLIST = "SELECT * FROM con_confessionscomm WHERE conID=$val";
    $totalComLIST = mysql_query($query_totalComLIST, $econtrolt411) or die(mysql_error());
    $totalComments = mysql_num_rows($totalComLIST);
}
But that does not work because the fucntion is not a variable name. Is there a way to do this? That may do the trick for me.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

yes, you have to return the variable for it to be used within the if statement.. echo'ing it will only echo it..

By NOT returning it, your if statement is basically being evaluated as

Code: Select all

if (null > 0) { echo "foo"; }
I think what your after is

Code: Select all

$totalComments = totalComments($row_conDETAILS['conID']);
echo $totalComments;

if ($totalComments > 0){ 
   echo "foo"; 
}
Also.. you do not have to declare your connection and database within the function if you have previously declared this prior to the function as it will automatically use the last assigned connection id/database.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

I realize since I have the declared my connection earlier in the page, that I don't have to do it in each function. But as soon as I remove it from the function, the function errors. That is why it is there. And I have the connection at the top of the page.

Oh well.

The if statement still does not work. The function returns '1'. So the if statement (if (1>0){ }) should work, right?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

try it :wink:
Post Reply