SELECT data from MySQL, and SHOW like this

Discussion in 'PHP' started by onlyican.com, Dec 22, 2005.

  1. #1
    Hey

    I know how to select data from the db using PHP, but when i do, it comes out like this

    Field name[1] fieldvalue[1]
    Field name[1] fieldvalue[1]

    What i want is

    Fieldname[1] Fieldname[1]
    Fieldvalue[1] Fieldvalue[1]
    Fieldvalue[2] Fieldvalue[2]
    ect.

    Any ideas....
     
    onlyican.com, Dec 22, 2005 IP
  2. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I got a complicated way

    
    mysql_connect ("localhost", "root");
    mysql_select_db(pet_shop);
    $query = "SELECT product, total_price FROM cust WHERE cust_id = '".$cust_num."'";
    $result = mysql_query($query);
    
    
    
    echo "<table border='1'>
    <thead>
    <tr>";
    for($i = 0;$i < mysql_num_fields($result);$i++)
    {
    echo "<th>".mysql_field_name($result,$i).
    "</th>";
    }
    echo " </tr>
    </thead>
    <tbody>";
    for ($i = 0; $i < mysql_num_rows($result); $i++)
    {
    echo "<tr>";
    $row = mysql_fetch_row($result);
    for($j = 0;$j<mysql_num_fields($result);$j++)
    {
    echo("<td>" . $row[$j] . "</td>");
    }
    echo "</tr>";
    }
    echo "</tbody></table>";
    
    
    PHP:
    Better ideas are welcome
     
    onlyican.com, Dec 22, 2005 IP
  3. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #3
    Just do 1 loop and build your header into one variable and your rows into another then echo them both at the end.
     
    dct, Dec 22, 2005 IP
  4. BKSH

    BKSH Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you can use while loop instead of for loop reducing the number of calls made to functions
    eg.
    while($row=mysql_fetch_row($result)){
    .
    //your code here
    .
    }
     
    BKSH, Dec 28, 2005 IP
  5. jimrthy

    jimrthy Guest

    Messages:
    283
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This may be the only acceptable method of using <table> I've ever run across.

    I'd have to see your code to tell you how to fix the problem. It's not a big deal. But, without your code, it's a guessing game.

    This is a problem with PHP. This sort of thing should be built in with the language. Better yet, an IDE (which handles ideas the designers never imagined) could handle it. All in a way which works with a CPU 10 years old...<sigh>.

    But back to your problem. What is your top level problem? What are you trying to solve? Let's talk about that, then we can talk about actual code. (Which you'll probably have to cough up as well...if you're reluctant to cough up code, think about it this way...if we couldn't [together] write better code, then why are you asking advice? Hmm?)
     
    jimrthy, Dec 28, 2005 IP