0

I another issue with MySQL (if you were following my last post I have decided to scrap inputting and focus on selecting for now).

I have been following a youtube tutorial on how to display data from my database on my php page. The following code is EXACTLY what he has given me.

However I'm sure the problem lies somewhere in connecting to the database, because if I remove everything other than that the connection code I still get the same problem.

When I load the page it just goes blank.

On Youtube when he loads the page he gets his results.

I've gone over my username, password and DB name 100 times and they are correct.

Can anyone see any problems with the following code?

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<?php

$servername = "localhost";
$username = "cbdadmin";
$password = "XXXX";
$dbName = "cbd_players";

//create connection

$conn = new mysqli($servername, $username, $password, $dbName);

// check connection

if (conn -> connect_error) {
    die ("connection failed: " . $conn -> connect_error);
}

$sql = "SELECT * FROM 'results'";

$result = $conn ->query($sql);

if ($result-> unm_rows > 0) {
    echo "<table> <tr><th>Home Team</tr></th> <tr><th>Home Score</tr></th> <tr><th>Away Score</tr></th> <tr><th>Awa Team</tr></th> <tr><th>Venue</tr></th>";
    while($row = $result -> fetch_assoc()){
        echo "<tr><td>" . $row["hometeam"] . "</td> <td>" . $row["homescore"] . "</td> <td>" . $row["awayteam"] . "</td> <td>" . $row["awayscore"] . "</td> <td>" . $row["venue"] . "</td></tr>";
    }

    echo "</table>";

}

else {
    echo "No game have yet been played.";
}

$conn->close();


?>

<p>Test</p>
</body>
</html>
14
  • 1
    You're using the wrong identifier qualifiers for the table dev.mysql.com/doc/refman/5.7/en/identifier-qualifiers.html Commented Sep 27, 2016 at 16:20
  • It still doesn't work even if I remove everything below $conn = new mysqli($servername, $username, $password, $dbName); Commented Sep 27, 2016 at 16:35
  • now you went and completely changed your question from the original. Can you define "doesn't work"? How are you using this file, as http://localhost/ (or your hosted site URL), or as file:///? What's the file's extension, .php or .html? I can't see your code failing otherwise. However, you do have a syntax error here if (conn -> where error reporting would have thrown you an undefined constant conn notice. Besides that, can't see how it would fail. You missed the $ for conn ->. Commented Sep 27, 2016 at 16:39
  • Yes it told me "your question has been marked as an exact duplicate, please change it" - although the exact duplicate I was given wasn't exactly an "exact duplicate". This is a new page called league.php I have a couple of other basic .php pages that connect to the database no problem with the exact same code. That's what I don't get. Commented Sep 27, 2016 at 16:44
  • 1
    if ($result-> unm_rows > 0) a typo here that needs to read as if ($result-> num_rows > 0). Again; error reporting and checking with proper error handling would have helped you here. If you post the same codes again, your question may be closed by others as typos. Commented Sep 27, 2016 at 17:08

1 Answer 1

2

Your code contains a few syntax errors.

This part of your code:

if (conn -> connect_error) {
    die ("connection failed: " . $conn -> connect_error);
}

$sql = "SELECT * FROM 'results'";

$result = $conn ->query($sql);

if ($result-> unm_rows > 0) {

and I will explain.

The single quotes around your table ' either need to be removed or use backticks, since they are not the right identifier qualifiers:

Then your if (conn -> with the missing $ in front of conn would have thrown an undefined constant conn notice using error reporting.

The unm_rows is a typo which should have read as num_rows.

Check for errors on the query also:

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

1 Comment

Super spacing out the -> operator is also a bad habit as well.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.