简体   繁体   中英

Get all rows from MySQL Query

The PHP Code:

<?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);
?>

As weird as all that looks, it works just how I want it to (I think).

My issue:

I want to be able to get the data from each row and export it as one large String, something along the lines of:

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

I just have absolutely no idea how to go about this. I've tested the Query and it's returning everything it should, however I was using "echo ($row[0])" etc to display them, which is pretty impractical if it's going to return a large amount of rows.

Sorry if something doesn't make sense, my brain is fried at the moment. Please let me know if I forgot anything.

You could concatenate the columns like this if the rest of your script works:

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

See this link for reference to CONCAT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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