Re: [PHP] counting records in db

2006-10-31 Thread Ivo F.A.C. Fokkema
On Mon, 30 Oct 2006 12:28:29 +0100, Robin Vickery wrote:

 On 30/10/06, Ivo F.A.C. Fokkema [EMAIL PROTECTED] wrote:
 On Sun, 29 Oct 2006 23:40:47 -0600, Richard Lynch wrote:
  On Fri, October 27, 2006 4:53 pm, Børge Holen wrote:
  On Friday 27 October 2006 19:34, Richard Lynch wrote:
  And the header(Location: ...) requires a full URL.
 
  No it doesn't. but he's missing an ' at first glance
 
  Yes, it does:
  http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
 
  Note the use of 'absolute' within that section.

 Although I always use a full URL as well, doesn't absolute just mean
 non-relative? As in:
 Location: /Protocols/rfc2616/rfc2616-sec14.html#sec14.30
 (absolute URI)

 Location: ./rfc2616-sec14.html#sec14.30
 (relative URI)
 
 If you need contextual information to make sense of the URI (such as
 the server name from a previous request) then it's not absolute.
 
 RFC 2396: Uniform Resource Identifiers
 
 An absolute identifier refers to a resource independent of the
 context in which the identifier is used. In contrast, a relative
 identifier refers to a resource by describing the difference within a
 hierarchical namespace between the current context and an absolute
 identifier of the resource.

Ah, thanks. I was confusing it with absolute and relative paths.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-31 Thread Roman Neuhauser
# [EMAIL PROTECTED] / 2006-10-27 20:14:58 +0200:
  And the header(Location: ...) requires a full URL.
 Why?

HTTP protocol specification[1] says:

14.30 Location

   The Location response-header field is used to redirect the recipient
   to a location other than the Request-URI for completion of the
   request or identification of a new resource. For 201 (Created)
   responses, the Location is that of the new resource which was created
   by the request. For 3xx responses, the location SHOULD indicate the
   server's preferred URI for automatic redirection to the resource. The
   field value consists of a single absolute URI.

   Location   = Location : absoluteURI

   An example is:

   Location: http://www.w3.org/pub/WWW/People.html

  Note: The Content-Location header field (section 14.14) differs
  from Location in that the Content-Location identifies the original
  location of the entity enclosed in the request. It is therefore
  possible for a response to contain header fields for both Location
  and Content-Location. Also see section 13.10 for cache
  requirements of some methods.

[1] ftp://ftp.rfc-editor.org/in-notes/rfc2616.txt

-- 
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-31 Thread Robin Vickery

On 30/10/06, Robert Cummings [EMAIL PROTECTED] wrote:

On Mon, 2006-10-30 at 12:28 +0100, Robin Vickery wrote:
 On 30/10/06, Ivo F.A.C. Fokkema [EMAIL PROTECTED] wrote:
  On Sun, 29 Oct 2006 23:40:47 -0600, Richard Lynch wrote:
 
   On Fri, October 27, 2006 4:53 pm, Børge Holen wrote:
   On Friday 27 October 2006 19:34, Richard Lynch wrote:
   And the header(Location: ...) requires a full URL.
  
   No it doesn't. but he's missing an ' at first glance
  
   Yes, it does:
   http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
  
   Note the use of 'absolute' within that section.
 
  Although I always use a full URL as well, doesn't absolute just mean
  non-relative? As in:
  Location: /Protocols/rfc2616/rfc2616-sec14.html#sec14.30
  (absolute URI)
 
  Location: ./rfc2616-sec14.html#sec14.30
  (relative URI)

 If you need contextual information to make sense of the URI (such as
 the server name from a previous request) then it's not absolute.

 RFC 2396: Uniform Resource Identifiers

 An absolute identifier refers to a resource independent of the
 context in which the identifier is used. In contrast, a relative
 identifier refers to a resource by describing the difference within a
 hierarchical namespace between the current context and an absolute
 identifier of the resource.

Please note you are quoting from an RFC with the following title:

Uniform Resource Identifiers (URI): Generic Syntax

Pay special attention to Generic Syntax in the title.

The RFC linked by Richard clearly indicates that for the Location
response-header that the field value consists of a single absolute
URI. This currently has the final word for the Location response-header
and therefore is the standard.


What?

Pay special attention to what I wrote.

I wasn't disagreeing with Richard, or the RFC he quoted. The OP
expressed some confusion about what comprised an 'absolute URI' so I
quoted the section of the RFC that defined an 'absolute URI'.

There's no question that a Location header should contain an absolute
URI as its value.

-robin

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-31 Thread Richard Lynch
On Tue, October 31, 2006 4:58 am, Robin Vickery wrote:
 On 30/10/06, Robert Cummings [EMAIL PROTECTED] wrote:
 On Mon, 2006-10-30 at 12:28 +0100, Robin Vickery wrote:
  On 30/10/06, Ivo F.A.C. Fokkema [EMAIL PROTECTED] wrote:
   On Sun, 29 Oct 2006 23:40:47 -0600, Richard Lynch wrote:
  
On Fri, October 27, 2006 4:53 pm, Børge Holen wrote:
On Friday 27 October 2006 19:34, Richard Lynch wrote:
And the header(Location: ...) requires a full URL.
   
No it doesn't. but he's missing an ' at first glance
   
Yes, it does:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
   
Note the use of 'absolute' within that section.
  
   Although I always use a full URL as well, doesn't absolute just
 mean
   non-relative? As in:
   Location: /Protocols/rfc2616/rfc2616-sec14.html#sec14.30
   (absolute URI)
  
   Location: ./rfc2616-sec14.html#sec14.30
   (relative URI)
 
  If you need contextual information to make sense of the URI (such
 as
  the server name from a previous request) then it's not absolute.
 
  RFC 2396: Uniform Resource Identifiers
 
  An absolute identifier refers to a resource independent of the
  context in which the identifier is used. In contrast, a relative
  identifier refers to a resource by describing the difference
 within a
  hierarchical namespace between the current context and an absolute
  identifier of the resource.

 Please note you are quoting from an RFC with the following title:

 Uniform Resource Identifiers (URI): Generic Syntax

 Pay special attention to Generic Syntax in the title.

 The RFC linked by Richard clearly indicates that for the Location
 response-header that the field value consists of a single absolute
 URI. This currently has the final word for the Location
 response-header
 and therefore is the standard.

Please note that the link I posted is part of this document:
http://www.w3.org/Protocols/rfc2616/rfc2616.html

Also note that THIS section of that document:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.2.1
clearly states:
For definitive information on URL syntax and semantics, see
Uniform Resource Identifiers (URI):
 Generic Syntax and Semantics,
and provides a link to:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec17.html#bib42
which in turn links to:
http://www.ietf.org/rfc/rfc2396.txt
which is, in fact, the document cited.

Are we all on the same page now?

Is there any doubt in anybody's mind that the standards clearly
require an absoluteURI, complete with scheme (e.g., http://)?

:-) :-) :-)

I just wanted to make it clear to any newbie reader whose wishful
thinking was leading them astray that they should NOT rely on a
non-absolute URI if they want to be standards-compliant.

It is rare that one cannot take an extra few seconds and compose the
standards-compliant URI.

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-31 Thread Robert Cummings
On Tue, 2006-10-31 at 11:58 +0100, Robin Vickery wrote:
 On 30/10/06, Robert Cummings [EMAIL PROTECTED] wrote:
  On Mon, 2006-10-30 at 12:28 +0100, Robin Vickery wrote:
   On 30/10/06, Ivo F.A.C. Fokkema [EMAIL PROTECTED] wrote:
On Sun, 29 Oct 2006 23:40:47 -0600, Richard Lynch wrote:
   
 On Fri, October 27, 2006 4:53 pm, Børge Holen wrote:
 On Friday 27 October 2006 19:34, Richard Lynch wrote:
 And the header(Location: ...) requires a full URL.

 No it doesn't. but he's missing an ' at first glance

 Yes, it does:
 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30

 Note the use of 'absolute' within that section.
   
Although I always use a full URL as well, doesn't absolute just mean
non-relative? As in:
Location: /Protocols/rfc2616/rfc2616-sec14.html#sec14.30
(absolute URI)
   
Location: ./rfc2616-sec14.html#sec14.30
(relative URI)
  
   If you need contextual information to make sense of the URI (such as
   the server name from a previous request) then it's not absolute.
  
   RFC 2396: Uniform Resource Identifiers
  
   An absolute identifier refers to a resource independent of the
   context in which the identifier is used. In contrast, a relative
   identifier refers to a resource by describing the difference within a
   hierarchical namespace between the current context and an absolute
   identifier of the resource.
 
  Please note you are quoting from an RFC with the following title:
 
  Uniform Resource Identifiers (URI): Generic Syntax
 
  Pay special attention to Generic Syntax in the title.
 
  The RFC linked by Richard clearly indicates that for the Location
  response-header that the field value consists of a single absolute
  URI. This currently has the final word for the Location response-header
  and therefore is the standard.
 
 What?
 
 Pay special attention to what I wrote.

Seems a bit ambiguous :)

 I wasn't disagreeing with Richard, or the RFC he quoted. The OP
 expressed some confusion about what comprised an 'absolute URI' so I
 quoted the section of the RFC that defined an 'absolute URI'.
 
 There's no question that a Location header should contain an absolute
 URI as its value.

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-30 Thread Ivo F.A.C. Fokkema
On Sun, 29 Oct 2006 23:40:47 -0600, Richard Lynch wrote:

 On Fri, October 27, 2006 4:53 pm, Børge Holen wrote:
 On Friday 27 October 2006 19:34, Richard Lynch wrote:
 And the header(Location: ...) requires a full URL.

 No it doesn't. but he's missing an ' at first glance
 
 Yes, it does:
 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
 
 Note the use of 'absolute' within that section.

Although I always use a full URL as well, doesn't absolute just mean
non-relative? As in:
Location: /Protocols/rfc2616/rfc2616-sec14.html#sec14.30
(absolute URI)

Location: ./rfc2616-sec14.html#sec14.30
(relative URI)


 You can argue that they shouldn't have designed the spec that way.
 
 You can argue that it works in all popular browsers.
 
 But there ain't much to argue about what the spec says...

Agreed. One should always follow the spec.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-30 Thread Robin Vickery

On 30/10/06, Ivo F.A.C. Fokkema [EMAIL PROTECTED] wrote:

On Sun, 29 Oct 2006 23:40:47 -0600, Richard Lynch wrote:

 On Fri, October 27, 2006 4:53 pm, Børge Holen wrote:
 On Friday 27 October 2006 19:34, Richard Lynch wrote:
 And the header(Location: ...) requires a full URL.

 No it doesn't. but he's missing an ' at first glance

 Yes, it does:
 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30

 Note the use of 'absolute' within that section.

Although I always use a full URL as well, doesn't absolute just mean
non-relative? As in:
Location: /Protocols/rfc2616/rfc2616-sec14.html#sec14.30
(absolute URI)

Location: ./rfc2616-sec14.html#sec14.30
(relative URI)


If you need contextual information to make sense of the URI (such as
the server name from a previous request) then it's not absolute.

RFC 2396: Uniform Resource Identifiers

An absolute identifier refers to a resource independent of the
context in which the identifier is used. In contrast, a relative
identifier refers to a resource by describing the difference within a
hierarchical namespace between the current context and an absolute
identifier of the resource.

-robin

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-30 Thread Robert Cummings
On Mon, 2006-10-30 at 12:28 +0100, Robin Vickery wrote:
 On 30/10/06, Ivo F.A.C. Fokkema [EMAIL PROTECTED] wrote:
  On Sun, 29 Oct 2006 23:40:47 -0600, Richard Lynch wrote:
 
   On Fri, October 27, 2006 4:53 pm, Børge Holen wrote:
   On Friday 27 October 2006 19:34, Richard Lynch wrote:
   And the header(Location: ...) requires a full URL.
  
   No it doesn't. but he's missing an ' at first glance
  
   Yes, it does:
   http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30
  
   Note the use of 'absolute' within that section.
 
  Although I always use a full URL as well, doesn't absolute just mean
  non-relative? As in:
  Location: /Protocols/rfc2616/rfc2616-sec14.html#sec14.30
  (absolute URI)
 
  Location: ./rfc2616-sec14.html#sec14.30
  (relative URI)
 
 If you need contextual information to make sense of the URI (such as
 the server name from a previous request) then it's not absolute.
 
 RFC 2396: Uniform Resource Identifiers
 
 An absolute identifier refers to a resource independent of the
 context in which the identifier is used. In contrast, a relative
 identifier refers to a resource by describing the difference within a
 hierarchical namespace between the current context and an absolute
 identifier of the resource.

Please note you are quoting from an RFC with the following title:

Uniform Resource Identifiers (URI): Generic Syntax

Pay special attention to Generic Syntax in the title.

The RFC linked by Richard clearly indicates that for the Location
response-header that the field value consists of a single absolute
URI. This currently has the final word for the Location response-header
and therefore is the standard.

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-29 Thread Richard Lynch
On Fri, October 27, 2006 12:14 pm, [EMAIL PROTECTED] wrote:
 And the header(Location: ...) requires a full URL.
 Why?

Because the docs say so?

Because some user agents will not do what you want them to if you don't?

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-29 Thread Richard Lynch
On Fri, October 27, 2006 4:53 pm, Børge Holen wrote:
 On Friday 27 October 2006 19:34, Richard Lynch wrote:
 And the header(Location: ...) requires a full URL.

 No it doesn't. but he's missing an ' at first glance

Yes, it does:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30

Note the use of 'absolute' within that section.

You can argue that they shouldn't have designed the spec that way.

You can argue that it works in all popular browsers.

But there ain't much to argue about what the spec says...

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-27 Thread Satyam
- Original Message - 
From: Paul Novitski [EMAIL PROTECTED]


While we're talking about optimization, I'd want to check to make sure 
COUNT(*) didn't ask MySQL to generate a throw-away recordset consisting of 
all fields.  I wonder if it would be more machine-efficient to use 
COUNT(`last_name`), specifying a single field, in this case the same field 
your query is examining anyway.




If the fields mentioned in the where clause are keys or if there is no where 
clause, a count(*) uses the index trees to make the count, it won't even 
access the data racords at all.  Moreover, it won't even read the whole 
index tree node, just the header record of the block that indicates how many 
key records that block contains.  I don't really know about MySql or any 
database engine, but these are techniques well known in the trade.


On the count(fieldname) issue, it is different from a count(*) and may give 
different results.  When you mentione a field it will only count records 
where that field is not null.  Select count(fieldname) from table is the 
same as doing a select count(*) from table where fieldname is not null.  It 
will take just as longer as processing any additional where clause.


Satyam 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-27 Thread Richard Lynch
Your idea to use 'COUNT(Username)' is just about the ONLY part of that
script that you are doing correctly...

:-)

Start reading here:
http://phpsec.org/

And the header(Location: ...) requires a full URL.

And you should have better error-checking, probably.
[Though maybe you have a custom error handler not apparent]

And your DB password should probably be a hash/crypted value.

And it looks like maybe you are using register_globals on which is bad.

Once again, start reading:
http://phpsec.org/

On Thu, October 26, 2006 1:16 pm, [EMAIL PROTECTED] wrote:
 Would it be ok to use the same code to check if customer is loged in?

 $query = mysql_query(
SELECT COUNT(Username) as NoOfRecords
FROM customers
WHERE Username = '$Username' AND Password =
 '$Password');
 if (mysql_result($query, 0) == 0)
 {
echo 'Please try again';
 }
 else
 {
header('location: index.php);
exit;
 }

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-27 Thread afan
 Your idea to use 'COUNT(Username)' is just about the ONLY part of that
 script that you are doing correctly...

 :-)

:D


 And the header(Location: ...) requires a full URL.
Why?


 And you should have better error-checking, probably.
 [Though maybe you have a custom error handler not apparent]
Right. I skipped that part to not waste space in post. $_POST values are
checked first and then used.

 And your DB password should probably be a hash/crypted value.
Correct. I use this, of course.

 And it looks like maybe you are using register_globals on which is bad.
Nope! globals are off.
:D

 Once again, start reading:
 http://phpsec.org/
I'm Shiflett's fan!
:)

-afan

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-27 Thread Børge Holen
On Friday 27 October 2006 19:34, Richard Lynch wrote:
 Your idea to use 'COUNT(Username)' is just about the ONLY part of that
 script that you are doing correctly...

 :-)

 Start reading here:
 http://phpsec.org/

 And the header(Location: ...) requires a full URL.

No it doesn't. but he's missing an ' at first glance




 And you should have better error-checking, probably.
 [Though maybe you have a custom error handler not apparent]

 And your DB password should probably be a hash/crypted value.

 And it looks like maybe you are using register_globals on which is bad.

 Once again, start reading:
 http://phpsec.org/

 On Thu, October 26, 2006 1:16 pm, [EMAIL PROTECTED] wrote:
  Would it be ok to use the same code to check if customer is loged in?
 
  $query = mysql_query(
 SELECT COUNT(Username) as NoOfRecords
 FROM customers
 WHERE Username = '$Username' AND Password =
  '$Password');
  if (mysql_result($query, 0) == 0)
  {
 echo 'Please try again';
  }
  else
  {
 header('location: index.php);
 exit;
  }
 
  --
  PHP General Mailing List (http://www.php.net/)
  To unsubscribe, visit: http://www.php.net/unsub.php

 --
 Some people have a gift link here.
 Know what I want?
 I want you to buy a CD from some starving artist.
 http://cdbaby.com/browse/from/lynch
 Yeah, I get a buck. So?

-- 
---
Børge
Kennel Arivene 
http://www.arivene.net
---

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-26 Thread Dave Goodchild

$query = mysql_query(
  SELECT COUNT(*) as NoOfRecords
  FROM customers
  WHERE last_name = 'Smith');
$result = mysql_result($query, 0);


Re: [PHP] counting records in db

2006-10-26 Thread afan
WOW! That was fast!
:D

Thanks Dave!

-afan


 $query = mysql_query(
SELECT COUNT(*) as NoOfRecords
FROM customers
WHERE last_name = 'Smith');
 $result = mysql_result($query, 0);


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-26 Thread afan
Hm. There is a little problem - this doesn't work.

Warning: mysql_result(): supplied argument is not a valid MySQL result
resource in /var/www/html/xxx/tests/count.php on line 28

?


 $query = mysql_query(
SELECT COUNT(*) as NoOfRecords
FROM customers
WHERE last_name = 'Smith');
 $result = mysql_result($query, 0);


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-26 Thread Paul Novitski

At 10/26/2006 10:38 AM, [EMAIL PROTECTED] wrote:

what would be better solution to count records in table (e.g., how many
customers have last name 'Smith'):

$query = mysql_query(
SELECT COUNT(*) as NoOfRecords
FROM customers
WHERE last_name = 'Smith');
$result = mysql_fetch_array($query);
$NoOfRecords = $result['NoOfRecords'];

OR

$query = mysql_query(
SELECT cust_id
FROM customers
WHERE last_name = 'Smith');
$NoOfRecords = mysql_num_rows($query);



My understanding of why COUNT() is the better solution is that 
mysql_num_rows() requires MySQL to cycle through the found records on 
a second pass to count them, whereas the first gathers the count 
during the first pass, the record-selection phase.


Of course, if you also need to have the selected records accessible 
to a loop, using COUNT() will force you to execute two queries, one 
for the record count and one for the data themselves, so at that 
point the relative advantage is less clear.


While we're talking about optimization, I'd want to check to make 
sure COUNT(*) didn't ask MySQL to generate a throw-away recordset 
consisting of all fields.  I wonder if it would be more 
machine-efficient to use COUNT(`last_name`), specifying a single 
field, in this case the same field your query is examining anyway.


Regards,
Paul 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-26 Thread afan
Sorry, my bad! It works jsut fine. I did misstake.
:)




  $query = mysql_query(
 SELECT COUNT(*) as NoOfRecords
 FROM customers
 WHERE last_name = 'Smith');
  $result = mysql_result($query, 0);
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-26 Thread Richard Lynch
On Thu, October 26, 2006 12:57 pm, Paul Novitski wrote:
$query = mysql_query(
 SELECT COUNT(*) as NoOfRecords
 FROM customers
 WHERE last_name = 'Smith');
$result = mysql_fetch_array($query);
$NoOfRecords = $result['NoOfRecords'];

This would be faster.
Index on last_name will probably help a great deal.

$query = mysql_query(
 SELECT cust_id
 FROM customers
 WHERE last_name = 'Smith');
$NoOfRecords = mysql_num_rows($query);

This will be slower as MySQL/PHP have to store and maintain a dataset
of all 'Smith' records.

 My understanding of why COUNT() is the better solution is that
 mysql_num_rows() requires MySQL to cycle through the found records on
 a second pass to count them, whereas the first gathers the count
 during the first pass, the record-selection phase.

That too.

 Of course, if you also need to have the selected records accessible
 to a loop, using COUNT() will force you to execute two queries, one
 for the record count and one for the data themselves, so at that
 point the relative advantage is less clear.

You'd have to test on your hardware and your dataset to see which is
faster at that point, I think.

And if the dataset is small enough to get the whole shebang, then you
probably don't care.

And if it's not, then you want LIMIT/OFFSET and then you want
SQL_CALC_FOUND_ROWS or whatever it is with the mysql_num_rows() so you
know how many rows there were without the LIMIT.

 While we're talking about optimization, I'd want to check to make
 sure COUNT(*) didn't ask MySQL to generate a throw-away recordset
 consisting of all fields.  I wonder if it would be more
 machine-efficient to use COUNT(`last_name`), specifying a single
 field, in this case the same field your query is examining anyway.

count(*) is heavily optimized in MySQL, as I understand it.

All this belongs on a MySQL list, not here, anyway.

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-26 Thread afan
Would it be ok to use the same code to check if customer is loged in?

$query = mysql_query(
   SELECT COUNT(Username) as NoOfRecords
   FROM customers
   WHERE Username = '$Username' AND Password = '$Password');
if (mysql_result($query, 0) == 0)
{
   echo 'Please try again';
}
else
{
   header('location: index.php);
   exit;
}

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-26 Thread Dave Goodchild

Why would you want to do that? Think about what you're trying to do. In the
first case you want a COUNT of records in the database, in the second you
just want to see if the user/password combination or whatever exist, so just
use a normal SELECT query, no need to use the wrong tool for the job...!


RE: [PHP] counting records in db

2006-10-26 Thread Brad Fuller
 Would it be ok to use the same code to check if customer is loged in?
 
 $query = mysql_query(
SELECT COUNT(Username) as NoOfRecords
FROM customers
WHERE Username = '$Username' AND Password = '$Password');
 if (mysql_result($query, 0) == 0)
 {
echo 'Please try again';
 }
 else
 {
header('location: index.php);
exit;
 }

Assuming that the 'Username' field is unique, then the COUNT() is not
necessary in this case as the number of returned results would never be
greater than 1.  

A more reasonable approach would be something like this:

?

$query = SELECT UserID, Password FROM customers WHERE Username =
'{$_POST['username']}';
$result = mysql_query($query);

if(mysql_num_rows($result) == 1) {
if($_POST['password'] == mysql_result($result, 0, 'Password')) {
$_SESSION['logged_in'] = 'Yes';
$_SESSION['user_id'] = mysql_result($result, 0, 'UserID');
header('location: index.php);
}else{
echo Invalid Password!;
}
}else{
echo Invalid Username!;
}

?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-26 Thread Paul Novitski

At 10/26/2006 11:16 AM, [EMAIL PROTECTED] wrote:

Would it be ok to use the same code to check if customer is loged in?

$query = mysql_query(
   SELECT COUNT(Username) as NoOfRecords
   FROM customers
   WHERE Username = '$Username' AND Password = '$Password');
if (mysql_result($query, 0) == 0)



I'd question whether you really needed to get a record count.  If 
your logon system is robust, each member will be listed only once; 
even if not, it's probably not the number of records you're 
interested in learning but whether or not there are any.  I'd suffice with:


SELECT Username
FROM customers
WHERE Username = '$Username' AND Password = '$Password'
LIMIT 1,0;

In a case like this, where I'm confident I have either one record or 
none, I'd be happy to use mysqsl_num_rows().


In the example above, which field you select is arbitrary, unless you 
actually want more information about the logged-on user:


SELECT FirstName, LastName, SecurityLevel
FROM customers
WHERE Username = '$Username' AND Password = '$Password'
LIMIT 1,0;

Regards,
Paul

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] counting records in db

2006-10-26 Thread afan
Would it be ok to use the same code to check if customer is loged in?

$query = mysql_query(
SELECT COUNT(Username) as NoOfRecords
FROM customers
WHERE Username = '$Username' AND Password =
 '$Password');
if (mysql_result($query, 0) == 0)

I just realized that the answer I already got in previous answers :)
Didn't pay an atention!
:)

Thanks.

-afan

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php