[英]Cannot write to MS SQL database using PHP ODBC connection
Problem: I can successfully use a select query in PHP using odbc_exec, however, I can not execute an INSERT INTO or UPDATE
query. 问题:我可以使用odbc_exec在PHP中成功使用选择查询,但是,我无法执行INSERT INTO or UPDATE
查询。
Resources: Windows Server 2003: IIS 6, SQL Server 2005. 资源:Windows Server 2003:IIS 6,SQL Server 2005。
Here is my code: 这是我的代码:
<?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);
?>
The output of the SELECT query displays and there is no errormsg or var_dump results on the page. 将显示SELECT查询的输出,并且该页面上没有errormsg或var_dump结果。
Here is the output from the SELECT query: 这是SELECT查询的输出:
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. 我不确定为什么当我的原始代码不行时,这行得通。
I needed to use an odbc_prepare and then an odbc_exec. 我需要先使用odbc_prepare,再使用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());
This also works on a INSERT INTO
query. 这也适用于INSERT INTO
查询。 Much thanks to @andrewsi!! 非常感谢@andrewsi!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.