![]() |
Fetch array question
Suppose if there are two row match the following statement. Will fetch array syntax return the exact value or will decrease the value by 1 because they start from 0
$result = mysql_query("SELECT COUNT(*) AS numrows FROM MEMBERS WHERE interest = '$inte' AND age < $age") or mysql_error(); $row = mysql_fetch_array($result); $number = $row[noofrows]; WILL IT BE 2 OR 1. |
It will be 2 because it is counting - and thus it needs to return a value that can be understood by humans. Although array indexes begin with a 0, if you were to call the
count then it would return 2.I can't think of any intellectual way to put this, maybe someone else can put it better than me. But array indexes start at 0, counting starts at 1. If it were to return 0 for 1 item, what would no items return? |
You make it an associative array, so you don't need to have any numbers involved. Instead, you're using the name of the result (numrows). Hope that makes any sense!
Let me clearify with an example.. First we try it with a numeric array: PHP Code:
PHP Code:
|
The array indexes starts from 0. The values don't.
A value of 0 should be logically "no rows returned". |
Thank you for your help. Since i was quite away from php certain basic things are bugging me. One of the top is:
--> When we define a query like $result = mysql_query("Query") or die(); Do we need to initialize it by the if function or will php automatically initiliazes the $result query. |
Quote:
|
Haris is correct, but I think everyone is overlooking a crucial detail; you're only retrieving one row, the value of the COUNT. Regardless of how many records match that SQL statement, there will only ever be one row. Therefore the value will always be accessed at the first index, '0'.
It's wasteful to use fetch_array on a statement like this anyway - use mysql_result() instead. |
| All times are GMT. The time now is 05:52 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0