从数据库中读取值,echo

时间:2016-10-26 10:40:19

标签: php database mysqli

非常基本,但作为一个新手,我正在努力。回声不显示任何值,只显示文本。我做错了什么?

Connect.php:

<?php
$connection = mysqli_connect('test.com.mysql', 'test_com_systems', 'systems');
if (!$connection){
    die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, 'swaut_com_systems');
if (!$select_db){
    die("Database Selection Failed" . mysqli_error($connection));
}
?>

Get.php:

<?php
    require('connect.php');
        $query2 = "SELECT systemid FROM user WHERE username=test";
        $result2 = mysqli_query($connection, $query2);

    echo ( 'SystemID: '.$result2);

    ?>

3 个答案:

答案 0 :(得分:3)

假设您已成功连接到数据库,则查询不正确。您必须将所有文本值包装在这样的引号中

<?php
    require('connect.php');
    $query2 = "SELECT systemid FROM user WHERE username='test'";
    $result2 = mysqli_query($connection, $query2);

现在mysqli_query将查询提交到运行它的数据库并构建结果集。要查看结果集,您需要使用fetch函数之一从数据库中读取结果集,例如

    $row = mysqli_fetch_assoc($result2);

    echo 'SystemID: ' . $row['systemid'];

如果结果集中有多行,则必须在这样的循环中执行此操作

    while ($row = mysqli_fetch_assoc($result2)){
        echo 'SystemID: ' . $row['systemid'];
    }

答案 1 :(得分:0)

您正在打印mysqli结果object。为了print您必须使用的结果:

$row = mysqli_fetch_assoc($result2);
print_r($row);

答案 2 :(得分:0)

您需要使用以下内容收集mysqli_query的结果:

require('connect.php');
$query2 = "SELECT systemid FROM user WHERE username=test";
$result2 = mysqli_query($connection, $query2);    

while ($row = mysqli_fetch_assoc($result2)) 
{
   echo "System ID is: " .  $row['systemid'];
}