使用 ODBC 和 PHP 检索存储过程的数据
Retrieving Data of Stored Procedure Using ODBC and PHP
我正在尝试使用 ODBC 和 PHP 检索 out 参数的值。我一直在网上搜索但无济于事。 PDO 和 mysqli 有很多解析,我只找到了 odbc 的 this。我有点迷失了参数和涉及的操作。
我可以连接到数据库,但总是弹出错误。我想不通。
[Sybase][ODBC Driver] Invalid string or buffer length
有什么建议吗?
<?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);
?>
现在我只是猜测,但如果实际查询中的 $UserId
和 $UserPwd
应该是列,请尝试从中删除 $-sign。稍后在代码中,您试图将它们作为不带 $ 符号的列名获取,因此我不确定这是否是导致错误的原因。在 $UserId=odbc_result($rs,"UserId");
.
之前,它们不是 PHP 变量
我终于找到了解决办法。
在检索此问题的数据时,我对存储过程有很多误解。哪怕是最简单的。
不获取调用参数的数据,调用参数用于调用其他结果参数。例如这里调用UserId
和UserPwd
来显示UserName
和LoginStatus
.
这是检索结果参数UserName
和LoginStatus
的数据的正确代码,基于UserId
和UserPwd
的可变参数。
<?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");
}
我正在尝试使用 ODBC 和 PHP 检索 out 参数的值。我一直在网上搜索但无济于事。 PDO 和 mysqli 有很多解析,我只找到了 odbc 的 this。我有点迷失了参数和涉及的操作。
我可以连接到数据库,但总是弹出错误。我想不通。
[Sybase][ODBC Driver] Invalid string or buffer length
有什么建议吗?
<?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);
?>
现在我只是猜测,但如果实际查询中的 $UserId
和 $UserPwd
应该是列,请尝试从中删除 $-sign。稍后在代码中,您试图将它们作为不带 $ 符号的列名获取,因此我不确定这是否是导致错误的原因。在 $UserId=odbc_result($rs,"UserId");
.
我终于找到了解决办法。
在检索此问题的数据时,我对存储过程有很多误解。哪怕是最简单的。
不获取调用参数的数据,调用参数用于调用其他结果参数。例如这里调用UserId
和UserPwd
来显示UserName
和LoginStatus
.
这是检索结果参数UserName
和LoginStatus
的数据的正确代码,基于UserId
和UserPwd
的可变参数。
<?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");
}