Mysql queries in PHP and CSS, why won't they work?

Discussion in 'PHP' started by mynameisdi, Mar 18, 2009.

  1. #1
    <?php
    
    // Create a connection to your database.
    
    // Query database and select the last 10 entries.
    $data = mysql_query("SELECT * FROM video ORDER BY id DESC LIMIT 50");
    while($row = mysql_fetch_array($data))
    {
    
    echo "
    
    ".$row[title]."
    
    ";
    
    }
    ?>
    Code (markup):
    If I try to put, say, <div class="cool">
    ".$row[title]."
    </div>

    It displays an error in the query

    although that div class works fine within barebone php.

    Any thoughts why this doesn't work in the queries?
     
    mynameisdi, Mar 18, 2009 IP
  2. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #2
    print("<div class=cool>$row['title']</div>");
    PHP:
     
    ActiveFrost, Mar 18, 2009 IP
  3. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #3
    Note the single and double quote after the word cool.

    Try echo "<li class='cool'". $row[title]. "</li>";
     
    Colbyt, Mar 18, 2009 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    To be correct, you also need quotes around title: $row['title']
     
    SmallPotatoes, Mar 18, 2009 IP
  5. mynameisdi

    mynameisdi Banned

    Messages:
    977
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Actually it works without quotes around <li> :)

    Thanks a lot guys!
     
    mynameisdi, Mar 18, 2009 IP
  6. mynameisdi

    mynameisdi Banned

    Messages:
    977
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Huh, that's odd.

    Whenever I use that tag it kinda messes up my "cool" div.

    It adds a *-symbol to every div. Odd, huh?
     
    mynameisdi, Mar 18, 2009 IP
  7. mynameisdi

    mynameisdi Banned

    Messages:
    977
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #7
    That one doesn't seem to do it either. I have a lot of "-quotes in my query code, maybe that's why print messes it up?
     
    mynameisdi, Mar 18, 2009 IP
  8. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #8
    Try putting the single quotes around title that I missed. It may be echoing your sql statement if it isn't properly formated.
     
    Colbyt, Mar 18, 2009 IP
  9. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #9
    Better one would be

    
    print '<div class="cool">'.$row['title'].'</div>';
    
    PHP:
    ---

    Regarding the * symbol

    1. Use your php editor to search for this * in your file?
    2. If it wasn't found in your file, it must be from your database

    - ads2help
     
    ads2help, Mar 18, 2009 IP
  10. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #10
    And even better would be
    
    echo '<div class="cool">';
    echo $row['title'];
    echo '</div>';
    
    PHP:
    What I want to say is - you can always improve your code :rolleyes:
     
    ActiveFrost, Mar 18, 2009 IP
  11. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #11
    mhm... why not just escape out of php?

    ?>
    <div class="cool">
    <?= $row['title']; ?>
    </div>

    works like a charm :)
     
    PoPSiCLe, Mar 18, 2009 IP
  12. mynameisdi

    mynameisdi Banned

    Messages:
    977
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Escaping from php won't help as it's a "loop" - I'm quering the database for results.

    Or will it..?
     
    mynameisdi, Mar 19, 2009 IP
  13. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #13
    of course it will - you will of course have to end the while-loop at the end, by calling php again :)
     
    PoPSiCLe, Mar 19, 2009 IP
  14. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #14
    It will be the same, whether it is in a loop, or not. :D

    - ads2help
     
    ads2help, Mar 19, 2009 IP
  15. Stylesofts

    Stylesofts Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Well u can try this

    <?php
    
    // Create a connection to your database.
    
    // Query database and select the last 10 entries.
    $data = mysql_query("SELECT * FROM video ORDER BY id DESC LIMIT 50");
    while($row = mysql_fetch_array($data))
    {
    
    echo "<div class='cool'>".$row['title']."</div>";
    
    }
    ?>
    Code (markup):
    This willl work

    Regards,
    Stylesofts Developing Team
     
    Stylesofts, Mar 19, 2009 IP