Connecting Tech Pros Worldwide Forums | Help | Site Map

An extra last empty field in an 'mysql_fetch_array' result array?

Marcel Brekelmans
Guest
 
Posts: n/a
#1: Oct 13 '05
Hello,

I seem to get an extra empty field in every 'mysql_fetch_array' command I issue. For example:

I have a simple table 'tblName':

ID Name
1 Jane
2 Joe
2 Doe

The following code:

$oCursor = mysql_query("SELECT ID from tblName WHERE Name='Jane'");
if (!$oCursor)
{
$bGo = false;
}
else
{
$aRow = mysql_fetch_array($oCursor);
}

results in:

count($aRow) = 2;

$aRow[0] = 1;
$aRow[1] = '';

Am I missing something, doing something wrong, a wrong PHP setting?

Thanks,
Marcel Brekelmans


windandwaves
Guest
 
Posts: n/a
#2: Oct 13 '05

re: An extra last empty field in an 'mysql_fetch_array' result array?



"Marcel Brekelmans" <marcel@marcel-art.com> wrote in message
news:29udnWsDMpJDW9PenZ2dnUVZ8qKdnZ2d@giganews.com ...
.....[color=blue]
>$oCursor = mysql_query("SELECT ID from tblName WHERE Name='Jane'");
>if (!$oCursor)
>{
> $bGo = false;
>}
>else
>{
> $aRow = mysql_fetch_array($oCursor);
>}[/color]
[color=blue]
>results in:[/color]
[color=blue]
>count($aRow) = 2;[/color]
[color=blue]
>$aRow[0] = 1;
>$aRow[1] = '';[/color]
[color=blue]
>Am I missing something, doing something wrong, a wrong PHP setting?[/color]

Hoi Marcel

This is the function I use:

function getdata($sql) {
$result = @mysql_query($sql) or die("Error: " . mysql_error()." sql was
".$sql);
$ret = array();
while($row = mysql_fetch_assoc($result)) {
$ret[] = $row;
}
mysql_free_result($result);
return $ret;
}

then, in my code, I used:

$sql = "SELECT ID from tblName WHERE Name='Jane'";
$data = getdata($sql)
foreach ($data as $ds) {
echo $ds["Name"];
}

Once you use it a few times, it just makes life really easy, especially
because you refer to fields by name rather than position (e.g.$ds[0]).

Having said that, I am not sure what you are doing wrong ;-) I think that
you only fetch one row. A better way to do this is

while($row = mysql_fetch_row($query)) {

}

Note the single "is teken".

- Nicolaas


Peter van Schie
Guest
 
Posts: n/a
#3: Oct 13 '05

re: An extra last empty field in an 'mysql_fetch_array' result array?


Marcel Brekelmans wrote:[color=blue]
> The following code:
>
> $oCursor = mysql_query("SELECT ID from tblName WHERE Name='Jane'");
> if (!$oCursor)
> {
> $bGo = false;
> }
> else
> {
> $aRow = mysql_fetch_array($oCursor);
> }
>
> results in:
>
> count($aRow) = 2;
>
> $aRow[0] = 1;
> $aRow[1] = '';[/color]

Marcel,

mysql_fetch_array by default fetches the result both as an associative
array and a numeric array. It has two parameters, the second parameter
is either MYSQL_ASSOC, MYSQL_NUM or MYSQL_BOTH, default is MYSQL_BOTH.
Doing a print_r($aRow) will show this too.

HTH.
Peter.
--
http://www.phpforums.nl
brekelm
Guest
 
Posts: n/a
#4: Oct 14 '05

re: An extra last empty field in an 'mysql_fetch_array' result array?



Peter van Schie schreef:
[color=blue]
> Marcel Brekelmans wrote:[color=green]
> > The following code:
> >
> > $oCursor = mysql_query("SELECT ID from tblName WHERE Name='Jane'");
> > if (!$oCursor)
> > {
> > $bGo = false;
> > }
> > else
> > {
> > $aRow = mysql_fetch_array($oCursor);
> > }
> >
> > results in:
> >
> > count($aRow) = 2;
> >
> > $aRow[0] = 1;
> > $aRow[1] = '';[/color]
>
> Marcel,
>
> mysql_fetch_array by default fetches the result both as an associative
> array and a numeric array. It has two parameters, the second parameter
> is either MYSQL_ASSOC, MYSQL_NUM or MYSQL_BOTH, default is MYSQL_BOTH.
> Doing a print_r($aRow) will show this too.
>
> HTH.
> Peter.
> --
> http://www.phpforums.nl[/color]

Thanks Peter, that was the solution: using the MYSQL_NUM parameter
restricted the output to the single value I expected.

Justin Koivisto
Guest
 
Posts: n/a
#5: Oct 14 '05

re: An extra last empty field in an 'mysql_fetch_array' result array?


Please... post in plain text & do it only once (there are 6 identical
messages)

Marcel Brekelmans wrote:
[color=blue]
> I seem to get an extra empty field in every 'mysql_fetch_array' command
> I issue. For example:
>
> I have a simple table 'tblName':
>
> ID Name
> 1 Jane
> 2 Joe
> 2 Doe
>
> The following code:
>
> $oCursor = mysql_query("SELECT ID from tblName WHERE Name='Jane'");
> if (!$oCursor)
> {
> $bGo = false;
> }
> else
> {
> $aRow = mysql_fetch_array($oCursor);
> }
>
> results in:
>
> count($aRow) = 2;
>
> $aRow[0] = 1;
> $aRow[1] = '';
>
> Am I missing something, doing something wrong, a wrong PHP setting?[/color]

What is actually set is the following:

Array
(
[0] => 1
[ID] => 1
)

That is because mysql_fetch_array by default returns by column name and
by number...

Use mysql_fetch_array($oCursor,MYSQL_NUM) or
mysql_fetch_array($oCursor,MYSQL_ASSOC) instead.

http://us2.php.net/manual/en/functio...etch-array.php

(see "Return Values" section)

--
Justin Koivisto, ZCE - justin@koivi.com
http://koivi.com
Closed Thread