无法使用 PHP ODBC 连接写入 MS SQL 数据库 [英] Cannot write to MS SQL database using PHP ODBC connection
问题描述
问题:我可以使用 odbc_exec 在 PHP 中成功使用选择查询,但是,我无法执行 INSERT INTO 或 UPDATE
查询.
Problem: I can successfully use a select query in PHP using odbc_exec, however, I can not execute an INSERT INTO or UPDATE
query.
资源:Windows Server 2003:IIS 6、SQL Server 2005.
Resources: Windows Server 2003: IIS 6, SQL Server 2005.
这是我的代码:
<?php
require('../_DSN/DSN_M.php');
$cnnOra = odbc_connect($strarrDSN['dsn'], $strarrDSN['username'], $strarrDSN['pswd']);
$res_slct = odbc_exec($cnnOra, " select * FROM person;") or die (odbc_errormsg());
odbc_result_all($res_sldr);
$sqlupd = "UPDATE person SET person_ame='Steve Woz' WHERE pk_person_ID = '32';"
$res_upd = odbc_exec($cnnora, $sqlupd) or die(odbc_errormsg());
$res_cmt = odbc_commit($cnnora);
var_dump($res_upd);
var_dump($res_slct);
var_dump($res_cmt);
// close the connection
odbc_close($cnnOra);
?>
显示 SELECT 查询的输出,页面上没有 errormsg 或 var_dump 结果.
The output of the SELECT query displays and there is no errormsg or var_dump results on the page.
这是 SELECT 查询的输出:
Here is the output from the SELECT query:
pk_person_ID fk_SSN fk_qual1_ID fk_qual2_ID person_Name
31 999999999 1 1 bobby buchier
32 999999999 2 2 Frank Gifford
我在这里遗漏了什么吗?
Am I missing something here?
推荐答案
我找到了适合我的解决方案.当我的原始代码没有时,我不确定为什么这有效.
I found the solution for me. I am not sure why this works when my original code didn't.
我需要先使用 odbc_prepare,然后再使用 odbc_exec.这是对我有用的代码:
I needed to use an odbc_prepare and then an odbc_exec. Here is the code that worked for me:
$q = "UPDATE person SET person_name = 'Steve Woz' WHERE pk_person_ID = '32'";
$res = odbc_prepare ($cnnOra, $q) or die (odbc_errormsg());
$exc = odbc_execute($res) or die (odbc_errormsg());
这也适用于 INSERT INTO
查询.非常感谢@andrewsi!!
This also works on a INSERT INTO
query. Much thanks to @andrewsi!!
这篇关于无法使用 PHP ODBC 连接写入 MS SQL 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!