query returning null values

I’m stumped on this. I have a query that gets 1 column from a table, then the script counts how many id’s are in that field and then displays, and while it works, it does’t seem to compensate for empty values. code is below
[php]

<?php // Require config.php to connect to venzo MySQL server, and other functions require_once 'config.php'; require_once 'functions.php'; $qry = mysql_query("SELECT id, fullname, label, email, country_code FROM clients c WHERE c.in_aff = 1"); ?>
Reward Members
	<td>Email</td>
	<td>Country</td>
	<td>No. of <br /> Referred Clients</td>
	<td>Total Rewards <br /> Sales (To Date)</td>
</tr>
<?php while($r = mysql_fetch_array($qry)) { $q = mysql_query("SELECT reward_id FROM rewards WHERE user_id = $r[id]"); $row = mysql_fetch_assoc($q); for($i = 0; $i < count($row); $i++) { $tmp = explode(",", $row['reward_id']); } ?>
<tr>
	<td><?=$r['id']?></td>
	<td><?=$r['fullname']?></td>
	<td><?=$r['label']?></td>
	<td><?=$r['email']?></td>
	<td><?=$r['country_code']?></td>
	<td><?=count($tmp)?></td>
	<td></td>
</tr>
<?php } ?>
Client ID Client Name Label
[/php] What's happening is this [code] 7 Kevin Tunes email US 3 79 Aleasha one email US 1 [/code] print_r($tmp) output [code] Array ( [0] => 62 [1] => 394 [2] => 79 ) Array ( [0] => )[/code]

That 1 is supposed to be 0 since she doesn’t have an entry in the rewards table. For whatever reason, instead of count($tmp) returning 0, it detects the space in [ 0 ] => (spaces are intentional) as a value and displays a 1 on the screen.

I’ve tried detecting the space, but i can’t get anything to change the 1 to a 0.

Anyone have any ideas? i have to get this fixed asap.

you can put a default of zero in the database structure when a user has nothing in it ?

Won’t work because if i default it in the database, when i count the number of values in the field, it’ll show up as 1, not 0.

SELECT IFNULL(amount,0) FROM (
SELECT count(*) as amount FROM rewards WHERE user_id = $r[id])

maybe

Didn’t work, thanks for trying though. Looks like i’ll have to find a way to detect the extra response.

I got it, changed the query to
[php]$qry = mysql_query(“SELECT c.id, c.fullname, c.label, c.email, c.country_code, r.reward_id, r.user_id FROM venzo_clients c LEFT JOIN venzo_rewards r ON c.id = r.user_id WHERE c.in_aff = 1”) or die(mysql_error());[/php]

and the loop was changed to [php]

<?php while($r = mysql_fetch_array($qry)) { if($r['reward_id'] !== NULL) { $tmp = explode(" ", $r['reward_id']); $ct = count($tmp); } else { $ct = 0; } ?>[/php]

welcome

I +repped u on that, you got me thinking about the actual problem :slight_smile:

Cool richei thanks for the rep let me know when you find out what you are looking for

Well, that problem was fixed, but now i have another :slight_smile: The last part of this page has me getting total sales data. at this point, i don’t know where i’m going wrong because i didn’t code the sales data page, so i really have no idea how it works.

As i have it now, this is what i have
[php]

<?php while($r = mysql_fetch_array($qry)) { if($r['reward_id'] !== NULL) { $tmp = explode(" ", $r['reward_id']); $ct = count($tmp); } else { $ct = 0; } $qrys = mysql_query("SELECT label, partner_share_currency, extended_partner_share_currency AS psc FROM venzo_itunes_sales WHERE label = '{$r[label]}' AND sales_or_returns = 'S'"); while($row = mysql_fetch_assoc($qrys)) { switch($row['partner_share_currency']) { case 'CNY': $row['psc'] = $row['psc'] * 0.158328; break; case 'EUR': $row['psc'] = $row['psc'] * 0.813460; break; case 'USD': $row['psc'] = $row['psc']; break; case 'CAD': $row['psc'] = $row['psc'] * 0.964782; break; case 'GBP': $row['psc'] = $row['psc'] * 1.46867; break; case 'JPY': $row['psc'] = $row['psc'] * 0.0108447; break; case 'AUD': $row['psc'] = $row['psc'] * 0.847417; break; case 'NZD': $row['psc'] = $row['psc'] * 0.684454; break; case 'MXN': $row['psc'] = $row['psc'] * 0.0783349; break; case 'CHF': $row['psc'] = $row['psc'] * 1.11174; break; case 'NOK': $row['psc'] = $row['psc'] * 0.177831; break; case 'DKK': $row['psc'] = $row['psc'] * 0.18229; break; case 'SEK': $row['psc'] = $row['psc'] * 0.14949; break; } $rows[$row['label']][] = $row; if(mysql_num_rows($qry) != 0) { foreach($rows as $key => $row) { for($i = 0 ; $i < count($row); $i++) { $rows[$key]['total'] += $row[$i]['psc']; } } } } ?>
<tr>
	<td><?=$r['id']?></td>
	<td><?=$r['fullname']?></td>
	<td><?=$r['label']?></td>
	<td><?=$r['email']?></td>
	<td><?=$r['country_code']?></td>
	<td><?=$ct?></td>
<?php foreach($rows as $key => $row) { ?>
	<td><?= $row['psc'] ?></td>
<?php } ?>
</tr>
<?php }?> [/php]

what does it need to do
or what is it doing wrong

What i’m trying to do is use the id’s that i got earlier in the script to run another query to get the label that goes with those id’s. I need the label in order to get the sales figures since that’s how itunes gives it to us.

The the current code, it only gives me 2 of the 4 labels and that’s got my stumped because i if i echo out $tmp[$1], i get all 4 id’s, but i print_r $lab, i only get 2 labels. Updated code is below
[php]while($r = mysql_fetch_assoc($qry)) {

if($r['reward_id'] !== NULL) {
	$tmp = explode(" ", $r['reward_id']);
	$ct = count($tmp);
	
	for($i=0; $i < count($tmp); $i++) {
		//echo $tmp[$i]."<br />";
		$find_label = mysql_query("SELECT label FROM venzo_clients WHERE id = $tmp[$i]") or die(mysql_error());
	}
	
	if(mysql_num_rows($find_label) != 0) {
	
		while($lab = mysql_fetch_array($find_label)) {
			//echo $lab['label']."<br />";
			echo "<pre>";
			print_r($lab);
			echo "</pre>";
			$qrys = mysql_query("SELECT label, partner_share_currency, extended_partner_share_currency AS psc FROM venzo_itunes_sales WHERE label = '{$lab[label]}' AND sales_or_returns = 'S'");
		}
		while($row = mysql_fetch_assoc($qrys)) {
			
			switch($row['partner_share_currency']) {
				case 'CNY':
					$row['psc'] = $row['psc'] * 0.158328;
					break;
				case 'EUR':
					$row['psc'] = $row['psc'] * 0.813460;
					break;
				case 'USD':
					$row['psc'] = $row['psc'];
					break;
				case 'CAD':
					$row['psc'] = $row['psc'] * 0.964782;
					break;
				case 'GBP':
					$row['psc'] = $row['psc'] * 1.46867;
					break;
				case 'JPY':
					$row['psc'] = $row['psc'] * 0.0108447;
					break;
				case 'AUD':
					$row['psc'] = $row['psc'] * 0.847417;
					break;
				case 'NZD':
					$row['psc'] = $row['psc'] * 0.684454;
					break;
				case 'MXN':
					$row['psc'] = $row['psc'] * 0.0783349;
					break;
				case 'CHF':
					$row['psc'] = $row['psc'] * 1.11174;
					break;
				case 'NOK':
					$row['psc'] = $row['psc'] * 0.177831;
					break;
				case 'DKK':
					$row['psc'] = $row['psc'] * 0.18229;
					break;
				case 'SEK':
					$row['psc'] = $row['psc'] * 0.14949;
					break;
			}
			$rows[$row['label']][] = $row;
	
			foreach($rows as $key => $row) {
				for($i = 0 ; $i < count($row); $i++) {
					$rows[$key]['total'] += $row[$i]['psc'];
					$total += $rows[$key]['total'];
				}
			}
		}
	}
} else {
	$ct = 0;
}

?>[/php]

wouldnt you want i to = 1 if you are not wanting a null
beings null would = the 0 in this case

Nulls are being filters out already when i get the id (if($r[‘reward_id’] !== NULL) {). The id’s in the client table are auto generated so i know they’ll never be null.

ok, well i got the labels to retrieve and now i’m having trouble getting the sales because i can’t get the labels to properly implode.

[php]
$find_label = mysql_query(“SELECT label FROM venzo_clients WHERE id IN ($r[reward_id])”) or die(mysql_error());

if(mysql_num_rows($find_label) != 0) {

while($lab = mysql_fetch_assoc($find_label)) {
	for($i=0; $i < count($lab); $i++) {
		$label = implode("', '", $lab);
			echo $label;
	}
		$qrys = mysql_query("SELECT label, partner_share_currency, extended_partner_share_currency AS psc FROM venzo_itunes_sales WHERE label IN ($label) AND sales_or_returns = 'S'");
}

// switch code from previous posts
}[/php]
This is the output from the echo
VG GroupCity CapmoneIn House We Trust
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/content/67/9435167/html/admin/rewards_view.php on line 43
raxinoar
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/content/67/9435167/html/admin/rewards_view.php on line 43

line 43 is the query from $qrys and its failing because the labels are giving me fits.

Have you tired swiching the fetch_assoc to array

while($lab = mysql_fetch_array($find_label)) {

yep, it changes the out to something like
array([0] => label, [label] => label, [1] => label2, [label] => label2)

Are the field names right as its case sensitive with mysql_fetch_assoc ?

So is the out right for the $lab[‘0’] ?

Where did label1 go to in the array ?or is that you making up the output and forgetting that it starts at 0?
[php]array([0] => label, [label] => label, [1] => label2, [label] => label2)[/php]

So does it work the way you expect it to now ?

Not making it up, if i change it to array, this is what i get:
(print_r($lab))
[php]
Array
(
[0] => VG Group
[label] => VG Group
)
Array
(
[0] => City Capmone
[label] => City Capmone
)
Array
(
[0] => In House We Trust
[label] => In House We Trust
)

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/content/67/9435167/html/admin/rewards_view.php on line 46

Array
(
[0] => raxinoar
[label] => raxinoar
)

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/content/67/9435167/html/admin/rewards_view.php on line 46
[/php]If i leave it as assoc, it takes out the [ 0 ] in each one.

Yes the fetch_array does both assoc and numeric array incase fields are not right .
You can use instead the of the assoc index the numeric index [‘0’] , so $lab[‘label’] could also be $lab[‘0’].

Try with a mysql_error

[php]if (!$find_label) {
echo $find_label.'
Could not run query: ’ . mysql_error();
exit;
}

if(mysql_num_rows($find_label) > 0) {[/php]

[php]$qrys = mysql_query(“SELECT label, partner_share_currency, extended_partner_share_currency AS psc FROM venzo_itunes_sales WHERE label IN ($label) AND sales_or_returns = ‘S’”);
if (!$qrys) {
echo $qrys.'
Could not run query: ’ . mysql_error();
exit;
}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service