我正在尝试学习mysql,但在更新/添加数据到表时遇到了一些问题
这是我的代码,在运行此页面后,当我转到phpmyadmin查看是否显示新数据时,我在那里看不到它。
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(!$conn) { die("Could not connect"); }
$dbname = "test";
mysql_select_db($dbname, $conn);
mysql_query("INSERT INTO 'test'.'table1'
('A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8')
VALUES
('test1', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7', 'test8')");
mysql_close($conn);
?>有人能告诉我这是怎么回事吗?
发布于 2010-08-18 11:54:17
问题在于字段名称两边的引号。MySQL不允许使用单引号的字段名称。它们要么必须是空的(A1),要么是在反引号内(A1),因此您的查询应该重写为:
INSERT INTO table1 (A1, A2, A3, A4, A5, A6, A7, A8)
VALUES ('test1', 'test2', 'test3', 'test4', etc.....);发布于 2010-08-18 08:42:47
一定要做错误检查,看看mysql连接是否也有问题:
if(!mysql_select_db($dbname, $conn)) {
echo mysql_error($conn);
}else {
if(!mysql_query("INSERT INTO 'test'.'table1' ('A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8') VALUES ('test1', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7', 'test8')")) {
echo mysql_error($conn);
}
}发布于 2010-08-18 08:41:46
首先,确保数据库被选中:
mysql_select_db($dbname, $conn) or die('Could not select the database');您的查询中可能存在一些错误,这是未添加数据的原因,请尝试在此mysql_query函数后添加or die(mysql_error()),以查看可能的错误:
mysql_query("your query....") or die(mysql_error()); 也不需要指定数据库名称:
'test'.'table1'只需在查询中使用table1即可。
打开错误报告也很有用:
ini_set('display_errors', true);
error_reporting(E_ALL);https://stackoverflow.com/questions/3508067
复制相似问题