doushi7819 2016-08-26 16:16
浏览 174
已采纳

PHP - 需要在网页中回显/打印Oracle查询结果

I've been searching for a couple of days and tried various forms of oci-fetch and not using it at all. What I'm trying to do is (code follows) get a submitted user ID (that we use in our institution) and obtain first and last name initals, add the last 4 digits of SSN and send as the reset password for the user.

I ran the first part of the sql in sql Developer, and satisfied myself that the desired outcome results.

Where I'm having a problem is taking the result of the sql and using it to update the ldap, then send in a web page. The holdup is obtaining the results of the sql via the php operations. Right now, I'm just trying to echo or print to make sure I'm getting the resultant password, but that's not happening.

Here's the code (adjusted for security):

<?php
$user = STRTOUPPER($_POST['uid']); //get input text
$conn = oci_connect("A_USER", "USER_PWORD>*", "DBSID");

if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

$query = "select UPPER(substr(a.per_lname,1,1)) || LOWER(substr(a.per_fname,1,1)) || a.per_sno from mccuser.Pers_info a where a.per_id = ('" . $user ."')";

$stid = oci_parse($conn, $query);
$success = oci_execute($stid);
//From this point, I've tried different approaches, including not even using oci_fetch.

oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC);
$newpw = $success;
echo $newpw;

?>

I'd appreciate any suggestions.

Thanks, dfonteno

  • 写回答

1条回答 默认 最新

  • doutuan8887 2016-08-30 11:20
    关注

    The PHP documentation for the OCI extension is excellent and there are plenty of examples showing how to do what you want.

    Your code is vulnerable to SQL Injection attack, so I've modified it to use binding.

    <?php
    
    $user = STRTOUPPER($_POST['uid']); //get input text
    $conn = oci_connect('A_USER', 'USER_PWORD>*', 'DBSID', 'UTF8'); // explicitly use UTF-8
    
    if (!$conn) {
        $e = oci_error();
        trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
    }
    
    // field is aliased as 'PWD' since you want an associative array as result.
    $query = 'select UPPER(substr(a.per_lname,1,1)) || LOWER(substr(a.per_fname,1,1)) || a.per_sno as pwd from mccuser.Pers_info a where a.per_id = (:usr)';
    $stid = oci_parse($conn, $query);
    // bind the user variable.
    oci_bind_by_name($stid, ':usr', $user);
    oci_execute($stid);
    $result = oci_fetch_assoc($stid);
    $newpw = $result['PWD']; // note the key is always uppercase by default.
    echo $newpw;
    

    That should do what you want. I do however agree with Mike_OBrien that this is a bad way to go. You replied:

    The worst that can happen is someone can log into a student account and see their Grades or email someone, using the student's account.

    I consider that incredibly bad. What if that account is used to email a bomb threat, or pornography? An innocent person could face criminal charges because of your lazy approach to application security.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程