使用ODBC和PHP检索存储过程的数据

7
我正在尝试使用ODBC和PHP检索输出参数的值。我在网上搜索了很多,但都没有结果。有很多关于PDO和mysqli的解决方法,只找到了这个ODBC的。我被参数和操作所困扰。
我能够连接到数据库,但总是出现错误。我无法解决它们。

[Sybase][ODBC Driver] 无效的字符串或缓冲区长度

有什么建议吗?
<?php

$conn=odbc_connect("dsn", " ", " ");

if (!$conn)
{
exit("Connection Failed: " . $conn);
}

$sql=odbc_prepare("CALL ndTblUser (@varUserId,@varUserPwd)"); 
$stmt=odbc_execute($sql, "SELECT @varUserId as UserId, @varUserPwd as UserPwd");

$rs=odbc_exec($conn,$stmt);

if (!$rs)
{
exit("Error : " . odbc_errormsg());
}
echo "<table><tr>";
echo "<th>Id</th>";
echo "<th>Password</th></tr>";

while (odbc_fetch_row($rs))
{
$UserId=odbc_result_all($rs,"UserId");
$UserPwd=odbc_result_all($rs,"UserPwd");
echo "<tr><td>$UserId</td>";
echo "<td>$UserPwd</td></tr>";
}

odbc_close($conn);
?>
2个回答

1

现在我只是猜测,但如果实际查询中的$UserId$UserPwd应该是列名,请尝试从它们中删除$符号。稍后在代码中,您尝试将它们作为没有$符号的列名获取,因此我不确定这是否是导致错误的原因。直到$UserId=odbc_result($rs,"UserId");之前,它们才成为PHP变量。


嘿,我在想。也许我误解了你的回答。你能再解释一下吗?@user2620460 - Iman Yasmin

1
我终于找到了解决方案。
当涉及到检索数据时,我对存储过程有很多误解,即使是最简单的存储过程也是如此。
调用参数的数据并不是被检索出来的,而是用于调用其他结果参数。例如,在这里,UserIdUserPwd被调用以显示UserNameLoginStatus
以下是正确的代码,用于检索基于变量参数UserIdUserPwd的结果参数UserNameLoginStatus的数据。
<?php

$conn=odbc_connect("dsn", " ", " ");

if (!$conn)
{
  exit("Connection Failed : " . $conn);
}

$stmt=odbc_exec($conn,"CALL ndTblUser (".$_POST['UserId'].",'".$_POST['UserPwd']."')");

if (!$stmt)
{
  "Error : " . odbc_errormsg();
}

if (odbc_fetch_row($stmt))
{
  $UserName=odbc_result($stmt,"UserName");
  $LoginStatus=odbc_result($stmt,"LoginStatus");
}


if($LoginStatus==1)
{
  echo odbc_result($stmt,"UserName");
  echo odbc_result($stmt,"LoginStatus");
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接