Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在两个表中选择。。。_Php_Mysql_Sql_Select_Join - Fatal编程技术网

Php 在两个表中选择。。。

Php 在两个表中选择。。。,php,mysql,sql,select,join,Php,Mysql,Sql,Select,Join,我有两个表,我想用来查看我的报告,我可以得到输入日期后 这是我的表格:客户-客户日期、姓氏、姓氏 服务-房间号、入住日期、退房日期 下面是我的代码:它似乎无法从我的表中获取任何行 <?php $conn = mysql_connect("localhost","root",""); mysql_select_db('irm',$conn); if(isset($_GET['Submit'])){ $customer_date = $_GET['customer_date']; }

我有两个表,我想用来查看我的报告,我可以得到输入日期后

这是我的表格:客户-客户日期、姓氏、姓氏

服务-房间号、入住日期、退房日期

下面是我的代码:它似乎无法从我的表中获取任何行

<?php 

$conn = mysql_connect("localhost","root","");

mysql_select_db('irm',$conn);

if(isset($_GET['Submit'])){

$customer_date = $_GET['customer_date'];
}

?>
<form method="get">
<table width="252" border="0">
  <tr>
    <td width="98">Choose Date:</td>
    <td width="144"><label>
  <input onclick="ds_sh(this);" name="customer_date" id="customer_date" readonly="readonly" style="cursor: text" />
</label></td>
 </tr>
 <tr>
<td align="right"><input type="submit" value="Submit" /></a></td>
<td></td>
</tr>
</table>
</form>  

<form>
<?php

    $tryshow = "SELECT * FROM customers,services WHERE customer_date = '$customer_date' ";

    $result = @mysql_query($tryshow,$conn)
            or die("cannot view error query"); 

    if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print...";
}
while($row=mysql_fetch_assoc($result)){
?>
<table width="700" border="0">
    <tr>
      <td width="100">Customer Date:</td>
      <td width="100">Last Name</td>
      <td width="100">First Name</td>
      <td width="100">Room Number</td>
      <td width="100">Date In</td>
      <td width="100">Date Out</td>
    </tr>
    <tr>
      <td><?php echo $row["customer_date"]; ?></td>
      <td><?php echo $row['lastname']; ?></td>
      <td><?php echo $row['firstname']; ?></td>
      <td><?php echo $row['room_number']; ?></td>
      <td><?php echo $row['date_in']; ?></td>
      <td><?php echo $row['date_out']; ?></td>
   </tr>
  </table>
<?php }?>
</form>
有了这个,我可以得到任何在那个日期登记的客户的报告


我需要一些建议。希望您能尽快回答我。

您需要将这两个表关联起来。根据给出的信息,customers.customer\u date to services.date\u in似乎是最有可能的候选人。这假定日期列只包含日期,而不包含日期/时间

还要注意,我在查询中没有使用select*,您也不应该使用-

SELECT c.customer_date, c.lastname, c.firstname,
       s.room_number, s.date_in, s.date_out
    FROM customers c
        INNER JOIN services s
            ON c.customer_date = s.date_in
    WHERE c.customer_date = '$customer_date' 

这两个表之间似乎没有任何共同的字段。您如何存储客户A在C日期在B房间的事实?要进行SQL联接,被联接的表必须至少有一个公共字段

同样,不要只说diecannot view error query(这对调试完全没有用处),而要尝试执行diemysql_error,这将为您提供查询失败的确切原因


同样,如果查询确实有效,那么您将为找到的每一行输出一个完整的HTML表。您应该将表的页眉和页脚数据放在fetch循环之外。

当您对数据库进行查询时,请确保您的日期格式为yyyy-mm-dd

Mysql只理解此格式的日期,因此您只能比较此格式的日期格式


您的$customer\u日期应为yyyy-mm-dd格式

另外,我会将customer\u日期更改为更有意义的日期,例如中的日期。这是一件好事,当名字是可预测的!您不需要指定它是customer,因为它已经在customer表中。

+1用于删除SELECT*和正确的联接@伦兹:对你想要的数据要具体、准确。当您指定所需的确切列时,您的RDBMS将始终能够更好地执行和扩展。感谢您的回复。我现在正在做改变。也谢谢你的编码建议。我还是个新手,但通过不断的阅读和努力,我知道我可以做得更多。谢谢again@joe我已经应用了您的查询,但仍然无法获取任何行??:@伦兹:你确定你有满足查询的数据吗?@joe是的,如果我删除where子句,它会显示行。嗯,真奇怪