I successfully use the below run_sql_script() function to execute SQL in a file using PHP.
static public function run_sql_script($script)
{
// Load and explode the sql file
$f = fopen($script,"r+");
$sqlFile = fread($f,filesize($script));
$sqlFile=preg_replace("/\\\;/", ';', $sqlFile); //replace semicolons with ascii
$sqlArray = explode(';',$sqlFile);
//Process the sql file by statements
foreach ($sqlArray as $stmt)
{
if (strlen($stmt)>8)
{
//Used to prevent blank lines at end of sql script from executing
$stmt=preg_replace("/;/", ';', $stmt); //replace ascii with semicolons
try {db::db()->exec($stmt);}
catch(PDOException $e){library::sql_error($e,$stmt);}
}
}
return;
}
I generate the SQL using MySQL Workbench, and recently, I added a trigger also through MySQL Workbench. It added the following SQL to the file.
DELIMITER $$
CREATE TRIGGER tg_contacts_upd AFTER UPDATE ON contacts
FOR EACH ROW
....
BEGIN
END$$
DELIMITER ;
Upon running the new file through my run_sql_script() function, I now get the following error.
Error in query: DELIMITER $$ CREATE TRIGGER tg_contacts_upd AFTER
UPDATE ON contacts FOR EACH ROW BEGIN END$$ DELIMITER SQLSTATE[42000]:
Syntax error or access violation: 1064 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 'DELIMITER $$ CREATE TRIGGER
tg_contacts_upd AFTER UPDATE ON contacts FOR EACH ' at line 1
Any suggestion how to fix
Seems too easy, but the following appears to work.
static public function run_sql_script($script)
{
$sql = file_get_contents($script);
db::db()->exec($sql);
}
Related
I'm working on a php project and i'm still a novice in error debugging.
whenever i try to execute the following code
CREATE PROCEDURE insertData(Name varchar(255),Description text)
BEGIN
INSERT INTO categories(name,description) VALUES (Name,Description);
END
i get an error stating (in phpmyadmin)
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 3
Try using a DELIMITER statement:
DELIMITER $$
CREATE PROCEDURE insertData (
in_Name varchar(255),
in_Description text
)
BEGIN
INSERT INTO categories(name, description)
VALUES (in_Name, in_Description);
END;$$
DELIMITER ;
Notice that I also renamed in the input parameters so they are less likely to be confused with column names. This is a good practice when writing stored procedures and functions.
DELIMITER //
CREATE PROCEDURE Sample
BEGIN
DECLARE v_SQLSTR VARCHAR(800);
SET v_SQLSTR='Hi';
END;
//
DELIMITER ;
Error Details: Error Code: 1064. 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 'BEGIN DECLARE v_SQLSTR VARCHAR(800); SET
v_SQLSTR='Hi'; END' at line 2
I am new in MySQL. Please help.
Missed the brackets. Try this:
...
CREATE PROCEDURE Sample()
...
That is because you have a syntax error:
Add () to your procedure's name
Remove ; after the END keyword
Change your code to:
DELIMITER //
CREATE PROCEDURE Sample()
BEGIN
DECLARE v_SQLSTR VARCHAR(800);
SET v_SQLSTR='Hi';
END //
DELIMITER ;
To avoid further errors, you can check the official documentation.
I'd like to create a trigger from the following PHP code.
$sql = 'delimiter $$';
$pdo->exec($sql);
$sql = 'create trigger avoid_empty_employee_insert before insert on `employee`
for each row begin
if name = "" then set name = null; end if;
end$$';
$pdo->exec($sql);
$sql = 'delimiter ;';
$pdo->exec($sql);
When I run the the code in MySQL it works and the trigger is created.
PHP shows the following error.
SQLSTATE[42000]: Syntax error or access
violation: 1064 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 'delimiter $$' at line 1
How can I fix it?
Definitely do not try to change the delimiter when you're executing the statement via the API. DELIMITER is a mysql client built-in command, it is not recognized by the MySQL server-side parser.
You don't need it anyway. The purpose of DELIMITER is to remove the ambiguity of semicolons that may appear within the body of a trigger or stored routine. Since the API is for executing one statement at a time, there's no ambiguity. The SQL parser just treats the whole string as one statement anyway.
Likewise, do not end the create trigger statement with $$. You don't need any statement terminator, but the SQL parser accepts ; as an optional statement terminator because so many people put it there even though they don't have to.
The next problem is that you when you use column names in a trigger, you have to prefix them with either NEW. or OLD. -- in an insert trigger, you can only use NEW. If you don't prefix the column, MySQL assumes you meant to set a system variable like tmpdir or slow_query_log.
If you are still getting the 1193 error, I suggest that you didn't change both references to the name column to NEW.name.
I tested the following using PHP 5.4.24 and MySQL 5.6.20, and it worked:
$sql = "create trigger avoid_empty_employee_insert before insert on `employee`
for each row begin
if NEW.name = '' then set NEW.name = null; end if;
end";
$pdo->exec($sql);
You don't need to delimit the column name of name, because it is not a MySQL reserved word. The set of reserved words is documented.
try that :
$sql = 'create trigger avoid_empty_employee_insert before insert on `employee`
for each row begin
if name = "" then set name = null; end if; END;';
$pdo->exec($sql);
Since you are creating the trigger using code, you might not need to set/reset the DELIMITER.
Just ignore those lines in your php code that changes them.
Your code becomes only:
$sql = 'create trigger avoid_empty_employee_insert before insert on `employee`
for each row begin
if NEW.`name` = "" then set NEW.`name` = null; end if;
end$$';
$pdo->exec($sql);
Mayby this helps:
$sql = "delimiter //\n";
$pdo->exec($sql);
$sql = "create trigger avoid_empty_employee_insert before insert on employee\n
for each row\n
begin\n
if new.name = '' then\n
set new.name = null;\n
end if;\n
end//\n";
$pdo->exec($sql);
$sql = 'delimiter ;';
$pdo->exec($sql);
Since you are creating the trigger using code, you might not need to set/reset the DELIMITER. Changing the DELIMITER is important for CLIs.
Just ignore those lines in your php code that changes them.
Your code becomes only:
$sql = 'create trigger `avoid_empty_employee_insert` before insert
on `employee`
for each row
begin
if `name` = "" then set `name` = null; end if;
end';
$pdo->exec($sql);
I got this request:
"START TRANSACTION; DELETE FROM `awaiting_auth` WHERE `code` = '06b8465eed00727a1eac49fae89b88f876ded2eb' LIMIT 1; INSERT IGNORE INTO `prsn` SET `login` = 'new_user', `passwd` = '40bd001563085fc35165329ea1ff5c5ecbdbbeef', `color` = '#cbc5f2'; COMMIT;"
And I receive this error:
"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 'DELETE FROM awaiting_auth WHERE code =
'06b8465eed00727a1eac49fae89b88f876de' at line 1".
However when executing sql via terminal everything goes well and no errors are thrown.
What is wrong with my request? Thanks in advance.
You must split the queries in separate ->query() calls. The SQL console does that automatically. E.g.
->query("START TRANSACTION");
->query("DELETE FROM...");
One operation per query. So you need to split your query into single queries.
Just run these queries one by one, not in single packet.
Try to execute each statement individually:
START TRANSACTION;
DELETE FROM `awaiting_auth` WHERE `code` = '06b8465eed00727a1eac49fae89b88f876ded2eb' LIMIT 1;
INSERT IGNORE INTO `prsn`(`login`, `passwd`, `color`) VALUES('new_user','40bd001563085fc35165329ea1ff5c5ecbdbbeef', '#cbc5f2');
COMMIT;
I want to run the following mysql create function statement from PHP:
DELIMITER $$
CREATE FUNCTION `myFunc`(`instring` varchar(4000)) RETURNS int(11)
NO SQL
DETERMINISTIC
SQL SECURITY INVOKER
BEGIN
DECLARE position int;
....here comes function logic
RETURN position;
END$$
DELIMITER ;
But I get this mysql error:
check the manual that corresponds to your MySQL server version for the
right syntax to use near 'DELIMITER'
What can I do? Can I execute create statement without DELIMITER keyword?
You most likely do not need the DELIMTER command. That belongs to MySQL-centric client programs.
Please try with plain old semicolons:
if (!$mysqli->query("DROP PROCEDURE IF EXISTS p") ||
!$mysqli->query("CREATE PROCEDURE p(IN id_val INT) BEGIN INSERT INTO test(id) VALUES(id_val); END;")) {
echo "Stored procedure creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
and let PHP worry about delimiting
CAVEAT
I got the above code from http://php.net/manual/en/mysqli.quickstart.stored-procedures.php