SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question php tinyint problem

    ok, so its late and i'll be surprised if anyone answers this but this problem is perplexing me. i'm following the sitepoint book on php, and instead of formatting the select results in a paragraph i've been a clever person and rendered them in a table. I have a tinyint(1) field in my mysql db and i can store the value correctly from the form. My while loop looks like this:

    while ($row = mysql_fetch_array($result)) {
    echo '<tr>';
    echo '<td>' . $row['id'] . '</td>';
    echo '<td>' . $row['name'] . '</td>';
    echo '<td>' . $row['type'] . '</td>';
    echo '<td>' . $row['queued' ? Yes : No];
    echo '</tr>';
    }

    queued is the tinyint(1) field. I'm trying to get it to return Yes or No instead of 1 or 0. The only problem is that if I put the '? Yes : No' inside the square brackets [] it pulls up blank, and if i place it after, it prints Yes or No outside the table. HELP!

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,840
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Try it like this:

    echo '<td>' . $row['queued'] ? 'Yes' : 'No' . '</td>';
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo '<td>' . ((int) $row['queued'] == 'Yes' 'No') . '</td>'
    Edit: blast, beaten by mere seconds.

  4. #4
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    great, thanks a lot SJH your solution worked a charm. Now i'll figure out what its doing!

  5. #5
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about enumerated values?

    i'm getting blank with that too. The mysql manual says you have to enclose numbers with quotes if you use them in ENUM types. but that doesn't work! I've also tried ['field1+0'] in the hope that would work.

    my code line is:

    echo '<td>' . $row['priority'] . '</td>';

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,840
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The field name from the database goes inside the $row[] and whatever you are trying to do with the value that has goes outside of it.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, i tried this:

    ((int) $row['priority']+0)

    to bring back a number but its failing. Is there a good reference for how to format different data types from mysql on the net? Apologies if this is one of those blindingly obvious i'm feeling lucky google type questions.

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    run this and post it:

    PHP Code:
    var_dump($row['priority']); 
    If its what I suspect though the answer is to explode the string. However, I would need to see the data dump to confirm that.

  9. #9
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oddz,

    this is coming back as NULL, sounds like what you were expecting?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •