TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-02-2007, 03:41 PM   #1 (permalink)
The Visitor
 
Blackbrain's Avatar
 
Join Date: Oct 2007
Posts: 4
Thanks: 1
Blackbrain is on a distinguished road
Default 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.
Blackbrain is offline  
Reply With Quote
Old 12-02-2007, 04:02 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-02-2007, 10:47 PM   #3 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

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:
$pResult mysql_query("SELECT COUNT(*) AS NumberOfResults FROM MEMBERS WHERE interest = '$inte' AND age < $age") or mysql_error();
$aRow mysql_fetch_array($pResultMYSQL_NUM);

$iNumber $aRow[0];
echo 
$iNumber;
// Would echo 2 in this case 
Now make it an associative array:

PHP Code:
$pResult mysql_query("SELECT COUNT(*) AS NumberOfResults FROM MEMBERS WHERE interest = '$inte' AND age < $age") or mysql_error();
$aRow mysql_fetch_array($pResultMYSQL_ASSOC);

$iNumber $aRow['NumberOfResults'];
echo 
$iNumber;
// Would also echo out 2 
Hope this helps you out :)
maZtah is offline  
Reply With Quote
Old 12-03-2007, 02:02 AM   #4 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

The array indexes starts from 0. The values don't.

A value of 0 should be logically "no rows returned".
Haris is offline  
Reply With Quote
Old 12-03-2007, 08:59 AM   #5 (permalink)
The Visitor
 
Blackbrain's Avatar
 
Join Date: Oct 2007
Posts: 4
Thanks: 1
Blackbrain is on a distinguished road
Default

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.
Blackbrain is offline  
Reply With Quote
Old 12-03-2007, 09:13 AM   #6 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Quote:
Originally Posted by Blackbrain View Post
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.
PHP automatically executes the query as soon as you want it to.
Haris is offline  
Reply With Quote
The Following User Says Thank You to Haris For This Useful Post:
Blackbrain (12-03-2007)
Old 12-04-2007, 07:23 AM   #7 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

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.
SOCK is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:43 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design