简体   繁体   English

如何做mysql db的多部分PHP查询

[英]How to do multi-part PHP query of mysql db

I have read through hundreds of posts on here and on other sites. 我已经在这里和其他网站上阅读了数百篇帖子。 I am not overly familiar with PHP and mysql, but already have my tables built in mysql, have called them using single checklists and filtered them based on date and time. 我并不太熟悉PHP和mysql,但已经在mysql中构建了我的表,使用单个清单调用它们并根据日期和时间对它们进行过滤。 However, in the multi-checkbox arena, I cannot seem to pin down what I'm doing wrong for an array code and customizing the resulting table. 但是,在多重复选框领域,我似乎无法确定我为数组代码做错了什么并自定义结果表。 This is the basic html: 这是基本的html:

<html> <body> <form method="POST" action="php/minute_feedback.php">
Date: <input type="text" name="from_date" id="from_date" value="2016-04-07">
<input type="checkbox" name="check_list[]" value="gate" id="gate">
<input type="checkbox" name="check_list[]" value="pressure" id="pressure">
<input type="checkbox" name="check_list[]" value="flow" id="flow">
<input type="submit" name="submit" value="submit">

This is the basic PHP (I'll only include the date parameters vs. date & time to keep it shorter since the inclusion will be the same): 这是基本的PHP(我只会包含日期参数与日期和时间以保持更短,因为包含将是相同的):

<?php
$servername = "myhostaddress.com";
$username = "myusername";
$password = "mypassword";
$dbname = "mydatabasename";
$conn=new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);}
if($_POST['submit'] == "submit") {
$from_date = ($_POST['from_date']);
$to_date = ($_POST['from_date']);}
if(isset($_POST['submit'])) {

Now ... I know there should be some sort of an array code in here like: 现在......我知道这里应该有一些数组代码,如:

$checklist[] = $_POST[check_list];

But - I'm not sure what the correct syntax/code is. 但是 - 我不确定正确的语法/代码是什么。 Next question is about my query. 下一个问题是关于我的查询。 I've seen most queries using a WHERE + IN combo and a '*' for SELECT. 我见过大多数查询使用WHERE + IN组合,而'*'用于SELECT。 However, I need something closer to: 但是,我需要更接近的东西:

$sql = "SELECT Fdate, Ftime, (list of variable columns from the checked options such as 'gate', 'pressure', 'flow' --> but only if they've been checked) FROM minute_db WHERE Fdate BETWEEN '$from_date' AND '$to_date'";

$result = $conn->query($sql);}
if ($result->num_rows > 0) {

Now - as for my table headers, I can use the following code if I had a single value being looked up: 现在 - 至于我的表头,如果我查找了一个值,我可以使用以下代码:

echo "<table><tr><th>Date</th><th>Time</th><th>Level (ft)</th></tr>";}

But, I need the header to be dynamically populated with the results of the checked items (1, 2, or 3 or more variables as needed) plus the date and time records. 但是,我需要使用已检查项目的结果动态填充标题(根据需要添加1,2或3个或更多变量)以及日期和时间记录。

echo "<table>";}
else {
echo "0 results";}
$conn->close();
?>

My end result is that the user can enter a date and time range and choose which data to display, so for example, if they want to know just the gate setting and pressure on 4/7/2016, they'd enter that date and check those two boxes so they will get a table (no formatting below - just trying to represent), for example: 我的最终结果是用户可以输入日期和时间范围并选择要显示的数据,例如,如果他们只想知道2016年4月7日的门设置和压力,他们就会输入该日期和检查这两个框,这样他们就会得到一张桌子(下面没有格式 - 只是试图表示),例如:

Date       |     Time |   Gate Setting  |  Pressure 
----------------------------------------------------
2016-04-07 |    11:00 |      21         |      50 

I will ultimately have a table with tens of thousands of rows of data to select from (and will include limiters to the amount of data that can be pulled), and up to 56 columns of variables to select from. 我最终会有一个包含数万行数据的表格(并且将包括可以提取的数据量的限制器),以及最多56列可供选择的变量。 I'm not using any special/fancy math functions to group time periods, etc. - I've already manually separated it all out and uploaded it so it's ready to go. 我没有使用任何特殊/花哨的数学函数来分组时间段等等 - 我已经手动将它全部分开并上传它所以它已准备就绪。 I've tried a lot of different solutions from the internet, but have ultimately failed. 我从互联网上尝试了很多不同的解决方案,但最终都失败了。 The * option for SELECT doesn't seem like what I what, neither does the IN option - but I am not sure (just speculating as neither has worked). SELECT的*选项看起来不像我的内容,也不是IN选项 - 但我不确定(只是猜测因为两者都没有用)。 I'm on a netfirms mysql platform, so it's pretty updated (and I'll be changing everything to mysqli to avoid whatever problems stem from not using it - so any feedback with that in advance would be AWESOME!!. I appreciate any ideas you have! 我在netfirms的mysql平台上,所以它已经很新了(我将把所有东西都改成mysqli以避免因为没有使用它而产生的任何问题 - 所以任何提前反馈都会很棒!!我很欣赏任何想法你有!

Thanks to Deep's solution above - it works perfectly as-is! 感谢Deep的解决方案 - 它完美无缺! AWESOME. 真棒。 As for the use of the table array, I was struggling because I could populate the table, but was getting duplicates of date and time (as noted in my replies above). 至于表数组的使用,我很挣扎,因为我可以填充表格,但是正在重复日期和时间(如上面的回复中所述)。 However - I simply moved the script around a bit and it works. 但是 - 我只是简单地移动了脚本并且它可以工作。 Now, the user will get back columns with the date, time, and then the additional variables they are looking for, side by side, across the page, as comparative values (note - I have not yet gotten to the point of mysqli or pdo, so if this is old, please update as you need). 现在,用户将返回列中的日期,时间,然后是他们在页面上并排查找的其他变量作为比较值(注意 - 我还没有达到mysqli或pdo的要点,如果这是旧的,请根据需要更新)。 Hopefully this helps - and thanks again! 希望这会有所帮助 - 再次感谢! So - from the sql line from Deep's post above (fully filled out), this is the table --> 所以 - 从上面的Deep帖子的sql行(完全填写),这是表 - >

$sql = "SELECT Fdate, {$wantedFields} Ftime FROM minute_db WHERE Fdate BETWEEN '$from_date' AND '$to_date' AND Ftime BETWEEN '$from_time' AND '$to_time'";

$result = $conn->query($sql);
}
if ($result->num_rows > 0) {
//header fields
echo "<table><tr><th>Date</th><th>Time</th>";       
        foreach($tableDynFields as $c) {
        echo "<th>$c</th>";
    }
    echo "</tr>";
//results
while ($row = $result->fetch_assoc()) {                         
            echo "<tr><td>".$row["Fdate"]."</td><td>".$row["Ftime"]."  </td>";
        foreach($tableDynFields as $c) {
            echo "<td>".$row[$c]."</td>";
        }
        echo "</tr>";
    }
    echo "</table>"; 
//result set free
  $result->free(); 
    } else {
        echo "query failed.<hr/>";
    }
$conn->close();
?>

The only thing I'll add is if you're having to filter and sort through as much data as I am - tis good to either put a forced limiter at the end of the select statement, or something to that effect! 我要添加的唯一内容是,如果你必须过滤和排序尽可能多的数据 - 在select语句的末尾放置一个强制限制器,或者那样的效果好!

$checklist     = $_POST['check_list'];
$allowedFields = [
    'gate'     => 'Gate',
    'pressure' => 'Pressure',
    'flow'     => 'Flow',
    'etc'      => 'Other field title'
];
$wantedFields   = [];
$tableDynFields = [];
foreach ($checklist as $field) {
    if (array_key_exists($field, $allowedFields)) {
        $wantedFields[]   = $field;
        $tableDynFields[] = $allowedFields[$field];
    }
}
if ($wantedFields) {
    $wantedFields = join(',', $wantedFields) . ',';
} else {
    $wantedFields = '';
}
$sql = "SELECT Ftime, {$wantedFields} Fdate FROM ...';
// and here you have $tableDynFields array for table generation

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

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