fetching one field

fetching one field

am 21.08.2006 10:53:05 von mark

let's say i have a table in my database, with only one column. all i
want to do is retrieve a list of the entries.. i could do it like this

$result = mysql_query("SELECT * FROM t1");
while( $x = mysql_fetch_row($result) ) {
echo $x[0];
}

but it seems kinda silly to fetch an entire array, when only the first
index is used. is there a better/more efficient way?

Re: fetching one field

am 21.08.2006 18:13:15 von Davie

Mark wrote:
> let's say i have a table in my database, with only one column. all i
> want to do is retrieve a list of the entries.. i could do it like this
>
> $result = mysql_query("SELECT * FROM t1");
> while( $x = mysql_fetch_row($result) ) {
> echo $x[0];
> }
>
> but it seems kinda silly to fetch an entire array, when only the first
> index is used. is there a better/more efficient way?
mysql_fetch_row() fetches a row !
as your table has only 1 column the row consists of only 1 value.
What you require is
$result = mysql_query("SELECT * FROM t1");
while ($row = mysql_fetch_array($result)) {
echo $x[0];
}

Re: fetching one field

am 21.08.2006 18:16:36 von Davie

Suffering from cut&pasteitis
$result = mysql_query("SELECT * FROM t1");
while ($x = mysql_fetch_array($result)) {
echo $x[0];
}
davie wrote:
> Mark wrote:
> > let's say i have a table in my database, with only one column. all i
> > want to do is retrieve a list of the entries.. i could do it like this
> >
> > $result = mysql_query("SELECT * FROM t1");
> > while( $x = mysql_fetch_row($result) ) {
> > echo $x[0];
> > }
> >
> > but it seems kinda silly to fetch an entire array, when only the first
> > index is used. is there a better/more efficient way?
> mysql_fetch_row() fetches a row !
> as your table has only 1 column the row consists of only 1 value.
> What you require is
> $result = mysql_query("SELECT * FROM t1");
> while ($row = mysql_fetch_array($result)) {
> echo $x[0];
> }

Re: fetching one field

am 22.08.2006 05:43:12 von mark

davie wrote:
> Mark wrote:
> > let's say i have a table in my database, with only one column. all i
> > want to do is retrieve a list of the entries.. i could do it like this
> >
> > $result = mysql_query("SELECT * FROM t1");
> > while( $x = mysql_fetch_row($result) ) {
> > echo $x[0];
> > }
> >
> > but it seems kinda silly to fetch an entire array, when only the first
> > index is used. is there a better/more efficient way?
> mysql_fetch_row() fetches a row !
> as your table has only 1 column the row consists of only 1 value.
> What you require is
> $result = mysql_query("SELECT * FROM t1");
> while ($row = mysql_fetch_array($result)) {
> echo $x[0];
> }

uhmm... a row is an array... what exactly is the difference between
your solution and mine?? (aside from also returning an associative
array by default, which i do not need)

perhaps you misinterpreted what i was asking. i'm asking for a solution
that does NOT return an array, but is equally or more efficient.

perhaps this is the best way to do it i guess.

Re: fetching one field

am 22.08.2006 10:55:33 von Davie

You wanted a list of all entries that is an array
As you have only 1 field (column) per record it is a 1 dimentional
array.
If it had more than 1 field it would be a 2 dimentional array.
mysql_fetch_row() fetches an array of row.
I suggest you read http://www.php.net/manual/en/ref.mysql.php
Mark wrote:
> davie wrote:
> > Mark wrote:
> > > let's say i have a table in my database, with only one column. all i
> > > want to do is retrieve a list of the entries.. i could do it like this
> > >
> > > $result = mysql_query("SELECT * FROM t1");
> > > while( $x = mysql_fetch_row($result) ) {
> > > echo $x[0];
> > > }
> > >
> > > but it seems kinda silly to fetch an entire array, when only the first
> > > index is used. is there a better/more efficient way?
> > mysql_fetch_row() fetches a row !
> > as your table has only 1 column the row consists of only 1 value.
> > What you require is
> > $result = mysql_query("SELECT * FROM t1");
> > while ($row = mysql_fetch_array($result)) {
> > echo $x[0];
> > }
>
> uhmm... a row is an array... what exactly is the difference between
> your solution and mine?? (aside from also returning an associative
> array by default, which i do not need)
>
> perhaps you misinterpreted what i was asking. i'm asking for a solution
> that does NOT return an array, but is equally or more efficient.
>
> perhaps this is the best way to do it i guess.

Re: fetching one field

am 20.09.2006 04:29:31 von mark

davie wrote:
> You wanted a list of all entries that is an array
> As you have only 1 field (column) per record it is a 1 dimentional
> array.
> If it had more than 1 field it would be a 2 dimentional array.
> mysql_fetch_row() fetches an array of row.
> I suggest you read http://www.php.net/manual/en/ref.mysql.php
> Mark wrote:
> > davie wrote:
> > > Mark wrote:
> > > > let's say i have a table in my database, with only one column. all i
> > > > want to do is retrieve a list of the entries.. i could do it like this
> > > >
> > > > $result = mysql_query("SELECT * FROM t1");
> > > > while( $x = mysql_fetch_row($result) ) {
> > > > echo $x[0];
> > > > }
> > > >
> > > > but it seems kinda silly to fetch an entire array, when only the first
> > > > index is used. is there a better/more efficient way?
> > > mysql_fetch_row() fetches a row !
> > > as your table has only 1 column the row consists of only 1 value.
> > > What you require is
> > > $result = mysql_query("SELECT * FROM t1");
> > > while ($row = mysql_fetch_array($result)) {
> > > echo $x[0];
> > > }
> >
> > uhmm... a row is an array... what exactly is the difference between
> > your solution and mine?? (aside from also returning an associative
> > array by default, which i do not need)
> >
> > perhaps you misinterpreted what i was asking. i'm asking for a solution
> > that does NOT return an array, but is equally or more efficient.
> >
> > perhaps this is the best way to do it i guess.

in such case it should be used as
$result = mysql_query("SELECT * FROM t1");
$arr = mysql_fetch_array($result)) ;
foreach($arr as $val) {
// do something with $val
}
if it does indeed retrieve the entire column, no?
I'll look into this more. thanks anyway.

Re: fetching one field

am 20.09.2006 09:02:54 von dan

If you have multiple column in your table then use

$result = mysql_query("SELECT column1 FROM t1");
while( $x = mysql_fetch_row($result) ) {
echo $x[0];
}

would limit the return data to column1 hence save bandwidth and memory.

Dan.

Mark wrote:
> davie wrote:
> > You wanted a list of all entries that is an array
> > As you have only 1 field (column) per record it is a 1 dimentional
> > array.
> > If it had more than 1 field it would be a 2 dimentional array.
> > mysql_fetch_row() fetches an array of row.
> > I suggest you read http://www.php.net/manual/en/ref.mysql.php
> > Mark wrote:
> > > davie wrote:
> > > > Mark wrote:
> > > > > let's say i have a table in my database, with only one column. all i
> > > > > want to do is retrieve a list of the entries.. i could do it like this
> > > > >
> > > > > $result = mysql_query("SELECT * FROM t1");
> > > > > while( $x = mysql_fetch_row($result) ) {
> > > > > echo $x[0];
> > > > > }
> > > > >
> > > > > but it seems kinda silly to fetch an entire array, when only the first
> > > > > index is used. is there a better/more efficient way?
> > > > mysql_fetch_row() fetches a row !
> > > > as your table has only 1 column the row consists of only 1 value.
> > > > What you require is
> > > > $result = mysql_query("SELECT * FROM t1");
> > > > while ($row = mysql_fetch_array($result)) {
> > > > echo $x[0];
> > > > }
> > >
> > > uhmm... a row is an array... what exactly is the difference between
> > > your solution and mine?? (aside from also returning an associative
> > > array by default, which i do not need)
> > >
> > > perhaps you misinterpreted what i was asking. i'm asking for a solution
> > > that does NOT return an array, but is equally or more efficient.
> > >
> > > perhaps this is the best way to do it i guess.
>
> in such case it should be used as
> $result = mysql_query("SELECT * FROM t1");
> $arr = mysql_fetch_array($result)) ;
> foreach($arr as $val) {
> // do something with $val
> }
> if it does indeed retrieve the entire column, no?
> I'll look into this more. thanks anyway.

Re: fetching one field

am 21.09.2006 04:08:18 von mark

dan wrote:
> If you have multiple column in your table then use
>
> $result = mysql_query("SELECT column1 FROM t1");
> while( $x = mysql_fetch_row($result) ) {
> echo $x[0];
> }
>
> would limit the return data to column1 hence save bandwidth and memory.
>
> Dan.
>
> Mark wrote:
> > davie wrote:
> > > You wanted a list of all entries that is an array
> > > As you have only 1 field (column) per record it is a 1 dimentional
> > > array.
> > > If it had more than 1 field it would be a 2 dimentional array.
> > > mysql_fetch_row() fetches an array of row.
> > > I suggest you read http://www.php.net/manual/en/ref.mysql.php
> > > Mark wrote:
> > > > davie wrote:
> > > > > Mark wrote:
> > > > > > let's say i have a table in my database, with only one column. all i
> > > > > > want to do is retrieve a list of the entries.. i could do it like this
> > > > > >
> > > > > > $result = mysql_query("SELECT * FROM t1");
> > > > > > while( $x = mysql_fetch_row($result) ) {
> > > > > > echo $x[0];
> > > > > > }
> > > > > >
> > > > > > but it seems kinda silly to fetch an entire array, when only the first
> > > > > > index is used. is there a better/more efficient way?
> > > > > mysql_fetch_row() fetches a row !
> > > > > as your table has only 1 column the row consists of only 1 value.
> > > > > What you require is
> > > > > $result = mysql_query("SELECT * FROM t1");
> > > > > while ($row = mysql_fetch_array($result)) {
> > > > > echo $x[0];
> > > > > }
> > > >
> > > > uhmm... a row is an array... what exactly is the difference between
> > > > your solution and mine?? (aside from also returning an associative
> > > > array by default, which i do not need)
> > > >
> > > > perhaps you misinterpreted what i was asking. i'm asking for a solution
> > > > that does NOT return an array, but is equally or more efficient.
> > > >
> > > > perhaps this is the best way to do it i guess.
> >
> > in such case it should be used as
> > $result = mysql_query("SELECT * FROM t1");
> > $arr = mysql_fetch_array($result)) ;
> > foreach($arr as $val) {
> > // do something with $val
> > }
> > if it does indeed retrieve the entire column, no?
> > I'll look into this more. thanks anyway.

so dont fetch more than you need. got it.

but still..

array mysql_fetch_row ( resource result )

Returns a numerical array that corresponds to the fetched row and moves
the internal data pointer ahead

vs

array mysql_fetch_array ( resource result [, int result_type] )

Returns an array that corresponds to the fetched row and moves the
internal data pointer ahead.


they both return arrays.. but in your case

result_type

The type of array that is to be fetched. It's a constant and can
take the following values: MYSQL_ASSOC, MYSQL_NUM, and the default
value of MYSQL_BOTH.

which defaults to MYSQL_BOTH thus also returns an associative array
which isn't needed...

so IMHO my original solution was ever so slightly more efficient..?
not that i'm trying to stir up an argument, i just don't understand why
you insist that mysql_fetch_array is better than mysql_fetch_row for
this scenario.