q.php add csv output
This commit is contained in:
parent
833a4d689d
commit
e4b164718a
239
q.php
239
q.php
|
|
@ -45,142 +45,139 @@
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<h2>Execute Query</h2>
|
<h2>Execute Query</h2>
|
||||||
<form method="post" action="">
|
<form method="post" action="">
|
||||||
<textarea id="query" name="query" rows="4" cols="50" placeholder="Enter your MySQL query here"><?php
|
<textarea id="query" name="query" rows="4" cols="50" placeholder="Enter your MySQL query here"><?php
|
||||||
echo $_POST['query'];
|
echo isset($_POST['query']) ? htmlspecialchars($_POST['query']) : '';
|
||||||
?></textarea><br><br>
|
?></textarea><br><br>
|
||||||
<button type="submit" name="submit">Execute Query</button>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<h3>Available Queries</h3>
|
<label>
|
||||||
<ul>
|
<input type="checkbox" name="csv_output" value="1" <?php
|
||||||
<?php
|
echo !empty($_POST['csv_output']) ? 'checked' : '';
|
||||||
$file = 'queries.txt';
|
?>>
|
||||||
|
CSV output
|
||||||
|
</label>
|
||||||
|
<br><br>
|
||||||
|
|
||||||
// Check if the file exists, if not, create it
|
<button type="submit" name="submit">Execute Query</button>
|
||||||
/*
|
</form>
|
||||||
if (!file_exists($file)) {
|
|
||||||
$writeResult = file_put_contents($file, ""); // Create the file if it doesn't exist
|
|
||||||
|
|
||||||
// Check if the file write was successful
|
<h3>Available Queries</h3>
|
||||||
if ($writeResult === false) {
|
<ul>
|
||||||
echo "<p>Error: Could not write {$file}</p>";
|
<?php
|
||||||
|
$file = 'queries.txt';
|
||||||
|
|
||||||
|
if (file_exists($file)) {
|
||||||
|
$queries = file($file, FILE_IGNORE_NEW_LINES);
|
||||||
|
echo '<select id="querySelect" onchange="populateQuery(this.value)">';
|
||||||
|
echo '<option value="">Select a query...</option>';
|
||||||
|
foreach ($queries as $line) {
|
||||||
|
list($label, $query) = explode('|', $line, 2);
|
||||||
|
echo '<option value="' . htmlspecialchars($query) . '">' . htmlspecialchars($label) . '</option>';
|
||||||
|
}
|
||||||
|
echo '</select>';
|
||||||
|
} else {
|
||||||
|
echo "<p>{$file} file not found.</p>";
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<?php
|
||||||
|
if (isset($_POST['submit'])) {
|
||||||
|
// Connection parameters (adjust for your MySQL server)
|
||||||
|
include_once("peter_db.php");
|
||||||
|
$peter_db = new peter_db();
|
||||||
|
$conn = $peter_db->getConnection();
|
||||||
|
|
||||||
|
if ($conn->connect_error) {
|
||||||
|
die("Connection failed: " . $conn->connect_error);
|
||||||
|
}
|
||||||
|
|
||||||
|
$query = $_POST['query'] ?? '';
|
||||||
|
$as_csv = !empty($_POST['csv_output']);
|
||||||
|
|
||||||
|
// Helper: CSV line builder (RFC 4180 style quoting)
|
||||||
|
$csv_line = function(array $vals): string {
|
||||||
|
$out = [];
|
||||||
|
foreach ($vals as $v) {
|
||||||
|
if ($v === null) {
|
||||||
|
$out[] = '';
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
$s = (string)$v;
|
||||||
} */
|
// Normalize line breaks
|
||||||
|
$s = str_replace(["\r\n", "\r"], "\n", $s);
|
||||||
// Open the queries.txt file and read each line
|
// Escape double quotes by doubling them
|
||||||
/*if (file_exists($file)) {
|
if (strpbrk($s, ",\"\n") !== false) {
|
||||||
$queries = file($file, FILE_IGNORE_NEW_LINES); // Read lines into an array
|
$s = '"' . str_replace('"', '""', $s) . '"';
|
||||||
foreach ($queries as $query) {
|
|
||||||
// Create a link for each query in the file
|
|
||||||
echo '<li><a href="#" onclick="populateQuery(\'' . addslashes($query) . '\')">' . htmlspecialchars($query) . '</a></li>';
|
|
||||||
}
|
}
|
||||||
|
$out[] = $s;
|
||||||
|
}
|
||||||
|
return implode(',', $out);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Split on semicolons (simple splitter; won’t handle semicolons inside strings)
|
||||||
|
$queries = array_filter(array_map('trim', explode(';', $query)));
|
||||||
|
|
||||||
|
foreach ($queries as $sql) {
|
||||||
|
if ($sql === '') { continue; }
|
||||||
|
|
||||||
|
echo "<p><code>" . htmlspecialchars($sql) . "</code></p>";
|
||||||
|
|
||||||
|
$result = $conn->query($sql);
|
||||||
|
|
||||||
|
if ($result === FALSE) {
|
||||||
|
echo "<p>Error: " . htmlspecialchars($conn->error) . "</p>";
|
||||||
|
} elseif ($result === TRUE) {
|
||||||
|
echo "<p>Query executed successfully.</p>";
|
||||||
} else {
|
} else {
|
||||||
echo "<p>{$file} file not found.</p>";
|
// SELECT-like result
|
||||||
}
|
if ($as_csv) {
|
||||||
*/
|
// Build header
|
||||||
|
$fields = $result->fetch_fields();
|
||||||
|
$headers = array_map(fn($f) => $f->name, $fields);
|
||||||
|
$csv = [];
|
||||||
|
$csv[] = $csv_line($headers);
|
||||||
|
|
||||||
|
// Rows
|
||||||
|
while ($row = $result->fetch_assoc()) {
|
||||||
|
// Preserve column order per $headers
|
||||||
|
$vals = [];
|
||||||
|
foreach ($headers as $h) { $vals[] = $row[$h]; }
|
||||||
|
$csv[] = $csv_line($vals);
|
||||||
|
}
|
||||||
|
|
||||||
if (file_exists($file)) {
|
$csv_text = implode("\n", $csv) . "\n";
|
||||||
$queries = file($file, FILE_IGNORE_NEW_LINES); // Read lines into an array
|
|
||||||
echo '<select id="querySelect" onchange="populateQuery(this.value)">';
|
|
||||||
echo '<option value="">Select a query...</option>'; // Default placeholder option
|
|
||||||
|
|
||||||
foreach ($queries as $line) {
|
// Show in a textarea for easy copy without HTML escaping issues
|
||||||
list($label, $query) = explode('|', $line, 2); // Split label and query
|
// Adjust rows/cols as you like
|
||||||
echo '<option value="' . $query . '">' . htmlspecialchars($label) . '</option>';
|
$rows = min(40, max(10, count($csv) + 2));
|
||||||
}
|
echo '<textarea readonly rows="' . $rows . '" cols="120">'
|
||||||
|
. htmlspecialchars($csv_text)
|
||||||
|
. '</textarea><br><br>';
|
||||||
|
} else {
|
||||||
|
// HTML table
|
||||||
|
echo "<table border='1'><tr>";
|
||||||
|
$fields = $result->fetch_fields();
|
||||||
|
foreach ($fields as $field) {
|
||||||
|
echo "<th>" . htmlspecialchars($field->name) . "</th>";
|
||||||
|
}
|
||||||
|
echo "</tr>";
|
||||||
|
|
||||||
echo '</select>';
|
while ($row = $result->fetch_assoc()) {
|
||||||
} else {
|
echo "<tr>";
|
||||||
echo "<p>{$file} file not found.</p>";
|
foreach ($row as $value) {
|
||||||
}
|
echo "<td>" . htmlspecialchars((string)$value) . "</td>";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
?>
|
|
||||||
</ul>
|
|
||||||
|
|
||||||
|
|
||||||
<?php
|
|
||||||
if (isset($_POST['submit'])) {
|
|
||||||
// Connection parameters (adjust for your MySQL server)
|
|
||||||
include_once("peter_db.php");
|
|
||||||
$peter_db = new peter_db();
|
|
||||||
$conn = $peter_db -> getConnection();
|
|
||||||
|
|
||||||
// Check connection
|
|
||||||
if ($conn->connect_error) {
|
|
||||||
die("Connection failed: " . $conn->connect_error);
|
|
||||||
}
|
|
||||||
|
|
||||||
$query = $_POST['query']; // Get the query from the text area
|
|
||||||
|
|
||||||
// Split the query if multiple queries are entered
|
|
||||||
$queries = explode(';', $query);
|
|
||||||
|
|
||||||
// Process each query
|
|
||||||
foreach ($queries as $sql) {
|
|
||||||
$sql = trim($sql); // Remove extra spaces
|
|
||||||
|
|
||||||
echo $sql;
|
|
||||||
echo "<p>\n";
|
|
||||||
if ($sql) {
|
|
||||||
$result = $conn->query($sql);
|
|
||||||
|
|
||||||
print_r($result);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if ($result === FALSE) {
|
|
||||||
echo "<p>Error: " . $conn->error . "</p>";
|
|
||||||
} elseif ($result === TRUE) {
|
|
||||||
echo "<p>Query executed successfully.</p>";
|
|
||||||
} else {
|
|
||||||
// If it's a SELECT query, display results in a table
|
|
||||||
echo "<table border='1'><tr>";
|
|
||||||
// Display table headers
|
|
||||||
$fields = $result->fetch_fields();
|
|
||||||
foreach ($fields as $field) {
|
|
||||||
echo "<th>" . $field->name . "</th>";
|
|
||||||
}
|
}
|
||||||
echo "</tr>";
|
echo "</tr>";
|
||||||
|
|
||||||
// Display rows
|
|
||||||
while ($row = $result->fetch_assoc()) {
|
|
||||||
echo "<tr>";
|
|
||||||
foreach ($row as $value) {
|
|
||||||
//echo "<td>" . htmlspecialchars($value) . "</td>";
|
|
||||||
echo "<td>" . $value . "</td>";
|
|
||||||
}
|
|
||||||
echo "</tr>";
|
|
||||||
}
|
|
||||||
echo "</table><br><br>";
|
|
||||||
}
|
}
|
||||||
|
echo "</table><br><br>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
// Now let's append the query to queries.txt if it's not already there
|
|
||||||
$queries = file($file, FILE_IGNORE_NEW_LINES); // Read the existing queries into an array
|
|
||||||
if (!in_array($query, $queries)) {
|
|
||||||
$queries[] = $query; // Add the new query to the array
|
|
||||||
sort($queries); // Sort the queries alphabetically
|
|
||||||
file_put_contents($file, implode(PHP_EOL, $queries) . PHP_EOL); // Save the sorted queries back to the file
|
|
||||||
//echo "<p>Query added to queries.txt.</p>";
|
|
||||||
} else {
|
|
||||||
echo "";
|
|
||||||
//echo "<p>Query already exists in queries.txt.</p>";
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
$conn->close();
|
|
||||||
}
|
}
|
||||||
?>
|
|
||||||
|
$conn->close();
|
||||||
|
}
|
||||||
|
?>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Loading…
Reference in New Issue