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 o开发者_运维问答ther 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
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.
精彩评论