Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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.