为什么oci_fetch_array($ stid)不返回oracle数据库表的第一行

时间:2016-01-09 16:23:50

标签: php oracle

我正在尝试创建一个从oracle数据库返回一些数据的搜索表单,除了它跳过表的第一行之外它工作正常。 我使用了以下代码:

enter code here
if(isset($_POST['search']) && !empty($_POST["search_box"])){
$name=$_POST['search_box'];
$sql='SELECT deejays.name,available_dates.data, available_dates.venue,available_dates.location FROM deejays,available_dates';
$sql .=" WHERE deejays.name ='{$name}'";
$sql .=' AND pk_id=fk_id';
$verify="SELECT  deejays.name FROM deejays WHERE deejays.name ='{$name}'";
$stid = oci_parse($conn,$sql );
oci_execute($stid);
if (oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)){
echo "<table width=\"100%\" align=\"center\" cellpadding=\"5\" cellspacing=\"5\">
<tr class=\"source\">
    <td colspan=3 align=\"center\"><h1>The facebook tour dates for ".$name." are:</h1></td>
</tr>
<tr align=\"center\" class=\"source1\">

    <td><strong>DATA</strong></td>
    <td><strong>VENUE</strong></td>
    <td><strong>LOCATION</strong></td>
</tr>";?>
<?php while($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)){ ?>
<tr align="center" class="rows">

    <td ><?php echo $row['DATA'];?></td>
    <td ><?php echo $row['VENUE'];?></td>
    <td ><?php echo $row['LOCATION'];?></td>
</tr>
<?php } ?>
</table>
<?php } else {
            $stid = oci_parse($conn,$verify );
            oci_execute($stid);
            if (oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS))
             { echo "This artist hasn't records on facebook/bandsintown<br>  
enter code here

预期结果是: enter image description here

,真正的结果是: enter image description here

第二张图片显示第一行被跳过,我该如何解决?先谢谢

1 个答案:

答案 0 :(得分:1)

正如我评论的那样,oci_fetch_array()将返回一个包含查询的下一个结果集行的数组。这意味着每次调用它时,光标将移动到下一行。我有两个建议。

<强>第一

oci_execute()之后将所有结果加载到数组中。如果数组有任何行,则对其进行计数。

oci_execute($stid);
$result = array();

while($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS))
{
    $array[] = $row;
}
//check if result is not empty
if (count($result)) {
   //the table part here.
}

当你想在html表中显示它时,使用foreach循环来迭代数组并构建<tr>

<?php foreach($result as $row) { ?>

    <tr align="center" class="rows">
        <t ><?php echo $row['DATA'];?></td>
        <td><?php echo $row['VENUE'];?></td>
        <td><?php echo $row['LOCATION'];?></td>
    </tr>
<?php } ?>

<强> SECOND

您可以使用oci_fetch_all()将查询中的所有行加载到数组中。

oci_execute($stid);
//$result will have all the result of the current query
$count = oci_fetch_all($stid, $result);

if ($count) {
//table part here
}

构建<tr>将与第一个相同。

另一种方法是使用缓冲查询。但这取决于很多因素。