user592638
user592638

Reputation:

can´t fetch data from db and print it within css and html

I am trying to fetch data from the following database data:

enter image description here

And display the data with this CSS & HTML code:

    <div class="event">
        <img src="http://dummyimage.com/80x70/f00/fff.png" alt="picture" />  
        <p>Room 2</p>
        <p class="patient-name">Jon Harris</p>
        <p class="event-text">This is a pixel. A flying pixel!</p>
        <p class="event-timestamp">feb 2 2011 - 23:01</p>
    </div>

.event {  
    display:block;  
    background: #ececec;  
    width:380px;  
    padding:10px;  
    margin:10px;  
    overflow:hidden;  
    text-align: left;
}  
.event img {
    display:block;
    float:left;
    margin-right:10px;
}  

.event p {  
    font-weight: bold;
}

.event img + p {
    display:inline;
}

.patient-name {
    display:inline;
    color: #999999;
    font-size: 9px;
    line-height:inherit;
    padding-left: 5px;
}
.event-text{
    color: #999999;
    font-size: 12px;
    padding-left: 5px;
}
.event-timestamp{
    color: #000;
    padding-left: 5px;
    font-size: 9px;
}

Here is my PHP code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>DASHBOARD - Arduino 3</title>
  <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
    <?php
    $con = mysql_connect("localhost","*****","***");
        if(!con)
        {
            die('Could not connect: ' . mysql_error());
        }

        mysql_select_db("arduino_db",$con);

        $result = mysql_query("SELECT * FROM events");
        //Start container
        echo " <div id='background_container'> ";

        while($row = mysql_fetch_array($result))
        {
            echo "<div class='event'>";
            echo "<img src='img/ev_img/red.jpg' alt='picture' />";
            echo "<p>" . $row['inneboende'] . "</p>";
            echo "<p class='patient-name'>" . "$row['overvakare']" . "</p>";
            echo "<p class='event-text'>" . "$row['handelse']" . "</p>";
            echo "<p class='event-timestamp'>" . "$row['tid']" . "</p>";
            echo "</div>";
        }

        //end container
        echo "</div>"
        mysql_close($con);

    ?>
</body>
</html>

All I get is a blank page, and I cant understand why.

Upvotes: 2

Views: 1576

Answers (3)

thirtydot
thirtydot

Reputation: 228182

You're missing a semicolon here (I added it):

//end container
echo "</div>";

You should remove the quotes around the $row array variables on these lines, like this:

echo "<p class='patient-name'>" . $row['overvakare'] . "</p>";
echo "<p class='event-text'>" . $row['handelse'] . "</p>";
echo "<p class='event-timestamp'>" . $row['tid'] . "</p>";

With these changes, it works (I tested it).

I suppose that's the one upside of you having posted your actual connection details :)

As mentioned in the comments, you should now definitely change your password.


Also, during development, you should make sure error reporting is enabled, see this answer for various ways to do this.

Upvotes: 1

Nicole
Nicole

Reputation: 33197

  1. row['overvakare'], $row['handelse'], and $row['tid'] should not be enclosed in double quotes. Since they are the only variable in that string, just remove the enclosing double quotes.
  2. echo "</div>" needs a ; on the end.

The script executes (and displays data) after fixing those errors.

Upvotes: 1

onteria_
onteria_

Reputation: 70517

A few things:

If mysql_query fails your loop won't be entered

you're using mysql_fetch_array but trying to access as an associative array. You want mysql_fetch_assoc

From a security standpoint, you shouldn't echo out database values as is if they are obtained from user input. This can lead to nasty things like xss injection. Make sure to use things like strip_tags and other filtering functions to be safe.

Upvotes: 0

Related Questions