Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Trying to show data from mysql db to my page!!

The db is connected but it doesent output from it, my code is below. Please help.

$servername = "sql303.byethost6.com";
$username = "b6_17546085";
$password = "hiding password but this works";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully<br>";
?>
<?php
$sql = "SELECT id, thePost FROM postings";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["thePost"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

1 Answer

2 things I'd attempt with the debug - why don't you try using only an if($result) { // } block instead of asking for rows > 0 and see if that echoes anything out.

Also, I'd use single quotes inside the array keys in your echo statement and see if that helps; it could be a syntax error located in there.

echo "id: " . $row['id'] . " - Name: " . $row['thePost'] . "<br>";

Thanks a lot mate, Ill try it out.