0

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)"); 
4
  • What do you mean by 5 rows instead of 1000? Do you have any code you can post? Your varchar is set to 20 chars, are you trying to add a 1000 letter word? Commented Mar 15, 2012 at 16:04
  • Where are your error checks? Not that something goes wrong and you don't even notice and miss the useful information error messages can give to you... . Commented Mar 15, 2012 at 16:04
  • quite unusual database setup. are you sure just the string itself won't suit you? Commented Mar 15, 2012 at 16:05
  • I'm writing a anagram solver just for fun. Each row will be a word (technically "$letter" is misleading). That's why there is 1000 rows. No word is more than 20 characters. Commented Mar 15, 2012 at 16:08

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Your Common Sense: I think your edit overwrote my edit, so I lost the last paragraph. Rolled back and added your escape edit back in. FYI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.