2014-09-12 139 views
1

我有以下的php/pdo,它調用一個mysql程序返回一個文件名,但儘管準備好的語句是正確的,並從剪切和粘貼的數據庫查詢返回以下內容:php pdo查詢無法返回結果,儘管準備好的語句正確

DB查詢/結果

>  call sp_CDRbyCustomer('Wind', 'R2X', DATE(DATE_SUB(NOW(), INTERVAL 10 WEEK)), DATE(NOW())); 
>  +------------------------------------------------------+ 
>  | Exported filename         | 
>  +------------------------------------------------------+ 
>  | '/tmp/CDR_for_Wind_20140704-20140912_1410516460.csv' | 
>  +------------------------------------------------------+ 
>  1 row in set (0.02 sec) 

的PHP/PDO

應返回我們的FIL ename是:

include('db.ini'); 

define('DEBUG', true); 
if (DEBUG) { 
     openlog("$iam", LOG_PID | LOG_ODELAY,LOG_LOCAL4); 
     syslog(LOG_INFO, "START OF DEBUG LOG."); 
} 

/***** connect to database using db.ini for credentials *****/ 
try { 
$dbConn = new PDO('mysql:host='.$host.';dbname='.$db, $dbUser, $dbPass, array (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)); 

} catch (PDOException $e) { 
    print "Error!: " . $e->getMessage() . "<br/>"; 
    die(); 
} 

$structure_name = "'Wind'"; 
$cug_uid = "'R2X'"; 
$num_weeks = 10; 

/**** Query - export file name ****/ 
$sqlQuery4filename = (" 
call sp_CDRbyCustomer($structure_name, $cug_uid, DATE(DATE_SUB(NOW(), INTERVAL $num_weeks WEEK)), DATE(NOW())) 
"); 
$sqlQuery4filename = preg_replace("/\r|\n/", "", $sqlQuery4filename); 

    debug("01: sqlQueryfile string is '" . $sqlQuery4filename . "'"); 
    $stmnt = $dbConn->prepare($sqlQuery4filename); 
    debug($stmnt); 
    $stmnt->execute(); 
    $stmnt->closeCursor(); 
    $filename = $stmnt->fetch(PDO::FETCH_ASSOC); 
    $filename = $filename['Exported filename']; 
    debug("01: filename string is '" . $filename . "'"); 

function debug($debug) { 
if(!DEBUG) return; 

print ' 
<div class="debug"> 
'; 
print_r($debug); 
print ' 
</div><!-- end of debug --> 
'; 
} 

但是,我只收到一個空字符串返回。

我已經有這個工作,但在顛覆它後,我結束了錯誤的版本,不能再得到它的工作,任何人給我任何線索,我做錯了什麼。

+0

要使結果這個更清楚,上面的sql過程調用是'cut'和'paste'從'debug($ stmnt);'的結果。然後我們運行'$ stmnt-> execute();'和'$ stmnt-> closeCursor();'但是'$ stmnt-> fetch(PDO :: FETCH_ASSOC);'返回空。 – 2014-09-12 11:19:39

回答

0

這麼多的調試和許多不同的編碼嘗試後,我終於取回德所需的結果。

我嘗試過多次嘗試使用binParam,但返回的結果總是空字符串。

我已經更改爲mysql程序來輸出所需的文件名,所以mysql命令SELECT @filename將提供所需的結果。

我則檢索與該值以下:

/**** Query - export file name ****/ 
    $sql = 'call sp_CDRbyCustomer(\'' . $structure_name . '\', \'' . $cug_uid . '\', DATE(DATE_SUB(NOW(), INTERVAL ' . $num_weeks . ' WEEK)), DATE(NOW()), @filename)'; 

    $stmnt = $dbConn->prepare($sql); 
    debug($stmnt); 
    $stmnt->execute(); 
    $stmnt->closeCursor(); 
    $filename_result = $dbConn->query("select @filename")->fetch(); 
    $filename = $filename_result['@filename'];   

我不知道這是否是做到這一點的最好方式,但它確實能產生我渴望:-)

0

您需要正確調試此:

  1. 看到確切的SQL命令你執行(我無法從你的問題告訴)。
  2. 測試針對數據庫的命令,例如使用PHPmyAdmin。
  3. 顯示fetch()的結果,然後以數組的形式訪問它。

如果您從開始到結束遵循PHP執行試用版,您應該能夠確定它出錯的位置。

0

您需要綁定輸出參數。見例#4中的文檔http://php.net/manual/en/pdo.prepared-statements.php

<?php 
$stmt = $dbh->prepare("CALL sp_returns_string(?)"); 
$stmt->bindParam(1, $return_value, PDO::PARAM_STR, 4000); 

// call the stored procedure 
$stmt->execute(); 

print "procedure returned $return_value\n"; 
?>