Re: [PHP-DB] First and Last ID of a table

2007-07-11 Thread Peter Beckman

On Wed, 11 Jul 2007, Matt Leonhardt wrote:


<[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]

SELECT MIN(id), MAX(id) FROM mytable


As an aside, is you are using associative arrays, be sure to use the
following keys:

$array['MIN(id)'] and $array['MAX(id)']

Just something I figured out recently :)


 or use select min(id) min, max(id) max from mytable

 then access as $array['min'] and $array['max']

---
Peter Beckman  Internet Guy
[EMAIL PROTECTED] http://www.angryox.com/
---
** PLEASE NOTE PurpleCow.com IS NOW AngryOx.com DO NOT USE PurpleCow.com **
** PurpleCow.com is now owned by City Auto Credit LLC as of May 23, 2007 **
---

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



Re: [PHP-DB] First and Last ID of a table

2007-07-11 Thread tg-php
Sounds about right... you can also do something like this (syntax should be 
right):

SELECT MIN(id) as minid, MAX(id) as maxid FROM mytable


$array['minid'] and $array['maxid']


Basically it's going to be whatever the heading of that column is.  Using "as" 
gives it an alias for less ugly headings.

If you did SELECT COUNT(Qty) FROM SomeTable

Then you might get:

$array['Count of Qty'] or something goofy like that.  I forget the exact 
circumstances but there's times you get goofy stuff like that.

If you run the SQL through some DB client (like mysql's command line stuff or I 
use WinSQL Lite in Windows to connect to our MySQL database across the network) 
you can usually see what the heading name is going to end up being, if you 
don't explicitly set it with an "AS" clause.

"AS" also works on table names:

SELECT l.LeadID, ld.FirstName FROM Leads as l, LeadData as ld
WHERE l.LeadID = ld.LeadID

(actually a lot of the time you can leave out the "as" and just do "Leads l")

Fun times..

-TG


= = = Original message = = =

<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> SELECT MIN(id), MAX(id) FROM mytable

As an aside, is you are using associative arrays, be sure to use the 
following keys:

$array['MIN(id)'] and $array['MAX(id)']

Just something I figured out recently :)

Matt 

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


___
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

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



Re: [PHP-DB] First and Last ID of a table

2007-07-11 Thread Matt Leonhardt
<[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> SELECT MIN(id), MAX(id) FROM mytable

As an aside, is you are using associative arrays, be sure to use the 
following keys:

$array['MIN(id)'] and $array['MAX(id)']

Just something I figured out recently :)

Matt 

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



Re: [PHP-DB] Newbie alert: supplied argument is not a valid MySQL result resource

2007-07-11 Thread Matt Leonhardt
""lameck kassana"" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> hey just make this
>  $sid = mysql_fetch_array($query) or die ('Could not fetch from database:
>> ' ." mysql_error().");
>  try this

Yep.  I figured that out yesterday.  Hit the nail right on the head.  As 
I've learned from experimentation, the pipe operator takes precedence to the 
assignment operator so I was basically evaluating whether my query or a die 
statement returned a true value (which the query statement was, so it never 
processed the die()) and then assigning "true," or 1 to $query.  The 'or' 
operator by contrast processes after the assignment, so the following line:

$query = mysql_query("some SQL") or die();

first assigns a handle to $query (assuming the query is valid, which in my 
case it was) and then evaluated the value of query.  This leaves me with two 
remaining questions.  I'm coming from a Perl background, so I'm wondering if 
there's a documentation page somewhere for PHP operators (something along 
the likes of perlop) and as an experiment I tried the following code, and it 
behaved exactly like the code I first posted, which I'm not sure why:

($query = mysql_query("valid SQL")) || die();

Shouldn't everything within the first set of parentheses here evaluate as 
the first || condition?

Thanks for everyone's help,
Matt 

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



Re: [PHP-DB] First and Last ID of a table

2007-07-11 Thread tg-php
SELECT MIN(id), MAX(id) FROM mytable

:)

Hope that helps!

-TG

= = = Original message = = =

I have a table where I need to figure out the very first ID and the  
very last ID, so here is what I wrote:

$first_query = "SELECT id FROM mytable ORDER BY id LIMIT 1";
$first_result = mysql_query($first_query,$con);
$first_id = mysql_result($first_result,0,'id');

$last_query = "SELECT id FROM mytable ORDER BY id DESC LIMIT 1";
$last_result = mysql_query($last_query,$con);
$last_id = mysql_result($last_result,0,'id');

I'm just wondering if there was any way to do this more efficiently,  
like with one query instead of two. Or is this about as simple as I  
can do it?

Thanks.



-- 
Kevin Murphy
Webmaster: Information and Marketing Services
Western Nevada College
www.wnc.edu
775-445-3326


___
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

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



[PHP-DB] First and Last ID of a table

2007-07-11 Thread Kevin Murphy
I have a table where I need to figure out the very first ID and the  
very last ID, so here is what I wrote:


$first_query = "SELECT id FROM mytable ORDER BY id LIMIT 1";
$first_result = mysql_query($first_query,$con);
$first_id = mysql_result($first_result,0,'id');

$last_query = "SELECT id FROM mytable ORDER BY id DESC LIMIT 1";
$last_result = mysql_query($last_query,$con);
$last_id = mysql_result($last_result,0,'id');

I'm just wondering if there was any way to do this more efficiently,  
like with one query instead of two. Or is this about as simple as I  
can do it?


Thanks.



--
Kevin Murphy
Webmaster: Information and Marketing Services
Western Nevada College
www.wnc.edu
775-445-3326




Re: [PHP-DB] Newbie alert: supplied argument is not a valid MySQL result resource

2007-07-11 Thread Niel
Hi

> On my way to bed so too tired too look closely, but at a guess I'd say
> it's a logic error.  I would guess this:
> 
> >$sid = mysql_fetch_array($query) || die ('Could not fetch from database: 
> > ' . mysql_error());   // <---  line 49
> 
>  isn't evaluating in the order you expect.. You're using a logic
> statement as an assignment, and the result of that get's assigned to your
> variable not the resource you expect. Try it like this to see if it works:
> 
> $sid = mysql_fetch_array($query);
> if ($sid === false) die ('Could not fetch from database:  ' . mysql_error()); 
> 
> If that's not the problem, I haven't got a clue until after sleep and
> coffee.

I've had some sleep, and coffee, and I see I missed something silly.

I should of suggested this

 // get sid and write cookies
   $query = mysql_query("SELECT MAX(SID) FROM Sessions");
  if ($query === false)  die ('Could not query database: ' . mysql_error());
   $sid = mysql_fetch_array($query)
  if ($sid === false) die ('Could not fetch from database:  ' . mysql_error()); 


--
Niel Archer

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



Re: [PHP-DB] Newbie alert: supplied argument is not a valid MySQL result resource

2007-07-11 Thread lameck kassana

hey just make this
 $sid = mysql_fetch_array($query) or die ('Could not fetch from database:

' ." mysql_error().");

 try this