2017-07-16 134 views
0

我试过寻找,但无法找到为什么我的代码不工作。PDO BindParam和BindValue不能正常工作

这是我无法工作 - 查询不返回任何内容,实际上处理停止在执行。

如果我在execute硬编码值,它的工作原理:

$CID = $_POST [ 'loginPageCID' ]; 
$LID = $_POST [ 'loginPageLID' ]; 
$PWD = $_POST [ 'loginPagePwd' ]; 

require_once('connectdb.php'); 

$sql = " 

    SELECT u.*, c.cname, c.cstatus 
    FROM t_users u, t_customers c 
    WHERE u.cid = ? 
    AND c.cid = u.cid 
    AND u.login_id = ? 
    AND u.pwhash = ? 

    "; 

$stmt = $conn->prepare ($sql); 

$stmt->execute (array(1, 'su1', 'hello')); 

$row = $stmt->fetchAll(); 

随着它上面的工作,但如果我尝试使用变量的执行似乎静默失败:

$CID = $_POST [ 'loginPageCID' ]; 
$LID = $_POST [ 'loginPageLID' ]; 
$PWD = $_POST [ 'loginPagePwd' ]; 

require_once('connectdb.php'); 

$sql = " 

    SELECT u.*, c.cname, c.cstatus 
    FROM ids_users u, ids_customers c 
    WHERE u.cid = ? 
    AND c.cid = u.cid 
    AND u.login_id = ? 
    AND u.pwhash = ? 

    "; 

$stmt = $conn->prepare ($sql); 

$stmt->bindParam(1, $CID, PDO::PARAM_INT); 
$stmt->bindParam(2, $LID, PDO::PARAM_STR); 
$stmt->bindParam(3, $PWD, PDO::PARAM_STR); 

$stmt->execute(); // fails here 

$row = $stmt->fetchAll(); 

编辑:

connectdb.php如下:

$conn; 

try { 
    $conn = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password); 
    // set the PDO error mode to exception 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

    } 
catch(PDOException $e) 
    { 
    echo "Error: " . $e->getMessage(); 
    } 
+0

你有[启用例外](http://php.net/manual/en/pdo.error-handling.php)?如果是这样,什么都不应该默默地失败。 – tadman

+0

@tadman - 添加了用于创建问题连接的代码。 – TenG

+0

仔细查看您回来的结果对象。仔细检查您的错误日志中是否有任何通知。 – tadman

回答

0

我有同样的问题,我发现这个职位的解决方案:在第二个答案,当用户建议避免使用bindParam或bindValue功能PDO早已较短的方式

PDO - bindParam not working

将参数分配给SQL查询。