Cannot write to MS SQL database using PHP ODBC connection

北战南征 提交于 2019-12-25 03:30:48

问题


Problem: I can successfully use a select query in PHP using odbc_exec, however, I can not execute an INSERT INTO or UPDATE query.

Resources: 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.

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?


回答1:


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. 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. Much thanks to @andrewsi!!



来源:https://stackoverflow.com/questions/16020582/cannot-write-to-ms-sql-database-using-php-odbc-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!