perleager has asked for the wisdom of the Perl Monks concerning the following question:

Hey - I've been trying to figure out how come this doesn't work for me. I have a insert field called MEMO and memos go longer then 255 characters, so I assigned the column type to "LONGTEXT" and now when I try to add a memo it gives me a SQL syntax error at line 1.

Previously the column type was "VARCHAR(255)" and the insert code worked fine w/the exception that it cut 75% of the memo_text field I have on my form.

Now I tried looking this up on the MySQL documentation, but no luck. I tried the $sql = $dbh->quote($string) option incase the memo field had escape strings.
-That didn't work.

I also tried changing the column type to BLOB instead of longtext - That didn't work.

Does anyone know what my problem might be?

Thanks,
Anthony

Replies are listed 'Best First'.
Re: MySQL Blob / DBI quote ?
by gmax (Abbot) on Dec 19, 2003 at 22:14 UTC
    That didn't work.

    That isn't the kind of remark that will help us to solve your problem. Perhaps you should read (and meditate upon) Before asking a database related question .... The most important piece of information from that node is about creating a test case for submission. Please try it.

    BTW, I remember now that I gave you the same advice for a different problem, but you don't seem to be listening.

    As for your case, you say that you are quoting $string, but you failed to mention what $string is. Is it your query? Your shopping list? Your cat's favorite food? Or a variable containing your text? Please give us more details. A few lines od code replicating your problem would be helpful.

    In addition to hardburn's advice on using placeholders, you can also have a look at a practical example where placeholders are used successfully with BLOB fields ( Handling huge BLOB fields with DBI and MySQL).

     _  _ _  _  
    (_|| | |(_|><
     _|   
    
      Hey - I further read more on the quote option and that was totally the wrong direction to fix my problem. I'm trying to add memo's to a database, some memos are way more the 255 characters so I changed the "memo_text" field into a blob columntype instead of "varchar(255) not null." Heres my code:
      $time = time; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($tim +e); $mon++; $year += 1900; $memo = "Original Message\n------------\n$INPUT{'memo'}"; $memo_final = $memo . "\n\nCreated by: $username\nDate Created: $year- +$mon-$mday\n\n"; use DBI; my $dbh_m = DBI->connect('DBI:mysql:MEMO') or print "Couldn't con +nect to database: " . DBI->errstr; $sql_m = " INSERT INTO memo VALUES ( '?', '$year-$mon-$mday', '$INPUT{'user'}', '$INPUT{'ref'}', '$INPUT{'username'}', 'notread', '$memo_final' ) "; $sth_m = $dbh_m->prepare($sql_m) or print "preparing: ",$dbh_m->errst +r; $sth_m->execute or print "executing: ", $dbh_m->errstr; $sth_m->finish; $dbh_m->disconnect; &header; print <<EOF; <B>Memo to $INPUT{'user'} successfully added.</b> <br> EOF &footer;


      Now, previously when the column type was "varchar(255) not null" , this insert code worked fine and all memos were added successfully.

      But when I altered the table to make it a blob column type this insert code returns a error giving me:

      executing: You have an error in your SQL syntax near 've been trying t +o figure out how come this doesn't work for me. I have a insert ' at +line 12


      Part of that error message is due to the fact that the memo I tried insterting included the sentence 'I've been trying to figure out how come this doesn't work for me. I have a insert '

      Thank you,
      Anthony
        Now, previously when the column type was "varchar(255) not null" , this insert code worked fine and all memos were added successfully.

        I don't think so. It could not possily work with any value of $memo. For example, you are preparing a query with one placeholder and then you are executing without parameters. And your placeholder is quoted, so it could not work.

        Further advice: use strict.

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: MySQL Blob / DBI quote ?
by hardburn (Abbot) on Dec 19, 2003 at 21:01 UTC

    In your code, just before the line that causes the error, do this:

    DBI->trace(2);

    And after the troublesome SQL is executed, do this:

    DBI->trace(0);

    This will allow you to see (on STDERR) what SQL and parameters are being passed to the database.

    Also, you usually don't need quote() if you're using placeholders. (You are using placeholders, right? Right?)

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated