Display just 1 record in a query

Display just 1 record in a query

am 12.01.2010 22:52:24 von dealtek

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








 



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

like just the 2nd record only:

i tried

but this makes an error....

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



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


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

Re: Display just 1 record in a query

am 12.01.2010 22:57:41 von Ashley Sheridan

--=-cwtvQ7VeIWMqXAReRYu5
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Tue, 2010-01-12 at 13:52 -0800, dealtek@gmail.com wrote:

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


>
>
>
>
>
>
>
 

>
>
> Q: but how I i just display a particular record with out the do /
> while loop?
>
> like just the 2nd record only:
>
> i tried
>
> but this makes an error....
>
> or $row_cur('tid',2) --- hmmm what's the syntax?
>
>
>
> Thanks,
> dealtek@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



--=-cwtvQ7VeIWMqXAReRYu5--

Re: Display just 1 record in a query

am 12.01.2010 22:59:33 von Ryan Sun

--0016e6460848e8ec31047cfec4b0
Content-Type: text/plain; charset=ISO-8859-1

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, dealtek@gmail.com wrote:

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


>
>
>
>
>
>
>
 

>
>
> Q: but how I i just display a particular record with out the do / while
> loop?
>
> like just the 2nd record only:
>
> i tried
>
> but this makes an error....
>
> or $row_cur('tid',2) --- hmmm what's the syntax?
>
>
>
> Thanks,
> dealtek@gmail.com
> [db-10]
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--0016e6460848e8ec31047cfec4b0--

Re: Display just 1 record in a query

am 12.01.2010 23:17:22 von Kim Madsen

dealtek@gmail.com wrote on 12/01/2010 22:52:
> I did a query... then I display records like:
>
>


>
>
>
>
>
>
>
 

>
>
> 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
>
> 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



$count=0;
do {
$count++;
if($count == 2) {
echo '



';
}
} while ($row_cur = mysql_fetch_assoc($cur));
?>
'. $row_cur['tid'] .'  



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


$count=0;
while ($row_cur = mysql_fetch_assoc($cur)) {
$count++;
if($count == 2) {
echo '



';
}
}
?>
'. $row_cur['tid'] .'  



--
Kind regards
Kim Emax - masterminds.dk

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

RE: Display just 1 record in a query

am 13.01.2010 00:00:49 von Daevid Vincent

> -----Original Message-----
> From: Kim Madsen [mailto:php.net@emax.dk]=20
> Sent: Tuesday, January 12, 2010 2:17 PM
> To: dealtek@gmail.com
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] Display just 1 record in a query
>=20
> dealtek@gmail.com wrote on 12/01/2010 22:52:
> > I did a query... then I display records like:
> >=20
> >


> >
> >
> >
> >
> >
> >
> >
 

> >=20
> >=20
> > Q: but how I i just display a particular record with out=20
> the do / while=20
> > loop?
>=20
> Just use extract($row_cur); before the table starts. That=20
> would give you=20
> first row only
>=20
> Another approach could be to add " LIMIT 1" to the end of=20
> your SQL statement
>=20
> > like just the 2nd record only:
> >=20
> > i tried
> >
> > but this makes an error....
> >=20
> > or $row_cur('tid',2) --- hmmm what's the syntax?
>=20
> Getting only second row, but not the first? That would be=20
> using a count=20
> var and show only data if count == 2
>=20
>=20
>
> > $count=3D0;
> do {
> $count++;
> if($count == 2) {
> echo '
>
>
>
> ';
> }
> } while ($row_cur =3D mysql_fetch_assoc($cur));
> ?>
>
'. $row_cur['tid'] .' 

>=20
>=20
> Another thing: drop the do and use this syntax instead, it's=20
> more readable:
>=20
>
> > $count=3D0;
> while ($row_cur =3D mysql_fetch_assoc($cur)) {
> $count++;
> if($count == 2) {
> echo '
>
>
>
> ';
> }
> }
> ?>
>
'. $row_cur['tid'] .' 


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 =3D mysql_query($query);
mysql_data_seek($result, 1); //rows start at 0, so second row is 1
$row =3D 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"=20

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 =3D array();

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

Or what I like to do:

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


ÐÆ5ÏÐ=20
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: Display just 1 record in a query

am 13.01.2010 00:16:53 von 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: Display just 1 record in a query

am 13.01.2010 00:29:47 von dealtek

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,
dealtek@gmail.com
[db-10]


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

Re: Display just 1 record in a query

am 13.01.2010 00:43:34 von Robert Cummings

dealtek@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: Display just 1 record in a query

am 13.01.2010 02:32:45 von Jochem Maas

Op 1/13/10 12:43 AM, Robert Cummings schreef:
>
> dealtek@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: Display just 1 record in a query

am 13.01.2010 04:19:42 von Robert Cummings

Jochem Maas wrote:
> Op 1/13/10 12:43 AM, Robert Cummings schreef:
>> dealtek@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