[PHP] Display just 1 record in a query

2010-01-12 Thread deal...@gmail.com

I did a query... then I display records like:

table
  ?php do { ?
tr
  td?php echo $row_cur['tid']; ?/td
  tdnbsp;/td
/tr
?php } while ($row_cur = mysql_fetch_assoc($cur)); ?
/table


Q: but how I i just display a particular record with out the do /  
while loop?


like just the 2nd record only:

i tried
?php echo $row_cur['tid',2]; ?
but this makes an error

or $row_cur('tid',2) --- hmmm what's the syntax?



Thanks,
deal...@gmail.com
[db-10]


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



Re: [PHP] Display just 1 record in a query

2010-01-12 Thread Ryan Sun
though you can fetch twice to get the 2nd row
$row_cur = mysql_fetch_assoc($cur); //skip 1st row
$row_cur = mysql_fetch_assoc($cur);
echo $row_cur['tid'];

you should really modify your sql statement, like 'select xxx from xx order
by xx limit 1, 1'  (limit 1,1 retrieve your 2nd row if you are using mysql)

On Tue, Jan 12, 2010 at 4:52 PM, deal...@gmail.com deal...@gmail.comwrote:

 I did a query... then I display records like:

 table
  ?php do { ?
tr
  td?php echo $row_cur['tid']; ?/td
  tdnbsp;/td
/tr
?php } while ($row_cur = mysql_fetch_assoc($cur)); ?
 /table


 Q: but how I i just display a particular record with out the do / while
 loop?

 like just the 2nd record only:

 i tried
 ?php echo $row_cur['tid',2]; ?
 but this makes an error

 or $row_cur('tid',2) --- hmmm what's the syntax?



 Thanks,
 deal...@gmail.com
 [db-10]


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




Re: [PHP] Display just 1 record in a query

2010-01-12 Thread Ashley Sheridan
On Tue, 2010-01-12 at 13:52 -0800, deal...@gmail.com wrote:

 I did a query... then I display records like:
 
 table
?php do { ?
  tr
td?php echo $row_cur['tid']; ?/td
tdnbsp;/td
  /tr
  ?php } while ($row_cur = mysql_fetch_assoc($cur)); ?
 /table
 
 
 Q: but how I i just display a particular record with out the do /  
 while loop?
 
 like just the 2nd record only:
 
 i tried
 ?php echo $row_cur['tid',2]; ?
 but this makes an error
 
 or $row_cur('tid',2) --- hmmm what's the syntax?
 
 
 
 Thanks,
 deal...@gmail.com
 [db-10]
 
 


Depends on how you're creating running the query. You could do something
like:

echo mysql_result($result, 1, 'fieldname');

Where $result is your result object and 1 is a 0 indexed array, so would
be the second result.

Thanks,
Ash
http://www.ashleysheridan.co.uk




Re: [PHP] Display just 1 record in a query

2010-01-12 Thread Kim Madsen

deal...@gmail.com wrote on 12/01/2010 22:52:

I did a query... then I display records like:

table
  ?php do { ?
tr
  td?php echo $row_cur['tid']; ?/td
  tdnbsp;/td
/tr
?php } while ($row_cur = mysql_fetch_assoc($cur)); ?
/table


Q: but how I i just display a particular record with out the do / while 
loop?


Just use extract($row_cur); before the table starts. That would give you 
first row only


Another approach could be to add  LIMIT 1 to the end of your SQL statement


like just the 2nd record only:

i tried
?php echo $row_cur['tid',2]; ?
but this makes an error

or $row_cur('tid',2) --- hmmm what's the syntax?


Getting only second row, but not the first? That would be using a count 
var and show only data if count == 2



table
?php
$count=0;
do {
$count++;
if($count == 2) {
  echo '
tr
  td'. $row_cur['tid'] .'/td
  tdnbsp;/td
/tr';
  }
} while ($row_cur = mysql_fetch_assoc($cur));
?
/table


Another thing: drop the do and use this syntax instead, it's more readable:

table
?php
$count=0;
while ($row_cur = mysql_fetch_assoc($cur)) {
  $count++;
  if($count == 2) {
echo '
tr
  td'. $row_cur['tid'] .'/td
  tdnbsp;/td
/tr';
  }
}
?
/table


--
Kind regards
Kim Emax - masterminds.dk

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



RE: [PHP] Display just 1 record in a query

2010-01-12 Thread Daevid Vincent
 -Original Message-
 From: Kim Madsen [mailto:php@emax.dk] 
 Sent: Tuesday, January 12, 2010 2:17 PM
 To: deal...@gmail.com
 Cc: php-general@lists.php.net
 Subject: Re: [PHP] Display just 1 record in a query
 
 deal...@gmail.com wrote on 12/01/2010 22:52:
  I did a query... then I display records like:
  
  table
?php do { ?
  tr
td?php echo $row_cur['tid']; ?/td
tdnbsp;/td
  /tr
  ?php } while ($row_cur = mysql_fetch_assoc($cur)); ?
  /table
  
  
  Q: but how I i just display a particular record with out 
 the do / while 
  loop?
 
 Just use extract($row_cur); before the table starts. That 
 would give you 
 first row only
 
 Another approach could be to add  LIMIT 1 to the end of 
 your SQL statement
 
  like just the 2nd record only:
  
  i tried
  ?php echo $row_cur['tid',2]; ?
  but this makes an error
  
  or $row_cur('tid',2) --- hmmm what's the syntax?
 
 Getting only second row, but not the first? That would be 
 using a count 
 var and show only data if count == 2
 
 
 table
 ?php
 $count=0;
 do {
 $count++;
 if($count == 2) {
echo '
  tr
td'. $row_cur['tid'] .'/td
tdnbsp;/td
  /tr';
}
 } while ($row_cur = mysql_fetch_assoc($cur));
 ?
 /table
 
 
 Another thing: drop the do and use this syntax instead, it's 
 more readable:
 
 table
 ?php
 $count=0;
 while ($row_cur = mysql_fetch_assoc($cur)) {
$count++;
if($count == 2) {
  echo '
  tr
td'. $row_cur['tid'] .'/td
tdnbsp;/td
  /tr';
}
 }
 ?
 /table

Holy, Jesus, Marry and Joseph! You can't be serious with that?!
So you're going to loop over potentially hundreds or thousands of records
and only display one?
Wow. Speechless.

http://www.php.net/manual/en/function.mysql-data-seek.php

$result = mysql_query($query);
mysql_data_seek($result, 1); //rows start at 0, so second row is 1
$row = mysql_fetch_assoc($result);
echo $row['tid'];

But the real way I suggest is using the LIMIT portion of your SELECT
http://dev.mysql.com/doc/refman/5.0/en/select.html
Then you don't need to data_seek() as you would have pulled the exact row
you wanted.
If you wanted the second row, you would LIMIT 1,1 

Also, if you just did a basic loop and stored all your results in a
multi-dimensional array, then you would just pull that element.

$mydata = array();

while ($row = mysql_fetch_assoc($result)) $mydata[] = $row;
Then you would just echo $mydata[1]['tid'];

Or what I like to do:

while ($row = mysql_fetch_assoc($result)) $mydata[$row['id']] = $row;
Assuming you were trying to pull a specific record $tid = ID:
echo $mydata[$tid];


ÐÆ5ÏÐ 
Light travels faster than sound. This is why some people appear bright
until you hear them speak.


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



Re: [PHP] Display just 1 record in a query

2010-01-12 Thread Kim Madsen

Daevid Vincent wrote on 13/01/2010 00:00:


Holy, Jesus, Marry and Joseph! You can't be serious with that?!
So you're going to loop over potentially hundreds or thousands of records
and only display one?
Wow. Speechless.


Either you're talking to dealtek or you didn't read my post very well:

Another approach could be to add  LIMIT 1 to the end of your SQL 
statement


I just pointed out different approaches and answered his questions. Of 
course I would use last or break on the count == 2 approach, not running 
through _any_ records after getting what I wanted. I don't really get 
the like just the 2nd record only, but again, from the material we've 
seen it's hard to give the right advice


--
Kind regards
Kim Emax - masterminds.dk

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



Re: [PHP] Display just 1 record in a query

2010-01-12 Thread deal...@gmail.com


On Jan 12, 2010, at 1:57 PM, Ashley Sheridan wrote:

Depends on how you're creating running the query. You could do  
something like:


echo mysql_result($result, 1, 'fieldname');

Where $result is your result object and 1 is a 0 indexed array, so  
would be the second result.


Thanks Ryan, Ashley  Kim for the good techniques...

- in my case I was trying to pull a random record from a query of  
Table1 - then do a 2nd query from 1st so


mysql_result worked fine for my needs like:


... do query 1... 'cur' - SELECT id FROM myTable1

$ran = rand(0, $totalRows_cur - 1); // pick random rec row within  
total count


$pick1 = mysql_result($cur, $ran); // get the ID from the choice -  
(like id=252)


... do query 2 // where relatedID = $pick1 of myTable2



cool - THANKS ALL!



Thanks,
deal...@gmail.com
[db-10]


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



Re: [PHP] Display just 1 record in a query

2010-01-12 Thread Robert Cummings


deal...@gmail.com wrote:

On Jan 12, 2010, at 1:57 PM, Ashley Sheridan wrote:

Depends on how you're creating running the query. You could do  
something like:


echo mysql_result($result, 1, 'fieldname');

Where $result is your result object and 1 is a 0 indexed array, so  
would be the second result.


Thanks Ryan, Ashley  Kim for the good techniques...

- in my case I was trying to pull a random record from a query of  
Table1 - then do a 2nd query from 1st so


mysql_result worked fine for my needs like:


... do query 1... 'cur' - SELECT id FROM myTable1

$ran = rand(0, $totalRows_cur - 1); // pick random rec row within  
total count


$pick1 = mysql_result($cur, $ran); // get the ID from the choice -  
(like id=252)


... do query 2 // where relatedID = $pick1 of myTable2


Put your random logic into the query:

SELECT
something
FROM
somewhere
WHERE
condition
ORDER BY
RAND()
LIMIT
1

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

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



Re: [PHP] Display just 1 record in a query

2010-01-12 Thread Jochem Maas
Op 1/13/10 12:43 AM, Robert Cummings schreef:
 
 deal...@gmail.com wrote:
 On Jan 12, 2010, at 1:57 PM, Ashley Sheridan wrote:

 Depends on how you're creating running the query. You could do 
 something like:

 echo mysql_result($result, 1, 'fieldname');

 Where $result is your result object and 1 is a 0 indexed array, so 
 would be the second result.

 Thanks Ryan, Ashley  Kim for the good techniques...

 - in my case I was trying to pull a random record from a query of 
 Table1 - then do a 2nd query from 1st so

 mysql_result worked fine for my needs like:


 ... do query 1... 'cur' - SELECT id FROM myTable1

 $ran = rand(0, $totalRows_cur - 1); // pick random rec row within 
 total count

 $pick1 = mysql_result($cur, $ran); // get the ID from the choice - 
 (like id=252)

 ... do query 2 // where relatedID = $pick1 of myTable2
 
 Put your random logic into the query:
 
 SELECT
 something
 FROM
 somewhere
 WHERE
 condition
 ORDER BY
 RAND()
 LIMIT
 1

please read the following to understand the consequences and
possible performance problems with this approach:

http://www.titov.net/2005/09/21/do-not-use-order-by-rand-or-how-to-get-random-rows-from-table/

personally I go with the 2 query approach

 ... or even to use a specific field to store a generated random value
(with suitable index) for each record, the stored values can be regenerated
in an out-of-band process (e.g. cron job) ... such a setup would suffice in
situations where frontend response speed is of primary concern and it's
acceptable to return the same 'random' record over a given period (i.e.
the time between random value regeneration)

 Cheers,
 Rob.


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



Re: [PHP] Display just 1 record in a query

2010-01-12 Thread Robert Cummings

Jochem Maas wrote:

Op 1/13/10 12:43 AM, Robert Cummings schreef:

deal...@gmail.com wrote:

On Jan 12, 2010, at 1:57 PM, Ashley Sheridan wrote:

Depends on how you're creating running the query. You could do 
something like:


echo mysql_result($result, 1, 'fieldname');

Where $result is your result object and 1 is a 0 indexed array, so 
would be the second result.

Thanks Ryan, Ashley  Kim for the good techniques...

- in my case I was trying to pull a random record from a query of 
Table1 - then do a 2nd query from 1st so


mysql_result worked fine for my needs like:


... do query 1... 'cur' - SELECT id FROM myTable1

$ran = rand(0, $totalRows_cur - 1); // pick random rec row within 
total count


$pick1 = mysql_result($cur, $ran); // get the ID from the choice - 
(like id=252)


... do query 2 // where relatedID = $pick1 of myTable2

Put your random logic into the query:

SELECT
something
FROM
somewhere
WHERE
condition
ORDER BY
RAND()
LIMIT
1


please read the following to understand the consequences and
possible performance problems with this approach:

http://www.titov.net/2005/09/21/do-not-use-order-by-rand-or-how-to-get-random-rows-from-table/


I was already aware of the issues with RAND(). However, at the very top 
it says:


  if your table have just 50-100 rows, use whatever you want.

Without more information about the person's particular use case I would 
use the RAND() version for its simplicity :)


Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

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