在两个表之间搜索获取mysql_fetch_array()警告

时间:2013-12-05 11:00:37

标签: php mysql

我创建了两个表

customers  | customer_details

This is from "customers" table This is from "customer_details"

我在两个tabel

之间创建两个搜索字段和给定值搜索

这是代码

<form>
    <input  type="text" id="example1" name="example1" value="<?php echo date('d/m/Y'); ?>">
    <input  type="text" id="example2" name="example2" value="<?php echo date('d/m/Y'); ?>">
    <button class="btn" name="submit" type="submit"> Search </button>
</form>
<table border="1">
    <thead>
      <tr>
        <th> SL</th>
        <th> U_ID</th>
        <th> NAME </th>
        <th> BOX_NO </th>
        <th> ACC_NO </th>
        <th> BOX CHRG </th>
        <th> INSTL CHRS </th>
        <th> OTHER CHRG </th>
        <th> PKG VAL </th>
        <th> TOTAL AMT </th>
        <th> RECD BY </th>
        <th> Date </th>
        </tr>
    </thead>
    <tbody>
<?php
if(isset($_GET['submit']))
{
    $db_host="localhost";
    $db_username="root";
    $db_password="";
    $db_name="administrator";
    $db_tb_name="customer_details";
    $db_tb1_name="customers";
    $db_tb_atr_name="u_id";
    $db_tb_btr_name="name";
    $db_tb_ctr_name="box_no";
    $db_tb_dtr_name="acc_no";
    $db_tb_etr_name="box_chrs";
    $db_tb_ftr_name="instl_chrs";
    $db_tb_gtr_name="other_val";
    $db_tb_htr_name="pkg_val";
    $db_tb_itr_name="total";
    $db_tb_jtr_name="rec_by";
    $db_tb_ktr_name="reg_date";
    $db_tb_ltr_name="id";
    $db_tb_mtr_name="date";

    mysql_connect("$db_host","$db_username","$db_password");
    mysql_select_db("$db_name");

    $s_name=mysql_real_escape_string($_GET['example1']);
    $m_name=mysql_real_escape_string($_GET['example2']);

    $query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE 
    $db_tb_mtr_name BETWEEN '".$s_name."' AND '".$m_name."'

    UNION

    SELECT * FROM $db_tb1_name WHERE 
    $db_tb_ktr_name BETWEEN '".$s_name."' AND '".$m_name."' 
    ORDER BY $db_tb_dtr_name, $db_tb_ltr_name DESC");

    $i = 1;
    while($data_fetch=mysql_fetch_array($query_for_result))
    {
?>
    <tr>
        <td><?php echo $i; ?></td>
        <td><?php echo substr($data_fetch[$db_tb_atr_name], 0,160); ?></td>
        <td><?php echo substr($data_fetch[$db_tb_btr_name], 0,160); ?></td>
        <td><?php echo substr($data_fetch[$db_tb_ctr_name], 0,160); ?></td>
        <td><?php echo substr($data_fetch[$db_tb_dtr_name], 0,160); ?></td>
        <td><?php echo substr($data_fetch[$db_tb_etr_name], 0,160); ?></td>
        <td><?php echo substr($data_fetch[$db_tb_ftr_name], 0,160); ?></td>
        <td><?php echo substr($data_fetch[$db_tb_gtr_name], 0,160); ?></td>
        <td><?php echo substr($data_fetch[$db_tb_htr_name], 0,160); ?></td>
        <td><?php echo substr($data_fetch[$db_tb_itr_name], 0,160); ?></td>
        <td><?php echo substr($data_fetch[$db_tb_jtr_name], 0,160); ?></td>
        <td><?php echo substr($data_fetch[$db_tb_ktr_name], 0,160); ?></td>
      </tr>
<?php
    $i++;
    } 
    mysql_close();
}
?>
    </tbody>
</table>

在输出中显示错误

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\projects\MainWork\administrator\report.php on line 63

我的代码有什么问题?

如何解决这个问题?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我可以看到你的问题是查询错误。

您正在查询中应用UNION。这是UNION的规则,在应用UNION时,两个查询中应该有相同数量的字段。

在你的情况下它没有发生。 请尝试重新设计您的查询。

希望这有助于你。 :)