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>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Chris
  • 123
  • 8
  • 1
    You're using the wrong identifier qualifiers for the table http://dev.mysql.com/doc/refman/5.7/en/identifier-qualifiers.html – Funk Forty Niner Sep 27 '16 at 16:20
  • It still doesn't work even if I remove everything below $conn = new mysqli($servername, $username, $password, $dbName); – Chris Sep 27 '16 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 ->`. – Funk Forty Niner Sep 27 '16 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. – Chris Sep 27 '16 at 16:44
  • Ah yes, that missing $ sign seems to have done something. However I now get the message "No games have yet been played" when there are 3 in the database. – Chris Sep 27 '16 at 16:46
  • `SELECT * FROM 'results'` look at that. That is what I popped out at me which is why I closed the question with. The quotes `'` needed to be ticks `\`` or removed altogether. Your original code also had the missing `$` sign which I overlooked; I'm only human after all and I can make mistakes. Now you posted another error message that again is another question entirely. You never fetched anything from the database. You're just wanting to connect. Why are you adding to your question "after the fact" like this? – Funk Forty Niner Sep 27 '16 at 16:48
  • I suggest you delete this question and post a new one, because your commenting after the fact, doesn't support what you posted. It will be better that way. – Funk Forty Niner Sep 27 '16 at 16:51
  • Yes, now it looks like I'm connected to the database but not fetching anything. I removed he `` from `results` and made no difference. OK, I'll delete this question and start a new one now the problem has shifted slightly, but please don't delete it straight away! Thesis been killing me all night and I'm so close!!! – Chris Sep 27 '16 at 16:59
  • 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. – Funk Forty Niner Sep 27 '16 at 17:08
  • Ahhh prefect. Thank you for spotting that. I had gone over the code 100 times and never spotting it. Thanks again! – Chris Sep 27 '16 at 17:29
  • You're welcome. I reopened the question in order to give a full/proper answer. – Funk Forty Niner Sep 27 '16 at 17:32
  • Btw Chris. I'm not kicking for rep points here, but you could mark my answer below as correct if you want, in order to show that the question has been solved. Otherwise, others may think it's still open. – Funk Forty Niner Sep 27 '16 at 17:45
  • 1
    If an answer solved your problem, consider accepting the answer. Here's how http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work then return here and do the same with the tick/checkmark till it turns green. This informs the community, a solution was found. Otherwise, others may think the question is still open and may want to post (more) answers. *Welcome to Stack!* – Jay Blanchard Sep 27 '16 at 17:46
  • Done! Thanks again for your help. I'm sure you'll see my questions pop up again soon. I"m not sure what I'd do without Stack Exchange! – Chris Sep 27 '16 at 17:53
  • @ChrisPaton You're most welcome Chris. Well, anything that we can do to help is part of what we do and enjoy doing; we are always ready to help. However, checking for errors with the links I gave you, will help you a lot during development. If you see a particular error, try to find it on Google first. If you still can't figure out what is wrong, then by all means post a question. Enjoy ;-) *Cheers* – Funk Forty Niner Sep 27 '16 at 17:58

1 Answers1

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:

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141