having problem displaying results

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wrw[three]

    having problem displaying results

    Hey here is my code - The number of rows are being displayed that are
    currently in the database (3) but when i try to get the data out it
    isn't being displayed, showing merely the just the rows with the
    lines.... can you help me? thanks!

    <?
    require_once ('mysqyl_connec t.php');

    $query="SELECT * FROM CLIENT_INFO";
    $result=mysql_q uery($query);

    $num=mysql_numr ows($result);

    mysql_close();

    echo "<b><center>Cli ent List</center></b><br><br>";

    $i=0;
    while ($i < $num) {

    $first=mysql_re sult($result,$i ,"first_name ");
    $last=mysql_res ult($result,$i, "last_name" );
    $mobile=mysql_r esult($result,$ i,"email");

    echo "<b>$last_n ame</b><br>E-mail: $email<br><hr>< br>";

    $i++;
    }

    ?>

  • Ken Robinson

    #2
    Re: having problem displaying results


    wrw[three] wrote:[color=blue]
    > Hey here is my code - The number of rows are being displayed that are
    > currently in the database (3) but when i try to get the data out it
    > isn't being displayed, showing merely the just the rows with the
    > lines.... can you help me? thanks!
    >
    > <?
    > require_once ('mysqyl_connec t.php');
    >
    > $query="SELECT * FROM CLIENT_INFO";
    > $result=mysql_q uery($query);
    >
    > $num=mysql_numr ows($result);
    >
    > mysql_close();[/color]

    You close the database connection here, after that how do you expect to
    retrieve any information?[color=blue]
    >
    > echo "<b><center>Cli ent List</center></b><br><br>";
    >
    > $i=0;
    > while ($i < $num) {
    >
    > $first=mysql_re sult($result,$i ,"first_name ");
    > $last=mysql_res ult($result,$i, "last_name" );
    > $mobile=mysql_r esult($result,$ i,"email");
    >
    > echo "<b>$last_n ame</b><br>E-mail: $email<br><hr>< br>";
    >
    > $i++;
    > }
    >
    > ?>[/color]

    The better way of doing this would be:
    <?
    $q="SELECT * FROM CLIENT_INFO";
    $rs=mysql_query ($q);
    $num=mysql_numr ows($rs);
    if ($num > 0) {
    echo "<b><center>Cli ent List</center></b><br><br>\n";
    while ($rw = mysql_fetch_ass oc($rs))
    echo '<b>' . $row['last_name'] . '</b><br>E-mail: ' .
    $row['email'] . "<br><hr><br>\n ";
    }
    ?>

    Basically, you're $result (my $rs) contains a pointer to the
    information, not the information. You still have to retrieve it from
    the database, which is what the while loop does.

    Ken

    Comment

    • wrw[three]

      #3
      Re: having problem displaying results

      hmmm.... still isn't working.... I'm getting the three rows, but no
      data is being put into them? any other suggestions??!? thanks for your
      help!

      Comment

      • Zilla

        #4
        Re: having problem displaying results

        wrw[three] wrote:[color=blue]
        > hmmm.... still isn't working.... I'm getting the three rows, but no
        > data is being put into them? any other suggestions??!? thanks for your
        > help!
        >[/color]

        In these lines you save the data in $first, $last and $mobile:

        $first=mysql_re sult($result,$i ,"first_name ");
        $last=mysql_res ult($result,$i, "last_name" );
        $mobile=mysql_r esult($result,$ i,"email");

        and in this line you use variables equal to the field names:

        echo "<b>$last_n ame</b><br>E-mail: $email<br><hr>< br>";

        Try using the variables you save the data in ;-)

        echo "<b>$last</b><br>E-mail: $mobile<br><hr> <br>";

        But i agree with Ken that his solution is better and the php manual also
        advises you to use this because it is much quicker. Look here:



        Zilla

        Comment

        • Zilla

          #5
          Re: having problem displaying results

          Forgot the link to mysql_fetch_arr ay(). Here it is:

          Fetch a result row as an associative array, a numeric array, or both


          Zilla

          Comment

          Working...