简体   繁体   English

如何将串联的MYSQL字符串查询存储到PHP变量中?

[英]How can I store a concatenated MYSQL string query into a PHP variable?

When using phpmyadmin I'm able to see a concatenated string that was ran from a query using the concat() method from mysql. 当使用phpmyadmin时,我可以看到使用MySQL的concat()方法从查询中运行的连接字符串。 However, in actual php code, I don't know how to access the string that was concatenated. 但是,在实际的php代码中,我不知道如何访问串联的字符串。

for example, in phpmyadmin, this query: 例如,在phpmyadmin中,此查询:

SELECT CONCAT("SELECT * FROM `account` WHERE 1") FROM `account` 

returns this result: 返回此结果:

SELECT * FROM `account` WHERE 1

And the result is what I would want stored in a php variable. 结果就是我想要存储在php变量中的内容。

$str = "SELECT * FROM `account` WHERE 1"

Call the fetch() method, it returns an array containing the row data. 调用fetch()方法,它将返回一个包含行数据的数组。 Then access the result from there. 然后从那里访问结果。 I recommend assigning an alias to the expression, then you can use an associative array for the row. 我建议为该表达式分配一个别名,然后可以为该行使用关联数组。

$result = $pdo->query('SELECT CONCAT("SELECT * FROM `account` WHERE 1") AS str FROM `account`');
$row = $result->fetch(PDO::FETCH_ASSOC);
$str = $row['str'];

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

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