Jump to content

[SOLVED] SELECT COUNT ?


Clinton

Recommended Posts

Anybody know where I could adopt a COUNT with at least a 2 grade education? After I get above 9 entries the count goes to 1. Then if I get into the twenties it goes to 2. It's like it's showing the first number but not the second or third or fourth. Any ideas?

 

$query = "SELECT COUNT(*) as num FROM thelist WHERE username = '$username' ORDER BY notices";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

?>
<div id="breadcrumb">
<div class="module_breadcrumb">
	<div>
		<div>
			<div>
    				<span class="breadcrumbs pathway">
                    <?php echo $prefix;?> <?php echo $lname;?>, you currently have <? echo $total_pages['num'];?>   notices.

Link to comment
https://forums.phpfreaks.com/topic/139445-solved-select-count/
Share on other sites

$total_pages = $total_pages[num];

It's that line.  You set $total_pages to a string.  Then, when you do:

<?php
echo $total_pages['num'];
?>

PHP uses the array-like string syntax to grab characters.  'num' evaluates to 0 as an int, so you get:

<?php
echo $total_pages[0];
?>

 

Which grabs the first character.

Link to comment
https://forums.phpfreaks.com/topic/139445-solved-select-count/#findComment-729439
Share on other sites

count(*) is bad practice and inefficient. Use the unique identifier for the table (Primary Key) for that...

 

$query = "SELECT COUNT(id) as num FROM thelist WHERE username = '$username' ORDER BY notices";

 

Should be more efficient, but not sure if this will help your problem, but yea.

Link to comment
https://forums.phpfreaks.com/topic/139445-solved-select-count/#findComment-729440
Share on other sites

I'm just trying to understand this as I read through the manual... How does num come out to 0 because I thought we were setting num in the query? I mean, it's getting the 1-9 somewhere so I thought that num was correct.

 

And I didn't know count(*) was bad practice. Even after I go back and search it seems like that's the way to do it because you can pull the rest of your database information off of instead of running a new query. So I take it running two separate query's trumps?

Link to comment
https://forums.phpfreaks.com/topic/139445-solved-select-count/#findComment-729444
Share on other sites

Okay.  When you get the results of mysql_fetch_assoc(), you get $total_pages to be:

Array
(
    [num] => 21
)

 

Then, you do:

$total_pages = $total_pages[num];

 

That makes $total_pages:

int(21)

 

Because you overwrote it.

 

Now, you go and do:

<?php
echo $total_pages['num'];
?>

 

PHP looks at this and says, "Oh, $total_pages?  We can pretend that's a string (for all we know it could be a string of numbers, so it's fine!), and we'll use array syntax to access individual characters."  Almost like:

Array
(
    [0] => 2
    [1] => 1
)

 

So, anyway, PHP looks at the code again and says, "Now wait.  You can't have a string character index as a string, it needs to be an integer."  They do a typecast to int, and:

(int) 'num';

Evaluates to 0.  That leaves:

<?php
echo $total_pages[0];
?>

 

That tells PHP to grab the first character only.

 

Get it now? ;)

Link to comment
https://forums.phpfreaks.com/topic/139445-solved-select-count/#findComment-729449
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.