-1

I want to show me from the database ID and User (from table users), this is my code but i keep getint an errors.

<?php
$conn = new mysqli('127.0.0.1','S024_V7','S024_V7','S024_V7');

$sql ="SELECT id, user FROM Users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<br> ID: ". $row["id"]. " - Name: ". $row["User"]."<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>

Errors: Parse error: syntax error, unexpected '""' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';' in /Projekti/S024_V7/index.php on line 9

Notice: Trying to get property of non-object in /Projekti/S024_V7/index.php on line 7 0 results

urosv
  • 63
  • 6
  • Line 7 is a comment and thus can't have a `Trying to get property of non-object` notice. Are you sure the line numbers match your code sample? – ceejayoz Jun 03 '17 at 21:58
  • If your column is `user` instead of what you wrote as `User` in the loop, then that's an issue. Your `$row["User"]` is being interpreted as `$row[""]` which could cause that error. – Funk Forty Niner Jun 03 '17 at 22:00
  • You can not have both this error and the notice at the same time, that is impossible. – CBroe Jun 03 '17 at 22:00
  • Sorry all, i had to transalte it from my native language to english so you can understand it. – urosv Jun 03 '17 at 22:02
  • You didn't given anyone a chance to solve this. This will only benefit you and nobody else. – Funk Forty Niner Jun 03 '17 at 22:03
  • @urosv you code looks fine but you are using double quotes in `echo` statement – Rtra Jun 03 '17 at 22:04
  • Actually not. This will help at least 20 of my college students to finish there home work. @-Fred-ii- Thank you all. – urosv Jun 03 '17 at 22:06
  • @urosv find my solution and try to run in your application – Rtra Jun 03 '17 at 22:07

2 Answers2

0

This is because you have used double quotes "" instead of single '' try this code it will solve your problem

<?php
$conn = new mysqli('127.0.0.1','S024_V7','S024_V7','S024_V7');

$sql ="SELECT id, user FROM Users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<br> ID: ". $row['id']. " - Name: ". $row['user']."<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>
Rtra
  • 514
  • 12
  • 25
-1

Solved:

    $sql = "SELECT idkorisnik, ime FROM korisnik";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "<br> ID: ". $row["idkorisnik"]. " - Ime: ". $row["ime"]. " " . "<br>";
    }
} else {
    echo "0 results";
}
urosv
  • 63
  • 6