1

Using the following function, I am attempting to update data to a database. The query works well when directly ran in phpmyadmin, but produces an error when running from php.

Here is the function

function edit_row($table, $columns, $where){
        db_connect();
        $query = "BEGIN WORK; SET AUTOCOMMIT=0; UPDATE $table SET $columns WHERE $where;    COMMIT;";
        echo $query; //this is to control for typing errors when testing in phpmyadmin
        mysql_query($query) or die ("Entry could not be made, " . mysql_error());
        db_close();
}

running this command:

edit_row("hello","test = 'some other string'", "test = 'somestring'");

echo outputs:

BEGIN WORK; SET AUTOCOMMIT=0; UPDATE hello SET test = 'some other string' WHERE test = 'some string'; COMMIT;

error produced:

Entry could not be made, 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 'SET AUTOCOMMIT=0; UPDATE hello SET test = 'some other string' WHERE test = 'so' at line 2

It appears to cut off the last bit of the query string, but not sure if this is a quirk of the die() method

1 Answer 1

4

You cannot execute multiple queries in a single call to mysql_query - you need to break-up your query into four separate calls.

As per the PHP mysql_query documentation:

mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

The reason this works in phpMyAdmin, is because phpMyAdmin is in fact carrying out four separate queries in the background after de-constructing the statement(s) entered.

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

10 Comments

I see, but breaking this string apart into separate queries won't break the transaction?
@Mild Fuzz As long as they're all executed within the same connection (i.e.: the resource returned by mysql_connect), you should be fine. :-)
excellent. Now I just need to work out how to add a ROLLBACK on error.
@Mild Fuzz Just be aware of the need to keep all such things within the same connection object and you should be good to go. :-)
is this not technically making more database calls, and thus taking a performance hit?
|

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.