【发布时间】:2012-03-06 09:08:30
【问题描述】:
编辑
我将表更改为 MyISAM,它工作正常。任何人都知道为什么 InnoDB 不起作用,以及是否有办法“启用”它?
基本上,我插入一行,能够通过 $mysqli->insert_id 引用新的 id,选择该行,将其转换为关联数组,然后返回它。在那之后,在另一个函数中,我尝试选择它并且它不存在。它也不存在于数据库中。更新根本不更新,而删除根本不删除(虽然当我说$stmt->affected_rows时,它返回> 0)。
一个表(新闻)修改没有问题,但另一个表(产品)使用相同的代码(只有它有自己的表名和字段值)不会。
函数是这样的:
function addNews($title, $message) {
$mysqli = open_mysqli();
if ($stmt = $mysqli->prepare("INSERT INTO news(title, message) VALUES (?,?)")) {
$stmt->bind_param("ss", $title, $message);
$stmt->execute();
$stmt->close();
$id = $mysqli->insert_id;
if ($stmt = $mysqli->prepare("SELECT * FROM news WHERE id=? LIMIT 1")) {
$stmt->bind_param("i", $id);
$stmt->execute();
$meta = $stmt->result_metadata();
while ($field = $meta->fetch_field()) {
$params[] = &$row[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), $params);
while ($stmt->fetch()) {
foreach($row as $key => $val) {
$c[$key] = $val;
}
$results[] = $c;
}
$stmt->close();
$mysqli->close();
if(isset($results) && $results) {
return sqlResultObj(true, $results[0]);
} else {
return sqlErrorObj('No news with id '.$id);
}
}
} else {
return sqlErrorObj('Problem adding news.');
}
}
除了字段本身之外,表之间的唯一区别是 product 是 InnoDB 存储引擎,并且有其他表将其作为外键引用,而 news 是 MyISAM,但没有。
就像我说的,这在我的本地 WAMP 服务器(运行 Apache 2.2.11、PHP 5.3.0、MySQL 5.1.36)上完美运行,但它在我的 1and1 服务器(PHP 5、MySQL 5 和我有 USA 1&1 Beginner Package Linux_WS...不确定哪个版本的 Apache 或在哪里可以找到它)。任何想法将不胜感激。
【问题讨论】:
-
可能你的mysql用户帐号没有写权限?
-
否 - 新闻表使用同一个帐户并且在同一个数据库中。我认为这不是问题。
-
您在插入新闻或产品时遇到问题?如果产品放置了您用于使用产品的代码。你的代码是你用来新闻的吧?
-
新闻有效,产品无效。代码实际上是完全相同的东西,除了查询是“INSERT INTO product(name, description, thumbnail, createdate, filedirectory, active) VALUES (?,?,?,?,?,?)”和“SELECT * FROM product WHERE id=?LIMIT 1" 问题不在于查询 - 它在我的本地主机上运行良好。我认为这与 InnoDB 和 1and1 有关?
-
您是否启用了 error_reporting 和 display_errors?您收到任何错误消息吗?
标签: php mysqli innodb prepared-statement myisam