简体   繁体   English

使用PHP输出MySQL查询的单个结果不起作用

[英]Output single result of a MySQL Query with PHP not working

My table 'viewlevels' has the following data (among other): 我的表“视图级别”具有以下数据(以及其他数据):

id   |title
10   |Cenas

I'm running the SQL query: 我正在运行SQL查询:

SELECT title FROM viewlevels WHERE id=10

Which is returning "Cenas" as expected. 这将按预期返回“ Cenas”。

But using the following PHP script, I just get "texto= " , why? 但是使用以下PHP脚本,我只会得到“ texto =”,为什么?

$res = $db->query("SELECT title FROM viewlevels WHERE id=10");
$res->data_seek(0);
while ($row = $res->fetch_assoc()) {
    echo " texto= " . $row['title'] . "\n";
};

To see both fields you have to echo those columns: 要查看两个字段,您必须回显这些列:

while ($row = $res->fetch_assoc()) {
    echo " id= " . $row['id'] . "\n";
    echo " texto= " . $row['title'] . "\n";
};

You don't need to use data_seek in this instance. 在这种情况下,您无需使用data_seek。

$res = $db->query("SELECT title FROM viewlevels WHERE id=10");

while ($row = $res->fetch_assoc()) {
    echo " texto= " . $row['title'] . "\n";
}

Will work. 将工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM