Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从mysqli结果中检索单个列 这是我的代码:_Php - Fatal编程技术网

Php 如何从mysqli结果中检索单个列 这是我的代码:

Php 如何从mysqli结果中检索单个列 这是我的代码:,php,Php,输出不完全是我想要的。我只需要审计、所得税等价值观。有人能帮我吗 所需产出: 稽核 所得税尝试使用$que[0]: while($que = mysqli_fetch_row($query)) { echo "<pre>"; print_r($que[0]); } <?php while($que = mysqli_fetch_row($query)) { echo "<pre>"; echo $que[0]; } ?> while(

输出不完全是我想要的。我只需要审计、所得税等价值观。有人能帮我吗

所需产出: 稽核

所得税

尝试使用$que[0]:

while($que = mysqli_fetch_row($query))
{
echo "<pre>";
print_r($que[0]);
} 
<?php

while($que = mysqli_fetch_row($query))
{
    echo "<pre>";
    echo $que[0];
} 
?>
while($que=mysqli\u fetch\u row($query))
{
回声“;
打印($que[0]);
} 
试试这个

// list the columns you want to get from each row as variables
$statement->bind_result($tagName);

// then loop the results and output the columns you bound above
while($statement->fetch()){
    echo $tagName.'<br>';
}

对于初学者,应该使用准备好的语句来避免SQL注入。之后,您可以使用
mysqli::bind_result
mysqli_stmt::get_result
mysqli_result::fetch_array
。我将在这里演示这两种方法

将这些实践纳入考虑是很重要的,因为您使用的是mysqli而不是mysql,所以利用这个库带来的增强功能是有意义的

首先,准备一份声明:

$statement->free_result();
上面的代码将安全地准备查询并在数据库中启动它,您现在可以使用一些结果了。如前所述,您可以使用两种不同的方法。首先是
bind\u result
,如下所示:

$result = array_column($connection->query('SELECT xy FROM table')->fetch_all(MYSQLI_ASSOC), 'xy');
。。。在这两种情况下,完成后使用
free\u result

文档

  • mysqli::prepare
    -
  • mysqli\u stmt::获取结果
  • mysqli\u stmt::bind\u param
    -
  • mysqli\u stmt::bind\u execute
    -
  • mysqli\u stmt::bind\u result
    -
  • mysqli\u stmt::fetch
    -
  • mysqli\u结果::获取数组
    -

如果您只想以assoc数组的形式获取结果,而不想使用bind_param等。您可以使用php函数
array_column
,并在结果上指定列名:


预期输出和实际输出之间有什么区别?我只需要输出值,不需要数组格式。代码中可能存在SQL注入漏洞。您正在使用
mysqli.*
,但仍在将值连接到查询中。查看参数化语句,否则使用
mysqli\u*
比使用
mysql\u*
好不了多少。嗨,克里斯,我同意你的代码。但我是新来的。当我使用你的代码时,我发现了一些错误。你能通过邮件帮我吗?不,但是你可以在这里询问,或者如果你已经孤立了一个特定的问题,可以发布一个新的问题。
// initiate database connection
$db = mysqli_connect(
    "localhost",
     "user",
     "password",
     "database"
);

// set up your statement with ? parameters
$statment = $db->prepare('
    SELECT 
        t.tag_name 
    FROM 
        tag_map tm 
    JOIN 
        article p ON
            p.post_id = tm.post_id 
    JOIN 
        tag t ON 
            t.tag_id = tm.tag_id 
    WHERE
        p.post_id = ?
');

// bind values for each parameter
$statement->bind_param('i', $post_id);

// execute the statement
$statement->execute();
// list the columns you want to get from each row as variables
$statement->bind_result($tagName);

// then loop the results and output the columns you bound above
while($statement->fetch()){
    echo $tagName.'<br>';
}
$result = $statement->get_result();
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
    echo $row['tag_name'].'<br>';
}
$statement->free_result();
$result = array_column($connection->query('SELECT xy FROM table')->fetch_all(MYSQLI_ASSOC), 'xy');