从MySQL Query获取所有行

时间:2016-05-17 22:06:05

标签: php mysql

PHP代码:

<?php
//Server Information
$servername = "localhost";
$dbusername = "USERNAME";
$password = "TOTALLYSECUREPASSWORD";
$dbname = "DEFINITELYADATABASE";

//Query Information
$guid = $_POST['GUID'];
$username = $_POST['USERNAME'];
$admin_username = $_POST['ADMIN_USERNAME'];
$ban_reason  = $_POST['BAN_REASON'];
$ip = $_POST['IP'];

//Create Connection
$connection = mysqli_connect($servername, $dbusername, $password, $dbname);

//Check the Connection
if ($connection->connect_error){
    die("Connection failed: " . $connection->connect_error);
}

//$sql = "SELECT DATE, DBUSERNAME, GUID, IP, USERNAME, BAN_REASON FROM bans";
//$result = $connection->query($sql);

$sql = "SELECT * FROM bans WHERE";

$types = json_decode($_POST['QUERY_TYPE'], true);

if (in_array("query_admin_username", $types)) {
    $sql = $sql . " DBUSERNAME = " . "\"" . $admin_username . "\"" . " &&";
}

if (in_array("query_guid", $types)) {
    $sql = $sql . " GUID = " . "\"". $guid . "\"" . " &&";
}

if (in_array("query_ip", $types)) {
    $sql = $sql . " IP = " . "\"" . $ip . "\"" . " &&";
}

if (in_array("query_username", $types)) {
    $sql = $sql . " USERNAME = " . "\"" . $username . "\"" . " &&";
}

if (in_array("query_ban_reason", $types)) {
    $sql = $sql . " BAN_REASON = " . "\"" . $ban_reason . "\"" . " &&";
}

$sql_query = substr($sql, 0, -3);

echo ($sql_query);

$result = $connection->query($sql_query);

while ($connection->query($sql_query)) {

}

if (!$result) {
    die("Invalid Query: " . mysqli_error());
}

$row = $result->fetch_array(MYSQLI_NUM);

while ($row = mysqli_fetch_assoc($result)) {
    echo ($row);
}

mysqli_close($connection);
?>

与所有看起来一样奇怪,它只是我想要的(我认为)。

我的问题:

我希望能够从每一行获取数据并将其导出为一个大型字符串,类似于以下内容:

[DATE] DBUSERNAME banned USERNAME (GUID / IP) for BAN_REASON.

我完全不知道如何解决这个问题。我已经测试了Query并且它返回了应有的一切,但是我使用“echo($ row [0])”等来显示它们,如果它将返回大量的行,这是非常不切实际的。

对不起,如果事情没有意义,我的脑子此刻就被炒了。如果我忘了什么,请告诉我。

1 个答案:

答案 0 :(得分:1)

如果脚本的其余部分有效,您可以将这些列连接起来:

SELECT CONCAT('[',DATE,'] ',DBUSERNAME,' banned ',USERNAME,'(',COALESCE(GUID, IP),),') for ', BAN_REASON) AS your_columns_in_one_line FROM your_table WHERE .....;

请参阅此链接以参考CONCAT