mysql_fetch_assoc query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Paul75
    New Member
    • Aug 2012
    • 1

    mysql_fetch_assoc query

    Hi there,

    Can anyone help?

    Why is the following code displaying "P1P2P3..." etc, instead of "P1 , P2, P3 ,.." etc. ?

    Thanks

    Paul

    Code:
    <!--Conect to database-->
    <?php include "connects/patientconn.php"; ?>
    
    <?php
    
    $query = "SELECT `Patient I.D.` FROM `patient`";
    
    $result = mysql_query($query);
    
    while($patient = mysql_fetch_assoc($result))
    {
    $pt = implode(" , ", $patient);
    echo $pt;
    }
    ?>
    Last edited by Rabbit; Aug 13 '12, 03:43 AM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code.

    The reason is that mysql_fetch_ass oc only returns one row. Not all of them.

    Comment

    • Sushant29
      New Member
      • Aug 2012
      • 5

      #3
      you have to use and looping method in it........

      Since mysql_fetch_ass oc is an array function of php...
      it will return u a no of rows in ur tables.

      using 'for loop' will be a better option for u.

      Comment

      • hemaldazzle
        New Member
        • Jul 2012
        • 15

        #4
        use it:
        foreach($pt as $val)
        {
        echo $val
        }

        Comment

        • johny10151981
          Top Contributor
          • Jan 2010
          • 1059

          #5
          if you dont know what would be the index of arrays then I would suggest you to use mysql_fetch_row .
          here is an example of what could happen.
          Code:
          $Query="SELECT SUM(Price+Tax), CustomerName FROM tblsometable Group By CustomerName"
          in this case it is bit confusing for SUM(Price+Tax)

          but on array you would get like this:
          Code:
          $array['SUM(Price+Tax)'];
          you can also fix this issue with naming the column like below:
          Code:
          $Query="SELECT SUM(Price+Tax) as Price, CustomerName FROM tblsometable Group By CustomerName"
          and all in all you can use integer base indexing by calling the function mysql_fetch_row

          Comment

          Working...