简体   繁体   English

我正在使用PHP ODBC写入Access数据库,但不会写入数据库

[英]I am using PHP ODBC to write to an access database, but it won't write to the database

I'm trying to use PHP/ODBC to connect to an access file. 我正在尝试使用PHP / ODBC连接到访问文件。 The problem is that I can read from the database but I can't write to it using the below: 问题是我可以从数据库中读取内容,但不能使用以下内容写入数据库:

$conn = odbc_connect('SKW-DB','','');
if (!$conn)
   {
    exit ("ODBC Connection Failed ". $conn);
   }
$stmt = "INSERT INTO PRODUCT (ProductCode, ProductName) VALUES ('TestCode', 'TestEntry')";
$result = odbc_exec ($conn,$stmt);
echo "Result1: ";
echo $result;

$result returns nothing. $ result不返回任何内容。 Again, I am able to read from the database, connectivity isn't an issue. 同样,我能够从数据库中读取数据,连接性不是问题。 I just can't write to it. 我只是写不出来。

That's because you're simply ASSUMING the query can never fail. 那是因为您只是假设查询永远不会失败。 It did fail, and returned a boolean false. 它确实失败了,并返回一个布尔值false。 echo false literally prints out nothing. echo false从字面上不打印任何内容。

Try this instead: 尝试以下方法:

$result = odbc_exec($conn, $stmt);
if ($result === false ) {
   die(odbc_errormsg($conn));
}

And what you get back from odbc_exec() cannot be echoed out anyways. 无论如何,您从odbc_exec()返回的内容都无法被回显。 On success, it returns a statement handle, which is NOT something you can simply print out. 成功后,它将返回一个语句句柄,您不能简单地将其打印出来。

Sounds like you need a little more debugging code. 听起来您需要更多的调试代码。

First, try var_dump ing the $result instead of echo ing it. 首先,尝试var_dump输入$result而不是echo它。

var_dump($result);

There's certain PHP variable types that echo can't/won't display. 还有,某些PHP变量类型echo不能/不会显示。

Next -- chances are your query's causing an error of some sort, so try using the odbc error reporting functions after making your query 下一步-可能是您的查询导致某种类型的错误,因此请在查询后尝试使用odbc错误报告功能

$result = odbc_exec ($conn,$stmt);
echo "Result1: ";
var_dump( $result );
if($result)
{
    var_dump( odbc_error($conn)    );    
    var_dump( odbc_errormsg($conn) );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM