2

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.

3
  • If you get a blank page either you have a mysql error or a php error which are suppressed you might want to take a look at error_reporting try adding error_reporting(E_ALL); at the top and echo a mysql_error() at the bottom. Commented May 10, 2011 at 21:41
  • 2
    Never EVER post your real IP, real usernames, real passwords, real whatever in any forums or in stackoverflow. I suggest you changing your password, if you dont know how check this link cyberciti.biz/faq/mysql-change-user-password Commented May 10, 2011 at 21:42
  • The problem is not withe the mySQL but the syntax that I have write my php code, I have tried to print it with regular html tags and it workt. Commented May 10, 2011 at 21:46

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Agreed, if the quotes are there all it would do is print out $row['xxxxxxxx'], not get the value of it
@bmbaeb - Wrong, it tries to parse $row and the ' throws it off because it's already in a string. $row[xxxxxxxx] is the correct string-embedded-variable alternate.
Thanks alot thirtydot, I love this place. Okey, I like to write my code in OO(object oriented) way.. any tips on how I cant structure that?
Make sure you read the edit to my answer. For an easy change, you could switch to mysqli (or PDO). mysql_connect and friends are rather outdated.
1
  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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.