Reputation: 1177
I have a MySQL database set up, and for each row I have 28 values (the alphabet, string size, and the string itself). Each value except the string itself is an integer, and they work fine. But when I try to insert the string into a table I end up only getting five rows in the table instead of 1000. And even then when I look at those five tables I end up getting an integer in the "valued" column instead of a string! Am I missing something when it comes to putting strings into databases?
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)");
Upvotes: 0
Views: 230
Reputation: 36512
For any string values you should encapsulate the variable with single quotes and escape it.
VALUES ('$letter', $countnow, $a, $b ... )
Better yet, use parameterized queries to ensure that mysql knows the type as well as offer a measure of protection against injection attacks. One way to do so is using PDO.
As to why you are getting fewer rows than expected, try appending or die(mysql_error())
to the end of your query execution statement; any error that is occurring is sneaking away undetected.
Upvotes: 2