treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] PHP spit table not working

  • <?php
    $id=$_SESSION['user_id'];
    $fetch=mysql_query("SELECT * FROM music WHERE user=$id limit 10") or die(mysql_error());
    echo("<tr>
    <td>Title</td>
    <td>Composer</td>
    <td>Type</td>
    </tr>");
    while($row = mysql_fetch_array($fetch))
    {
    echo("<tr>
    <td>");
    echo $row['title'];
    echo("</td>");
    echo("<td>");
    echo $row['composer'];
    echo("</td>");
    echo("<td>");
    echo $row['type'];
    echo("</td>");
    echo("</tr>");
    }
    ?>


    As I go, it's probably syntax, but I can't figure it out. After a while, I figured out I was missing three semicolons, but it still doesn't work. Developing locally. Trimmed it down to something here.

    You can see what I'm doing--selecting from mysql table and spitting out the first 10 results in a table. Yes, a table. My first use of tables.

    But the table doesn't show up. That don't help.
  • I think you need to change mysql_fetch_array to mysql_fetch_assoc

    Does it print the first line of the table that is outside the while loop?
  • Where are the
    <table>
    tags?
  • @karlpcrowley actually no. I'm starting to think it's something with my include. I guess assos would be better...why didn't I think of that. But it still doesn't work.

    @TT_Mark they're right around this PHP, I just didn't copypaste them.
  • Just after the query put in
    print_r($fetch);
    It will show an array of data if it's getting the right information
  • also, during development, you should always use error_reporting( -1 );. It really does help.
  • Hem it's still not working. either of those statements.

    I'm starting to think it's my include.


    <?php if(!isset($_SESSION['user_id'])){
    include_once("login.php");
    } else {
    include_once("display.php") or die();
    }
    ?>


    I put die() there so that the rest of my page will load, but it's not working.

    @traq what exactly does that do?
  • Try require_once() instead of include_once(). That way your script will die if the file cannot be included.

    I'd also echo out mysql_num_rows( $fetch ) just to make sure you're actually returning rows
  • Hi peoples,

    Just like it always does, it works for some random reason now. Either include or require works. Thanks for all your help!

    Red