繁体   English   中英

选择没有列名的列值

[英]Selecting column values WITHOUT column name

我有一个包含两列的表格:标题和内容。

我想选择标题列中的值,所以我执行

"SELECT title FROM table"

它给我这个

[{"title":"Just a test"},{"title":"Just a test 2"},{"title":"Just a test 3"}]

现在我的问题很简单:如何Just a test 3没有列名称的情况下选择值Just a testJust a test 2Just a test 3

我需要使用Android代码将结果发送到应用程序,因此我需要像这样才能解析它们并填充列表视图(否则,我必须在android代码中操纵结果,但我不知道如何去做)。

更新:这是我的PHP代码:

<?php
require 'jsonwrapper.php';
mysql_connect("+++++++","++++++++","+++++++++");
mysql_select_db("my_tripleleon");
$q=mysql_query("SELECT titolo FROM articoli");
while($raw=mysql_fetch_assoc($q))
{       $output[]=$raw;
}
print(json_encode($output));
mysql_close();
?>

使用json_decode($ q)不会打印任何内容...

将查询结果分配给json_decode()传递的$variable ,因为这是您要获取的格式。

$myvar = json_decode($your_mysql_result, true);

这将返回一个关联数组,如下所示:

Array
(
    [0] => stdClass Object
        (
            [title] => Just a test
        )

    [1] => stdClass Object
        (
            [title] => Just a test 2
        )

    [2] => stdClass Object
        (
            [title] => Just a test 3
        )

)

因此,为了获得值,您可以这样做:

foreach ($myvar as $item)
  echo "My item is: " . $item->title . "\n";

----编辑----

替换此代码:

while($raw=mysql_fetch_assoc($q))
{       $output[]=$raw;
}
print(json_encode($output));

通过这个:

while($raw=mysql_fetch_assoc($q))
{       $output[]=$raw['title'];
}
print(json_encode($output));

暂无
暂无

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

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