Where子句的空结果

时间:2012-05-11 16:25:06

标签: mysql php

我根据搜索表单显示“访问”表的结果有问题!

这是代码!

                <?php

                $date1 = $_POST['day'] . "-" . $_POST['month'] . "-" . $_POST['year'];
                $date2 = $_POST['day1'] . "-" . $_POST['month1'] . "-" . $_POST['year1'];
                $product=$_POST['product'];
                $region=$_POST['region'];
                $speciality=$_POST['speciality'];
                $type=$_POST['visit_type'];

$query="SELECT id, name, seller_1_name, seller_2_name FROM visits Where (speciality ='$speciality') AND (type ='$type') AND (product ='$product') AND (date BETWEEN '$date1' AND '$date2')";
$num=mysql_numrows($result);
$row = mysql_fetch_array($result);

?>

<h3> Showing results where Product is <?php echo $product; ?>, Speciality is <?php echo $speciality ?>, Region is <?php echo $region ?> and Type is <?php echo $type ?>.</h3>
        <table class="auto-style4" style="width: 100%" border="1"><tr>
        <td style="height: 18px">ID</td>
        <td style="height: 18px">Name</td>
        <td style="height: 18px">seller one name</td>
        <td style="height: 18px">seller 2 name</td>   
        </tr>
        <tr>
        <?php
$i=0;
while ($i < $num) {


$f1=mysql_result($result,$i,"id");
$f2=mysql_result($result,$i,"name");
$f4=mysql_result($result,$i,"seller_1_name");
$f5=mysql_result($result,$i,"seller_2_name");

?>        
        <td><?php echo $f1; ?> </td>
        <td><?php echo $f2; ?> </td>
        <td><?php echo $f4; ?></td>
        <td><?php echo $f5; ?></td>
         </tr>

<?php
$i++;
}
?>  

它显示输入了正确变量的标题,但表格为空,错误代码为:

警告:mysql_numrows():提供的参数不是第175行/home/ebarea/public_html/.../.../results_submitt.php中的有效MySQL结果资源

警告:mysql_fetch_array():提供的参数不是第176行/home/ebarea/public_html/.../.../results_submitt.php中有效的MySQL结果资源

什么是错的&gt;!

4 个答案:

答案 0 :(得分:2)

你从未执行过mysql_query。也许有些东西:

$query="SELECT id, name, seller_1_name, seller_2_name FROM visits Where (speciality ='$speciality') AND (type ='$type') AND (product ='$product') AND (date BETWEEN '$date1' AND '$date2')";
$result=mysql_query($query); ## This line is new.
$num=mysql_numrows($result);
$row = mysql_fetch_array($result);

要修复日期问题,您可能需要明确告诉MySQL有关日期的信息。例如:

$query="SELECT 
            id, name, seller_1_name, seller_2_name
        FROM visits 
        WHERE
            (speciality ='$speciality') AND
            (type ='$type') AND
            (product ='$product') AND
            (date BETWEEN DATE('$date1') AND DATE('$date2'))";

正如其他人所指出的那样,用于填充变量的代码很容易受到SQL注入攻击,并且应该真正更新。

答案 1 :(得分:0)

没有

$result = mysql_query($query);

$num=mysql_numrows($result);
$row = mysql_fetch_array($result);

答案 2 :(得分:0)

你忘记在以前运行mysql_query:

$result = mysql_query($query);
$num = mysql_numrows($result);

答案 3 :(得分:0)

这可以帮助你处理多行,idd,别忘了mysql_query();解析用户输入也是明智的,现在你完全容易受到sql注入的攻击。<​​/ p>

$query = mysql_query($sql, $conn);
$total = mysql_num_rows();
while ( $row = mysql_fetch_object() )
{
   $data[] = $row;
}
foreach( $data as $r )
{
  echo "<td>".$r->id."</td>";
  echo "<td>".$r->name."</td>";
  echo "<td>".$r->seller_1_name."</td>";
  echo "<td>".$r->seller_2_name."</td>";
}