douyu1656 2016-08-12 12:43
浏览 29

如何在没有行返回时停止执行并回显用户消息

I have two php pages first one index.php as below

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Site Title</title>
</head>

<body align="right">

<h4 align="right">Site Title<br>Subtitle</h4>

<form action="acc_data.php" method="post">
<input type="text" name="ACCNUM"> Account Number</br>
<input type="submit" value="Search">

</form>


</body>

</html>

and acc_data.php as below

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Site Title</title>
</head>

<?php

$conn = oci_connect('admin', 'admin', 'localhost/JDT','AL32UTF8');
if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

// Prepare the statement
$sqlstr = 'SELECT CUSTOMER_ID,CUST_NAME,PHONE1 FROM customers where CUSTOMER_ID=:ACCNUM';

$stid = oci_parse($conn,$sqlstr);
$ACCNUM = $_POST['ACCNUM'];
oci_bind_by_name($stid, ':ACCNUM', $ACCNUM);
oci_execute($stid);


// Fetch the results of the query
print "<table style=width:75% align=center border=1>
        <tr><th>Account Number</th>
        <th>Name</th>
        <th>Phone</th></tr>";

        while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) 
        {
            print "<tr>";
            foreach ($row as $item) 
            {
                print "<td align=center>" . ($item !== null ? htmlentities($item, ENT_QUOTES) : "&nbsp;") . "</td>";
            }
            print "</tr>
";
        }
print "</table>
";

oci_free_statement($stid);
oci_close($conn);

?>

</body>
</html>

the user in first page will enter its data and pass the value to another php page to be processed at server and return back the result

question 1: is there any security improvement should I do?

question 2: in case of no rows return how to stop execution and echo user a message?

  • 写回答

1条回答 默认 最新

  • douju4594 2016-08-12 13:25
    关注

    The PHP Oracle API doesn't provide a function that just returns the number of rows int he result set. The only way I can see to tell how many rows were selected is to fetch all the rows before printing anything. So use oci_fetch_all to fetch all the rows at once.

    $count = oci_fetch_all($stid, $rows, null, null, OCI_FETCHSTATEMENT_BY_ROW + OCI_ASSOC);
    if ($count == 0) {
        Print "No results found";
    } else {
        print "<table style=width:75% align=center border=1>
                <tr><th>Account Number</th>
                <th>Name</th>
                <th>Phone</th></tr>";
    
        foreach ($rows as $row) {
            ...
        }
        print "</table>
    ";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)