Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-09-26 21:45:11

gerhard
Plugin Author
From: London, UK
Registered: 2005-06-29
Posts: 409
Website

[Solved] SQL query works fine in command line, not in safe_query()

This is just doing my head in! I might have been looking too long at it. This is the error which I’m getting after calling safe_query():

Warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
'ALTER TABLE `textpattern` MODIFY `custom_3` VARCHAR(255) NOT NULL DEFAULT ''' at line 1
UPDATE `textpattern` SET `custom_3` = ''; ALTER TABLE `textpattern` MODIFY `custom_3` VARCHAR(255) NOT NULL DEFAULT '';
/var/www/glz_custom_fields/lib/db.php:37 glz_reset_custom_field() in /var/www/textpattern-4.2.0/textpattern/lib/txplib_db.php on line 85

Same query, executed on the command line:

mysql> UPDATE `textpattern` SET `custom_3` = ''; ALTER TABLE `textpattern` MODIFY `custom_3` VARCHAR(255) NOT NULL DEFAULT '';
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0

This is the bit of code which throws the above error:

  else if ( $table == PFX."textpattern" ) {
    $query = "UPDATE `".PFX."textpattern` SET `{$name}` = ''; ";
    $query .= "ALTER TABLE `".PFX."textpattern` MODIFY `{$custom_field}` VARCHAR(255) NOT NULL DEFAULT '';";
  }
  safe_query($query);

What am I missing?

Last edited by gerhard (2009-09-26 21:47:56)

Offline

#2 2009-09-26 21:59:01

gerhard
Plugin Author
From: London, UK
Registered: 2005-06-29
Posts: 409
Website

Re: [Solved] SQL query works fine in command line, not in safe_query()

It’s not pretty, but it seems to be working. Still can’t understand why…

  else if ( $table == PFX."textpattern" ) {
    safe_query("UPDATE `".PFX."textpattern` SET `{$name}` = ''");
    safe_query("ALTER TABLE `".PFX."textpattern` MODIFY `{$custom_field}` VARCHAR(255) NOT NULL DEFAULT ''");
  }

Offline

#3 2009-09-26 22:12:28

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: [Solved] SQL query works fine in command line, not in safe_query()

gerhard wrote:

It’s not pretty, but it seems to be working. Still can’t understand why…

PHP’s mysql_query() doesn’t support multiple queries.

Last edited by Gocom (2009-09-26 22:13:10)

Offline

#4 2009-09-26 22:17:42

gerhard
Plugin Author
From: London, UK
Registered: 2005-06-29
Posts: 409
Website

Re: [Solved] SQL query works fine in command line, not in safe_query()

So what happens when you have 10 queries queued for execution, do you run them one by one? That doesn’t sound right…

Offline

#5 2009-09-26 22:21:54

gerhard
Plugin Author
From: London, UK
Registered: 2005-06-29
Posts: 409
Website

Re: [Solved] SQL query works fine in command line, not in safe_query()

Jumping jelly beans, you are right, mysql_query() supports a single query. mysqli is the improved interface supporting multiple ones. Thanks for your help!

Offline

#6 2009-09-26 22:29:25

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: [Solved] SQL query works fine in command line, not in safe_query()

gerhard wrote:

So what happens when you have 10 queries queued for execution, do you run them one by one? That doesn’t sound right…

It’s handled by PHP. It’s not as bad as it seems. Note that mysql_query always returns something (single value). If you want to save memory there are alternative ways.

Thanks for your help!

Np.

Last edited by Gocom (2009-09-26 22:30:53)

Offline

#7 2009-09-27 08:08:46

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: [Solved] SQL query works fine in command line, not in safe_query()

If multiple SQL queries were supported by default (it is possible, see the comments in the PHP manual) and there was an SQL injection vulnerability in your code, the possibilities for exploiting it would be far greater.

Offline

Board footer

Powered by FluxBB