Re: [Phplib-users] HELP! php-mysql weirdness
Brought to you by:
nhruby,
richardarcher
|
From: Stephen W. <wo...@me...> - 2001-09-20 17:28:54
|
Thanks. Yes, that is what I did do, but all the books I have say to use:
while($row = mysql_fetch_array($result)) {
...
}
to fetch rows but this construct does not work reliably if (my guess is
if $row[0]=NULL) it can arbitrary fail based on the data set being
fetched. It is a hidden and silent failure based on data. this construct
is a poor one to tesch people to use as compared to something more ugly
but reliable like:
$num = mysql_num_rows($result);
for ($i=0; $i<$num; $i++) {
$row = mysql_fetch_array($result);
...
}
Sorry, I'm just frustrated after spending 3.5 hours trying to track this
down when I was just trying a trivial experiment in my existing app. I
think I need to review all my existing code to see if this might be
happening elsewhere <sigh>
-Steve
"nathan r. hruby" wrote:
>
> On Thu, 20 Sep 2001, Stephen Woodbridge wrote:
>
> > Bizzare, but true!
> >
> > Here is what I think the problem is.
> >
> > The column BIRT_PLAC may be NULL so the result set has a row with the
> > value of NULL unless you use the WHERE clause. The problem is the that
> > the
> >
> > while($row = mysql_fetch_array($result))
> >
> > terminates the the while when it returns the NULL row result and the
> > ORDER BY always place the NULL row result as the first row in the result
> > set. Even more insidious would be an unsorted result that returns some
> > row of data, then a NULL, then more rows of data. So you would get some
> > rows, thnk things are great and not realize that you never processed the
> > rest of the rows. Which is exactly what happens in my testing of this
> > problem.
> >
> > This is VERY BAD! I use this type of construct ALL through my code.
> >
> > Any thoughts or comments?
> >
>
> add NOT NULL to your WHERE clause
>
> > -Stephen Woodbridge
> >
> > Stephen Woodbridge wrote:
> > >
> > > Hi all,
> > >
> > > Sorry this is a little off topic, but I'm can't figure this one out and
> > > someone here might have run into this problem.
> > >
> > > I have a SQL query to mysql from php3, the query has an optional WHERE
> > > clause. I get results back if the where clause is included but none if
> > > it is left out.
> > >
> > > select distinct BIRT_PLAC as PLACE from INDI where BIRT_PLAC like "%%"
> > > order by PLACE
> > > num=719
> > >
> > > select distinct BIRT_PLAC as PLACE from INDI order by PLACE
> > > num=0
> > >
> > > BOTH queries work FINE if I cut and paste them into mysql directly.
> > > Anyone have any ideas?
> > > -Steve
> > >
> > > function Places($str) {
> > > global $conn;
> > >
> > > $aPlaces = array();
> > >
> > > $sql = "select distinct BIRT_PLAC as PLACE from INDI ";
> > > if ($str != "")
> > > $sql .= "where BIRT_PLAC like \"%$str%\" ";
> > > $sql .= "order by PLACE ";
> > >
> > > print "<br>$sql<br>\n";
> > > $result = @mysql_query($sql, $conn)
> > > or die("Can't execute query: $sql ! $php_errormsg");
> > >
> > > $num = 0;
> > > while ($row = mysql_fetch_array($result)) {
> > > $aPlaces[] = $row["PLACE"];
> > > $num++;
> > > }
> > > print "num=$num<br>\n";
> > > }
> > >
> >
> > _______________________________________________
> > Phplib-users mailing list
> > Php...@li...
> > https://lists.sourceforge.net/lists/listinfo/phplib-users
> >
>
> --
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
> nathan hruby / digital statement
> na...@ds...
> http://www.dstatement.com/
>
> Public GPG key can be found at:
> http://www.dstatement.com/nathan-gpg-key.txt
> ED54 9A5E 132D BD01 9103 EEF3 E1B9 4738 EC90 801B
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|