Jump to content

mysql count help Please


semperfi

Recommended Posts

Hi still learning php mysql etc and ran into a little issue and haven't been about to figure it out. trying to get/display the invite count for each email her is the php code

<?php
$i = 1;
while($data = mysql_fetch_array($user)){
$count = mysql_fetch_array(mysql_query("SELECT COUNT( * ) FROM `interested` WHERE `invites` = '".$invites['invites']."'"));
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $data['email']; ?></td>
<td><?php echo $count['COUNT( * )']; ?></td>
<td><?php echo $data['signup_time']; ?></td>
<td><?php echo $data['signup_date']; ?></td>
</tr>
<?php
$i++;
  }
?>

 

Everything displays email etc and the invite count displays the total of the highest email that has invited for example joe@j.com has 3 invites and john@j.com has none and so on joe having the most invites at 3 the table displays 3 for all emails (hope that makes sense).

Any help is much appreciated also any better way to write/refactor the code.     

Link to comment
Share on other sites

I don't know what $user is, so maybe there is a better way, but based off what you posted

<?php
$i = 1;
while($data = mysql_fetch_array($user)){
$sql = mysql_query("SELECT COUNT(*) as total FROM `interested` WHERE `invites` = '".$invites['invites']."'")or die(mysql_error()); // remove "or die" after testing
$count = mysql_fetch_array($sql);
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $data['email']; ?></td>
<td><?php echo $count['total']; ?></td>
<td><?php echo $data['signup_time']; ?></td>
<td><?php echo $data['signup_date']; ?></td>
</tr>
<?php
$i++;
  }
?>

Link to comment
Share on other sites

I don't know what $user is, so maybe there is a better way, but based off what you posted

<?php
$i = 1;
while($data = mysql_fetch_array($user)){
$sql = mysql_query("SELECT COUNT(*) as total FROM `interested` WHERE `invites` = '".$invites['invites']."'")or die(mysql_error()); // remove "or die" after testing
$count = mysql_fetch_array($sql);
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $data['email']; ?></td>
<td><?php echo $count['total']; ?></td>
<td><?php echo $data['signup_time']; ?></td>
<td><?php echo $data['signup_date']; ?></td>
</tr>
<?php
$i++;
  }
?>

 

Thank you for your help tried the above and it didnt work. I also should have mentioned the $user. This is in the head of the page for db connection etc.

<?php

include "config_db.php"; 
$user = mysql_query("SELECT * FROM `interested`");
?>

 

Again thank you for your help in trying to get this working been at it for a few more hours and got close but not there yet any more help would be greatly appreciated.

Link to comment
Share on other sites

also, what is $invites?

 

Again sorry should show the db structure.

TABLE `interested` (

  `id` bigint(20) NOT NULL AUTO_INCREMENT,

  `email` varchar(1000) NOT NULL,

  `hash` varchar(8) NOT NULL,

  `invites` int(20) NOT NULL,

  PRIMARY KEY (`id`),

  UNIQUE KEY `hash` (`hash`)

 

Thanks for any help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.