简体   繁体   English

如何在Opencart / PHP中将查询结果分配给变量?

[英]How do I assign a query result to a variable in Opencart/PHP?

I'm having a problem trying to assign a single query result to a variable in Opencart. 我在尝试将单个查询结果分配给Opencart中的变量时遇到问题。 For example, 例如,

$variable = $this->db->query("SELECT value FROM mydb WHERE ID='example'");

From what I have read, $variable is now the result of the query in an associative array, if that is true, how can I get the string value from the associative array to be assigned to the variable? 从我的阅读中可以看出,$ variable现在是关联数组中查询的结果,如果是,那么如何从关联数组中获取要分配给变量的字符串值? If I am wrong on anything I have said please correct me. 如果我说的话有误,请纠正我。 Any help is appreciated. 任何帮助表示赞赏。

Thank you! 谢谢!

Try this: 尝试这个:

$query = $this->db->query("SELECT value FROM mydb WHERE ID='example'");

// Check if there are any rows returned from the query
if ($query->num_rows > 0) {

    // Here you are getting array of your row.
    $row = $query->row;

    // Now here you are getting specific column value from our row into your variable.
    $variable = $row['yourcolumnname'];

   // Here we are echo our variable to test it.
    echo $variable;
}

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

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