CodeCharge Studio
search Register Login  

Visual PHP Web Development

Visually Create Internationalized Web Applications, Web Reports, Calendars, and more.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 Custom code SQL SELECT COUNT not returning what is expected

Print topic Send  topic

Author Message
Aaron


Posts: 145
Posted: 11/07/2007, 6:26 AM

Hey all. I'm adding a PM (private message) to my application. Very simple and I got it to work with just straight up PHP/mySQL in a text editor. But adding it in to CCS seems to not be working quite right.

Here is what I'm trying to do. When the page loads, the header will run some code to look at the database to see if the user has any PMs. If so, it displays how many and how many are new. On the 'BeforeShow' of my header page I have the following custom code:
        $db = new clsDBimperial();  
	  
	$user = $_SESSION['UserID'];  
	$sql = "SELECT COUNT(PMID) FROM PM WHERE toID = '$user' AND deleted <> 'y'";  
  
	$db->query($sql);  
  
	if($db->num_rows()=='1'){  
		$msgCount = $db->f("COUNT(PMID)");  
		echo "You have ".$msgCount." private message";  
	}else{  
		echo "You have ".$msgCount." private messages";  
	}  
  
	$sqlUnopened = "SELECT COUNT(opened) FROM PM WHERE toID = '$user' AND opened = 'n' AND deleted <> 'y'";  
	$db->query($sqlUnopened);  
  
	if($db->num_rows() > '0'){  
		$newMsgCount = $db->f("COUNT(opened)");  
		echo ", ".$newMsgCount." new";  
	}  

I'm sure it's something simple, but I don't know what it is.

Thanks
Aaron
View profile  Send private message
headhunter


Posts: 130
Posted: 11/07/2007, 8:50 AM

What exactly does not work?
Are there any errors?

shouldn't it be (part of the code):

  
if($db->f("COUNT(PMID)") == 1){    
		echo "You have 1 private message";    
	}else{    
		$msgCount = $db->f("COUNT(PMID)");    
		echo "You have ".$msgCount." private messages";    
	}    
  
  
View profile  Send private message
Aaron


Posts: 145
Posted: 11/07/2007, 9:00 AM

It's not retrieving anything from the database. I've checked the query with debug and it's correct. As far as the proper syntax, I'm not sure. I'll try running it again with your suggestion & post back.

Thank you
View profile  Send private message
Aaron


Posts: 145
Posted: 11/07/2007, 9:08 AM

Nope. Does not work. All it shows is:

Quote :
You have private messages

Whereas it should show:

Quote :
You have 5 private messages

If I could just get it to retrieve the value of the Count statement, I could get the rest (I'm pretty sure). But everything I've tried to do to get it, won't work.

I've also tried changing the SQL to say ... SELECT COUNT(PMID) as total from ...
Using 'total' instead of the count statement. That doesn't work either.
View profile  Send private message
aondecker

Posts: 58
Posted: 11/07/2007, 11:10 AM

you could try this


$db = new clsDBimperial();

$user = $_SESSION['UserID'];
$sql = "SELECT PMID FROM PM WHERE toID = '$user' AND deleted <> 'y'";

$db->query($sql);
$rs=$db->next_record();
$x = 0;
while($rs)
{
$x =$x+1;
$rs=$db->next_record();
}

X's output should be whatever the total is.
View profile  Send private message
headhunter


Posts: 130
Posted: 11/07/2007, 11:31 AM

Replace the line

$user = $_SESSION['UserID']; 

with

$user = CCGetUserID();
View profile  Send private message
Aaron


Posts: 145
Posted: 11/07/2007, 12:06 PM

aondecker -- Perfect! Thank you. Works like a charm.

headhunter -- yes, that too will work. Just depends on if you want to write it in "CodeCharge-eeze". Though, since it is in CCS, it's probably best to stick with that methodology.

For those who later find this, here's the finished BeforeShow placed within an included header file.

//Custom Code   
// -------------------------  
  	$db = new clsDBimperial();  
#  	$db->Debug = True;  
#  	$user = $_SESSION['UserID'];  //this is equally valid, but not in CCS-syntax  
	$user = CCGetUserID();  
  
// Get all the PMs for the user where the delete flag has not been set  
  	$sql = "SELECT PMID FROM PM WHERE toID = '$user' AND deleted <> 'y'";  
  	$db->query($sql);  
  
  	$recordSet=$db->next_record();  
	$messageCounter = 0;  
	while($recordSet)  // count how many messages the user has  
	{  
		$messageCounter =$messageCounter+1;  
		$recordSet=$db->next_record();  
	}  
// Check to see if they have one or more messages and choose which message to display  
  	if($messageCounter == 1){  
		echo "You have 1 private message";  
  	}else{  
		echo "You have ".$messageCounter." private messages";  
  	}  
    
// Get all the PMs for the user where the delete flag has not been set AND not been read  
  	$sqlUnopened = "SELECT PMID FROM PM WHERE toID = '$user' AND opened = 'n' AND deleted <> 'y'";  
  	$db->query($sqlUnopened);  
    
  	$newRecordSet=$db->next_record();  
	$newMessageCounter = 0;  
	while($newRecordSet)  // count how many new, unread messages the user has  
	{  
		$newMessageCounter =$newMessageCounter+1;  
		$newRecordSet=$db->next_record();  
	}  
	if($newMessageCounter > 0){  // if they have new, unread messages, display the count  
  		echo ", ".$newMessageCounter." new";  
  	}  
// -------------------------  
//End Custom Code
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Web Database

Join thousands of Web developers who build Web applications with minimal coding.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.