Problem:
Unable to store data with mySQL stored procedure with mysqli_begin_transaction.
Details:
The below code will do simple insert and select using mysql stored procedure. Code runs fine without select statement. However once the select statement is added, it won't commit any data even the query returns success at PHP side.
Snippets (PHP):
$DB_DRRM_SQLI = mysqli_connect("localhost","root","", "sandbox_db");
mysqli_begin_transaction($DB_DRRM_SQLI);
$SQL_QUERY_CODE = "CALL SANDBOX_TEST()";
$DB_QUERY = mysqli_query($DB_DRRM_SQLI, $SQL_QUERY_CODE);
// ERROR REPORTING
if($DB_QUERY === false)
{
echo mysqli_error($DB_DRRM_SQLI);
mysqli_rollback($DB_DRRM_SQLI);
}
else
{
echo 'success';
mysqli_commit($DB_DRRM_SQLI);
}
exit;
Snippets (mySQL Stored procedure):
BEGIN
INSERT INTO
`sandbox_table`
(
`SOME_STRING`
)
VALUES
(
'ABCDEFGHIJKL...'
);
SELECT
LAST_INSERT_ID() AS INSERTED_ID,
'ABCDE...' AS OTHER_PARAMS;
END
Database (Table sandbox_table):
RECORD_PRIMARY_ID (Int - Auto increment)
SOME_STRING (Varchar - 500 length)
Spec:
PHP version: 5.6.14
10.1.8-MariaDB
Storage Engine: InnoDB
Notes:
If transaction is made at stored procedure works fine, but I need a PHP managed transaction to handle multiple query requests and response depending on the result of query.
(It can be a possible last resort if there's no other solution, where I need to convert whole PHP code to stored procedure and need pass tons of parameter)
Methods Tested:
Tried with other PHP version 7.0.9 with same result (10.1.16-MariaDB)
Tested with new database with no other data except sandbox_tableand above stored procedure.
Tested without additional include libraries (tested with purely on above snippets).
Solution:
It was caused by Commands out of sync error at mysqli_commit. Seems the mysqli won't allow committing transaction while the query is open, which happens if you add select statement to above stored procedure.
So to handle this, it must close the query first or put the query to buffer.
Snippets (PHP):
// SQL Database
$DB_DRRM_SQLI = mysqli_connect("localhost","root","", "sandbox_db");
mysqli_begin_transaction($DB_DRRM_SQLI);
$SQL_QUERY_CODE = "CALL SANDBOX_TEST()";
$DB_QUERY = mysqli_query($DB_DRRM_SQLI, $SQL_QUERY_CODE);
// ERROR REPORTING
if($DB_QUERY === false)
{
echo mysqli_error($DB_DRRM_SQLI);
mysqli_rollback($DB_DRRM_SQLI);
}
else
{
// Must free current query result before committing transaction
#mysqli_free_result($DB_QUERY);
#mysqli_next_result($DB_DRRM_SQLI);
if(mysqli_commit($DB_DRRM_SQLI) === false)
{
echo mysqli_error($DB_DRRM_SQLI);
}
else
{
echo 'success';
}
}
exit;
Related
I am attempting to do two things using the PHP OCI Oracle functions:
Run a package procedure within an Oracle database.
Once the package has ran, query a temporary table to get the results of the procedure's operation.
I am able to do this successfully using the SQL Developer software provided from Oracle. My query is extremely basic and can been seen below:
BEGIN
PKG_KTY_SEARCH.PR_PRICE_LIST();
END;
/
SELECT * FROM kty_web.KTY_PROD_PRICE_TEMP;
This code above works perfectly and I get a full table of results in SQL Developer.
I am attempting to do the same thing above in PHP using OCI. My code can be seen below:
<?php
// Load up the system.
require('../../system/init.php');
global $config;
$oracleDb = oci_new_connect($config['oracleDb']['username'], $config['oracleDb']['password'], $config['oracleDb']['connectionString']);
$firstStid = oci_parse($oracleDb, "BEGIN PKG_KTY_SEARCH.PR_PRICE_LIST(); END;");
oci_execute($firstStid);
$secondStid = oci_parse($oracleDb, "SELECT * FROM kty_web.KTY_PROD_PRICE_TEMP");
oci_execute($secondStid);
oci_fetch_all($secondStid, $result);
echo json_encode($result);
echo "<br />Import complete!";
?>
This however returns no errors, and an empty result set. I can't figure out why. Anybody seeing anything obvious here that I'm missing?
Result set returned from PHP
{"PRODUCT_ID":[],"CUST_ROLE":[],"MIN_QTY":[],"MAX_QTY":[],"PRICE":[]}
My connection string is as follows:
$config['oracleDb']['connectionString'] = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = " . $config['oracleDb']['host'] . ")(PORT = " . $config['oracleDb']['port'] . ")))(CONNECT_DATA=(SID=" . $config['oracleDb']['sid'] . ")))";
I am using PHP7.1.22, and Oracle 11g database. I am able to query normal tables and get results without problems within PHP and get a full result set.
Is the temporary table defined as on commit delete rows or as on commit preserve rows?
By default, oci_execute will implicitly issue a commit after every successful call. Assuming your temporary table is defined as on commit delete rows, that will delete the rows before the subsequent query. You can change that behavior by passing an optional second parameter
oci_execute($firstStid, OCI_DEFAULT);
Assuming you do this, however, you'll want to do an explicit oci_commit in order to close the transaction you've opened.
I wanted to make a simple website, which can open, show, and run sql server queries, just like executing queries from sql server management studio. It already can execute some queries like select (then show it as a table), insert, update, delete, including when i use where/group/order statement.
But, when I doing backup on my database with query :
BACKUP DATABASE 'mydb' TO DISK='pathname\filename.bak' WITH INIT/NOINIT
It can only backup the database once, and then when I execute the query with the same inputs, it got out with no error, but also no file backups produced from the execution. I don't know why, but when i'm executing with the same query from sql server management studio (with different filename so that the previous file didn't get overwritten), it execute without error and creating another file backup.
The code :
php
$sql = "ALTER DATABASE $db_name SET RECOVERY FULL";
$stmt = sqlsrv_query($conn, $sql);
if($stmt === false)
{
die(print_r(sqlsrv_errors()));
}
else
{
echo "Recovery model set to FULL";
}
if($_POST['type']=="Backup Full")
{
$query="BACKUP DATABASE $db_name TO DISK='$file\\$filename.bak' WITH
NOINIT;";
echo "<br>".$query."<br>";
$stat=sqlsrv_query($conn,$query);
if($stat == false)
{
echo "Error to retrieve info, THIS IS THE ERROR : <br />";
die(print_r(sqlsrv_errors()));
}
else
{
echo "Success!";
}
}
It create a new file .bak filled with my database backup
It returns error inside the 'if'
When i tried to run the code again with the different filename, it didn't create anymore .bak file.
When i restarted my PC, things back to normal (creating another .bak again)
It seems there's a solution here. Your issue it happened to me also but in my case sometimes the backup was created and sometimes no and I guess that the backup process fails when php executes and it exits before the ending backup process, therefore, it kills the connection in the middle of backup process. I realized that to execute a sql sequence of no-query type, it has to be consumed with the instruction sqlsrv_next_result till there's no more results.
You should use this snippet in order to execute the backup query successfully.
$query="BACKUP DATABASE $db_name TO DISK='$file\\$filename.bak' WITH NOINIT;";
if ( ($stmt = sqlsrv_query($conn, $query)) )
{
do
{
} while ( sqlsrv_next_result($stmt) ) ;
sqlsrv_free_stmt($stmt);
}else{
die(print_r(sqlsrv_errors()));
}
Background
I have a simple database driven Semaphore in a php based web application that uses sqlsrv extension for windows and mssql for linux/max.
The Problem
The query I'm running looks like:
BEGIN TRY
INSERT INTO Semaphores VALUES (:name, :process_id)
END TRY
BEGIN CATCH
SELECT 'false' AS Result
END CATCH
This query works perfectly in SQL Server management studio. It also works in mssql extension. But in sqlsrv the catch query is never executed (or at least the results are not available).
The code for that looks like:
$results = sqlsrv_query($this->connection, $built_query);
/* Error handling here left out for brevity */
$row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC);
if ($row === FALSE)
{
// The insert worked so no rows exist causing the fetch array to fail.
return true;
}
else
{
// The select executed. Don't actually care about the contents.
// This means the insert failed.
return false;
}
$row is always FALSE (ERROR STATE). No matter what the query looks like. I can force either branch of the query by itself and it works as expected.
The question.
Why does the sqlsrv_fetch_array not obtain the catch results?
I have a very simple INSERT statement that I can run using SSMS. When I run the same INSERT statement using PHP mssql_execute or mssql_query SQL profiler shows me that the 'TextData' is identical when sent in by PHP as it is when sent in by SSMS. The problem is when executed via the PHP code, the INSERT never actually happens, whereas when run from within SSMS, it inserts just fine. Any ideas?
MS SQL: 2008 R2
PHP: 5.4, freetds 8.0
PHP error:
PHP Warning: mssql_execute(): message: Internal Query Processor Error: The query processor could not produce a query plan. For more information, contact Customer Support Services. (severity 16) in /home/...
Edit: PHP relevant PHP added
<?php
putenv('FREETDS=/home/###/freetds.conf');
putenv('FREETDSCONF=/home/###/freetds.conf');
$link = mssql_connect('mssqlserver', '###','###'); // connect
if ( !$link ) {
if ( function_exists('error_get_last') ) {
var_dump(error_get_last());
}
die('connection failed');
}
// Create a new statement
$stmt = mssql_init('Db.dbo.SprocName');
// Some values
$a = 1;
$b = 'test';
$c = 'myname';
// Bind values
mssql_bind($stmt,'#a',$a,SQLINT2,false,false);
mssql_bind($stmt,'#b',$b,SQLVARCHAR,false,false,8000);
mssql_bind($stmt,'#c',$c,SQLVARCHAR,false,false,50);
// Execute the statement
mssql_execute($stmt);
// And we can free it like so:
mssql_free_statement($stmt);
?>
The stored procedure (Db.dbo.SprocName) did a simple insert on a table, lets call it (Db.dbo.TableName). When I removed the foreign key from Db.dbo.TableName then the PHP mssql INSERT calls started working.
I realize this technically resolves the issue, but in practice its not acceptable. Those FK's are there for a reason :). Any thoughts?
I'm developing an update system for a Web Application written in PHP. In the process of the update I might need to execute a bunch of MySQL scripts.
The basic process to run the scripts is:
Search for the Mysql scripts
Begin a transaction
Execute each script with mysqli_multi_query since a script can contain multiple queries
If everything goes ok COMMIT the transaction, otherwise ROLLBACK.
My code looks something like:
$link = mysqli_connect(...);
mysqli_autocommit($link, false);
// open dir and search for scripts in file.
// $file is an array with all the scripts
foreach ($scripts as $file) {
$script = trim(file_get_contents($scriptname));
if (mysqli_multi_query($link, $script)) {
while (mysqli_next_result($link)) {
if ($resSet = mysqli_store_result($link)) { mysqli_free_result($resSet); }
if (mysqli_more_results($link)) { }
}
}
// check for errors in any query of any script
if (mysqli_error($link)) {
mysqli_rollback($link);
return;
}
}
mysqli_commit($link);
Here is an example of the scripts (for demonstration purposes):
script.1.5.0.0.sql:
update `demo` set `alias` = 'test1' where `id` = 1;
update `users` set `alias` = 'user1' where `id` = 1;
script 1.5.1.0.sql:
insert into `users`(id, key, username) values(3, '100', 'column key does not exist');
insert into `users`(id, key, username) values(3, '1', 'column key exists');
In this case, script 1.5.0.0 would execute without errors and script 1.5.1.0 would generate an error (for demonstration purposes, let's say that column key is unique and there is already a row with key = 1).
In this case I want to rollback every query that was executed. But what happens is that the first insert of 1.5.1.0 is not in the database (correctly) but the updates from 1.5.0.0 were executed successfully.
Remarks:
My first option was to split every query from every script with ";" and execute the queries independently. This is not an option since I have to be able to insert HTML code to the database (ex: if I want to insert something like "& nbsp;")
I've already searched StackOverflow and google and came across solutions like this one but I would prefer using a solution like mysqli_multi_query rather than using a function to split every query. It's more understandable and easier for debug purposes
I haven't tested it, but I believe that I could merge all the scripts and execute just a query. However it would be usefull to execute one script at a time so that I can figure out which script has the error.
The tables engine is InnoDB.
Appreciate if you can point some way to make this work.
Edit:mysqli_multi_query() only returns false if the first query fails. If the first query doesn't fail then your code will run mysql_store_result() which if it succeeds will leave mysqli_error() empty. You need to check for errors after every mysqli function that can succeed or fail.
Ok, after spending another day debugging, i've discovered the problem.
Actually, it has nothing to do with the code itself or with mysqli functions. I'm used to MS SQL transactions which supports DDL statements. MySQL does not supports DDL statements and commits data implicitly (Implicit commit). I had one DROP Table in one of the scripts that was auto commiting data.