Clinton Posted January 4, 2009 Share Posted January 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/139445-solved-select-count/ Share on other sites More sharing options...
DarkWater Posted January 4, 2009 Share Posted January 4, 2009 $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. Quote Link to comment https://forums.phpfreaks.com/topic/139445-solved-select-count/#findComment-729439 Share on other sites More sharing options...
premiso Posted January 4, 2009 Share Posted January 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/139445-solved-select-count/#findComment-729440 Share on other sites More sharing options...
Clinton Posted January 4, 2009 Author Share Posted January 4, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/139445-solved-select-count/#findComment-729444 Share on other sites More sharing options...
Clinton Posted January 4, 2009 Author Share Posted January 4, 2009 Should I just use num_rows like this? $result = mysql_query("SELECT * FROM tablename"); $num_rows = mysql_num_rows($result); echo $num_rows; Quote Link to comment https://forums.phpfreaks.com/topic/139445-solved-select-count/#findComment-729448 Share on other sites More sharing options...
DarkWater Posted January 4, 2009 Share Posted January 4, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/139445-solved-select-count/#findComment-729449 Share on other sites More sharing options...
Clinton Posted January 4, 2009 Author Share Posted January 4, 2009 Well, I wouldn't say that I've got it but I would say that it makes more sense. :-) Thanks for taking the time to explain it. Quote Link to comment https://forums.phpfreaks.com/topic/139445-solved-select-count/#findComment-729455 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.