Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

trouble printing off array derived from MySQL db

Options
  • 15-07-2005 6:39pm
    #1
    Closed Accounts Posts: 38


    I want to select a single record from a MySQL database, stick it in an array and then print off the keys in one table column and the values inside a textbox for editing in a second contiguous column.

    Only problem is the resultant array ($rows) has interpolated additional numerically indexed key-value pairs doubling the number of fields. What is going on here? It's doing my head in.

    ============================================
    $query = "SELECT * FROM $table
    WHERE student_id = $student_id";

    $result = mysql_query($query);
    $rows = mysql_fetch_array($result);

    reset($rows);
    //start table and print headings
    echo("<table><tr><th>Field</th><th>Value</th></tr>");
    // print remaining values
    while (list($col1,$col2) = each($rows)) {
    echo ("<tr><th>$col1</th><td><input type=text name=$col1 value=$col2 /></td></tr>");
    }
    // end table
    echo "</table>";
    ============================================

    This above code results in the following:
    Field Value
    0 135
    student_id 135
    1 2
    lunches 2
    2 2
    dinners 2

    I don't want the numerically indexed keys (0, 1 and 2) or their values, only those with the db table field names and their values.


Comments

  • Closed Accounts Posts: 38 PixelPixie


    I should have mentioned that the language used is PHP.


  • Registered Users Posts: 1,393 ✭✭✭Inspector Gadget


    Hmm...

    First thing I'd say is to recommend you look into a database abstraction layer such as ADOdb (no, this one isn't the Microsoft thing, do a google) or PEAR's DB thingy.

    Second, your PHP generates HTML, and you're giving back plain text that looks like you copied-and-pasted it out of the browser window. Pull up the outputted page's source, copy out the relevant bit of actual HTML, and post that - preferably in {code}{/code} blocks (replace the curly brackets with square ones), and you'll get:
    Nice monospaced text that makes code easy to read
    

    If you give the actual output like this it makes diagnosing such problems much easier.

    Gadget


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Afaik, mysql_fetch_array() when used without an array type indentifier, fetches both an associative and a numerical array, resulting in the behaviour you're getting there. To print off the field name and the value like you have there, you just need to change one line;
    $rows = mysql_fetch_array($result);
    change to
    $rows = mysql_fetch_array($result, MYSQL_ASSOC);

    This should ensure that you only get the associative array and not the numerical one.


  • Closed Accounts Posts: 38 PixelPixie


    That's brilliant, Seamus. Thanks a mill!
    seamus wrote:
    Afaik, mysql_fetch_array() when used without an array type indentifier, fetches both an associative and a numerical array, resulting in the behaviour you're getting there. To print off the field name and the value like you have there, you just need to change one line;
    $rows = mysql_fetch_array($result);
    change to
    $rows = mysql_fetch_array($result, MYSQL_ASSOC);

    This should ensure that you only get the associative array and not the numerical one.


  • Closed Accounts Posts: 38 PixelPixie


    First thing I'd say is to recommend you look into a database abstraction layer such as ADOdb (no, this one isn't the Microsoft thing, do a google) or PEAR's DB thingy.

    I have a look into that when I have a bit more time, Gadget.
    Second, your PHP generates HTML, and you're giving back plain text that looks like you copied-and-pasted it out of the browser window. Pull up the outputted page's source, copy out the relevant bit of actual HTML, and post that - preferably in {code}{/code} blocks (replace the curly brackets with square ones), and you'll get:
    Nice monospaced text that makes code easy to read
    

    If you give the actual output like this it makes diagnosing such problems much easier.

    Gadget

    I appreciate that, Gadget, but in this case I'm developing on my laptop which is not connect to the internet right now, and accessing the web from my main machine, so I've actually retyped the code.
    In any case I'll do as you advise in future.


  • Advertisement
  • Registered Users Posts: 1,393 ✭✭✭Inspector Gadget


    Sorry about that; wasn't up to mentally piecing the text output back together to figure out what was what at the time, hence the request for the HTML output.

    (If you didn't know how to do that already, it's probably useful for next time anyway ;) )

    Gadget


Advertisement