用PHP将字符串放入MySQL数据库

时间:2012-03-15 15:59:51

标签: php mysql

我有一个MySQL数据库设置,每行我有28个值(字母,字符串大小和字符串本身)。除字符串本身之外的每个值都是一个整数,它们工作正常。但是当我尝试将字符串插入表中时,我最终只得到表中的五行而不是1000行。即使这样,当我查看这五个表时,我最终得到的是“值”列中的整数而不是串!在将字符串放入数据库时​​,我是否遗漏了什么?

mysql_query("CREATE TABLE allwords
(
valued varchar(20),
length int,
a int,
b int,
c int,
d int,
e int,
f int,
g int,
h int,
i int,
j int,
k int,
l int,
m int,
n int,
o int,
p int,
q int,
r int,
s int,
t int,
u int,
v int,
w int,
x int,
y int,
z int
)");   
mysql_query("INSERT INTO allwords (valued, length, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z)
VALUES ($letter, $countnow, $a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $q, $r, $s, $t, $u, $v, $w, $x, $y, $z)"); 

1 个答案:

答案 0 :(得分:2)

对于任何字符串值,您应该使用单引号封装变量并将其转义。

VALUES ('$letter', $countnow, $a, $b ... )

更好的是,使用参数化查询来确保mysql知道类型以及提供针对注入攻击的保护措施。一种方法是使用PDO

至于为什么行数比预期少,请尝试将or die(mysql_error())附加到查询执行语句的末尾;任何正在发生的错误都会被偷偷带走。

相关问题