PHP数据库选择

时间:2017-06-03 21:54:51

标签: php

我想从数据库ID和用户(来自表用户)向我展示,这是我的代码,但我保持getint错误。

<?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();
?>

错误: 解析错误:语法错误,意外的“”“(T_CONSTANT_ENCAPSED_STRING),期待','或';'在第9行的/Projekti/S024_V7/index.php

注意:尝试在第7行的/Projekti/S024_V7/index.php中获取非对象的属性 0结果

2 个答案:

答案 0 :(得分:0)

这是因为您使用了双引号""而不是单''尝试此代码它将解决您的问题

<?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();
?>

答案 1 :(得分:-1)

解决:

    $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";
}