1
 <?php

$batchcourseid = $_POST['batchcourseid'];
$coursestatus = $_POST['coursestatus'];
$batchname = $_POST['batchname'];



$conn=mysql_connect("localhost","root","qwerty");
mysql_select_db("MIS");
$sql=("SELECT * from batch where CourseID = '$batchcourseid' AND BatchName LIKE '%$batchname%'");

$results = mysql_query($sql, $conn);

?>
<table width="1070" height="54" border="1">
  <tr>
    <td height="23">BatchID</td>
    <td>CourseID</td>
    <td>Batch Name</td>
    <td>Number Of Students</td>
    <td>Start Date</td>
    <td>End Date</td>
    </tr>
  <?php  while($row = mysql_fetch_array($results)) ?>
  <?php { ?>
  <tr>
    <td height="23"><?php echo $row['BatchID'] ?>&nbsp;</td>
    <td><?php echo $row['CourseID']; ?>&nbsp;</td>
    <td><?php echo $row['BatchName']; ?>&nbsp;</td>
    <td><?php echo $row['NumStud']; ?>&nbsp;</td>
    <td><?php echo $row['StartDate']; ?>&nbsp;</td>
    <td><?php echo $row['EndDate']; ?>&nbsp;</td>
    </tr>
    <?php } ?>

这段代码应该在表格中给出一行。但它只是出现的空行。我做错了什么?所有的列和文本也存在

4

6 回答 6

3

%必须是LIKE字符串的一部分,所以它应该在 之间',而不是之前/之后:

BatchName LIKE '%$batchname%'"
于 2013-08-17T11:20:54.880 回答
2

like应该写成如下:

LIKE '%$batchname%'

% 应该在引号内。

于 2013-08-17T11:25:33.477 回答
2

在您的选择查询中,请检查like operator %

$sql=("SELECT * from batch where CourseID = ".$batchcourseid." AND BatchName LIKE '%".$batchname."%' ");
于 2013-08-17T11:41:09.393 回答
1

%里面的文字('%".$batchname."%'):

$sql="SELECT * from batch where CourseID = '".$batchcourseid"' ".
         "AND BatchName LIKE '%".$batchname."%'";

根据您的环境,您应该了解SQL 注入。所以你可能宁愿使用绑定变量

$sql="SELECT * from batch where CourseID = ? AND BatchName LIKE ?";

将第一个参数设置为$batchname ,将第二个参数设置为"%".$batchname."%"

于 2013-08-17T11:21:35.150 回答
1

在文本中使用 % 试试这个:-

$sql=("SELECT * from batch where CourseID = '$batchcourseid' 
AND BatchName LIKE '%$batchname%'");

代替

$sql=("SELECT * from batch where CourseID = '$batchcourseid' 
AND BatchName LIKE %'$batchname'%");

问题更新后编辑:-

 $sql=("SELECT * from batch where CourseID = ".$batchcourseid." AND BatchName LIKE '%".$batchname."%'
于 2013-08-17T11:21:58.243 回答
0
$sql=("SELECT * from batch where CourseID = '$batchcourseid' AND BatchName LIKE %'$batchname'%");

将其更改为

$sql="SELECT * from batch where CourseID = '$batchcourseid' AND BatchName LIKE '%$batchname%'";
于 2013-08-17T11:23:12.440 回答