Loops

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fire Juggler

    Loops

    Hi I have this this code
    {while ($row = mysql_fetch_arr ay($numresultsg al)) {
    $table=$row{'ta ble_gal'};
    $numresultphoto s=mysql_query(" SELECT * FROM" .$table. "order by date desc");
    $numphotos=mysq l_num_rows($num resultphotos);

    echo"<b>Gallery :</b> " .$row{'gallery' }. "<br>";
    echo "<b>No. Of Photos:</b> " .$numphotos. "<br>" ;}

    But it keeps failing to produce the number of photos in the gallery. But it
    will display the table name if i want it to. So it's got the correct name,
    so any ideas why it's failing and not telling me how many rows are in the
    database.

    Thanks

    --
    Kathryn (Fire Juggler)




  • Ewoud Dronkert

    #2
    Re: Loops

    On Sat, 9 Jul 2005 16:42:17 +0100, Fire Juggler wrote:[color=blue]
    > {while ($row = mysql_fetch_arr ay($numresultsg al)) {[/color]

    What's that first { doing there?
    [color=blue]
    > $table=$row{'ta ble_gal'};[/color]

    Replace { and } with [ and ].
    [color=blue]
    > $numresultphoto s=mysql_query(" SELECT * FROM" .$table. "order by date desc");[/color]

    The table name gets plastered against 'FROM' and 'order', the query will
    not be valid. Also, if you only need the number of rows, don't get the
    complete result set, but use an aggregate function (COUNT) to let de
    database do the counting for you.
    [color=blue]
    > echo"<b>Gallery :</b> " .$row{'gallery' }. "<br>";[/color]

    Replace { and } with [ and ].

    --
    Firefox Browser - Rediscover the web - http://getffox.com/
    Thunderbird E-mail and Newsgroups - http://gettbird.com/

    Comment

    • Tony

      #3
      Re: Loops

      Fire Juggler wrote:[color=blue]
      > Hi I have this this code
      > {while ($row = mysql_fetch_arr ay($numresultsg al)) {
      > $table=$row{'ta ble_gal'};
      > $numresultphoto s=mysql_query(" SELECT * FROM" .$table. "order by date
      > desc"); $numphotos=mysq l_num_rows($num resultphotos);
      >
      > echo"<b>Gallery :</b> " .$row{'gallery' }. "<br>";
      > echo "<b>No. Of Photos:</b> " .$numphotos. "<br>" ;}
      >
      > But it keeps failing to produce the number of photos in the gallery.
      > But it will display the table name if i want it to. So it's got the
      > correct name, so any ideas why it's failing and not telling me how
      > many rows are in the database.[/color]

      Is that the EXACT code? If so, you have an error in your SQL syntax. That
      error is resulting in a false value for $numresultphoto s, which, in turn,
      gives a false value for $numphotos.

      The error that I see is in the lack of spaces: "SELECT * FROM" . $table .
      "order by date desc" - if $table = 'MyTable', your query would be "SELECT *
      FROMMyTableorde r by date desc" - add spaces: ".. FROM " . $table . " order
      by..."

      If you also add some error reporting, you'll probably find errors faster -
      and, BTW, you don't have to concatenate variables when using double-quotes:

      $numresultphoto s = mysql_query("SE LECT * FROM $table order by date desc");
      if (!$numresultpho tos) { die (mysql_error()) ; }
      $numphotos = mysql_num_rows( $numresultphoto s);


      --
      Tony Garcia
      Web Right! Development
      Riverside, CA



      Comment

      Working...