I'm trying to use transactions with PDO, with the following code
try
{
$bdd = new PDO('mysql:host=HOSTNAME;dbname=DATABASENAME', 'USERNAME', 'PASSWORD');
}
catch(Exception $e)
{
die('Error : '.$e->getMessage());
}
$qry = 'UPDATE table SET field = CASE';
foreach($elements as $el){
$qry .= ' WHEN id = '. $el['id'] .' THEN '. $el['value'];
}
$qry .= ' ELSE field END, SET update_date = CASE';
foreach($elements as $el){
$qry .= ' WHEN id = '. $el['id'] .' THEN NOW()';
}
$qry .= ' ELSE update_date END';
$update = $bdd->prepare($qry);
$bdd->beginTransaction();
try {
$update->execute();
$bdd->commit();
}
catch(Exception $e) {
$bdd->rollback();
echo 'Error : '.$e->getMessage().'<br />';
echo 'N° : '.$e->getCode();
exit();
}
But I get the following error (and the table is not updated) :
Fatal error: Uncaught exception 'PDOException' with message 'There is no active transaction' in script.php:106
Stack trace:
#0 script.php(106): PDO->rollBack() #1 {main} thrown in script.php on line 106
(Line 106 is $bdd->rollback();)
I understand that it's telling me there is no transaction going on, but... there should be, with $bdd->rollback();...
Thanks for any help you can give !
EDIT : The database engine is MyISAM.
I have additional info after further testing :
I simplified the $qry in my question because I thought it was irrelevant, but it seems it isn't. The query I'm actually using is a much longer one, updating multiple elements with the following code (I also modified the code up there ^ in the original question) :
$qry = 'UPDATE table SET field = CASE';
foreach($elements as $el){
$qry .= ' WHEN id = '. $el['id'] .' THEN '. $el['value'];
}
$qry .= ' ELSE field END, SET update_date = CASE';
foreach($elements as $el){
$qry .= ' WHEN id = '. $el['id'] .' THEN NOW()';
}
$qry .= ' ELSE update_date END';
It seems that the length of the query and/or the number of updated lines is causing my issue :
when updating 72 lines at most (the query is 3463 characters long, all spaces included), the table is updated, and vardump($bdd->beginTransaction();) returns bool(true) (regardless of wether it's before or after the $update = $bdd->prepare($qry) line.
when updating 73 lines or more (the query is 3509 characters long, all spaces included, when it updates 73 lines), the table is not updated, and vardump($bdd->beginTransaction();) returns bool(false) (once again, regardless of wether it is before or after the $update = $bdd->prepare($qry) line.
Hints on why, and how I can fix it are appreciated.
Thanks again
EDIT 2 : I switched the engine to InnoDB. The issue still stands.
Move
$bdd->beginTransaction();
to above your query:
$qry = 'UPDATE table SET field = value'
$update = $bdd->prepare($qry);
EDIT:
You are using MyISAM engine which does not support transactions. Update your tables to use InnoDB.
Related
I have custom payment module that I need to upgrade and while upgrade, I got an error while executing Insert query.
Error:
Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.
I can't find solution :( please guide me, If you know the solution..
I have tried other stackoverflow link but did not help.
this is my code:
$insert = 'INSERT INTO '.$this->getTableNameSQL($tableName).' SET ';
foreach ($tableMapping as $varName => $dbFieldName) {
if (!isset($valueArray[$varName])) {
if ($this->tblMapping->isAutoFill($tableName, $dbFieldName)) {
continue;
}
return $this->log->put(3, "Var '$varName' not found", $valueArray);
}
if ($fieldsMustHaveValues && '' == trim($valueArray[$varName])) {
return $this->log->put(3, "Var '$varName' must have a value", $valueArray);
}
$chkstr[$varName] = $valueArray[$varName];
$insert .= $this->checkName($dbFieldName).' = '.$this->checkValue($valueArray[$varName]).', ';
}
$insert .= ' checkstr = '.$this->checkValue($this->calcCheckStr($tableName, $chkstr));
$result = $this->prestaDb->getInstance()->execute($insert);
And printed insert query is:
INSERT INTO `ps_product_pronuxtwint_log` SET type = 'SW5mbw==', msg_type = 'U29hcFJlcXVlc3Q=', http_header = 'QXJyYXkKKAogICAgW0hvc3RdID0+IGxvY2FsaG9zdDo4MDgwCiAgICBbVXNlci1BZ2VudF0gPT4gTW96aWxsYS81LjAgKFgxMTsgVWJ1bnR1OyBMaW51eCB4ODZfNjQ7IHJ2OjYwLjApIEdlY2tvLzIwMTAwMTAxIEZpcmVmb3gvNjAuMAogICAgW0FjY2VwdF0gPT4gdGV4dC9odG1sLGFwcGxpY2F0aW9uL3hodG1sK3htbCxhcHBsaWNhdGlvbi94bWw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aWZpZWRDdXN0b21lcj4KCQkJPFFSQ29kZVJlbmRlcmluZz4xPC9RUkNvZGVSZW5kZXJpbmc+CgkJPC9TdGFydE9yZGVyUmVxdWVzdEVsZW1lbnQ+CiAgICAgICAgICAgIDwvc29hcDpCb2R5PgogICAgICAgIDwvc29hcDpFbnZlbG9wZT4=', ip_address = 'MTI3LjAuMC4x', device_type = 'ZGVza3RvcA==', unix_timestamp = 'MTUzMjc1ODUxOQ==', date_string = 'MjguMDcuMjAxOA==', id_shop = 'MQ==', id_cart = 'MTE=', id_order = '', id_cust = 'NA==', amount = 'NDQuMTc=', id_lang = 'NA==', status = 'dW5rbm93', timestamp = 'MTUzMjc1ODUxOQ==', checkstr = 'N2IyODdhYzkxZDlhMThjMzMyOTc2NGI5NGQ1MDUxNDM='
Thanks in advance
I am using Server version 10.1.21-MariaDB and mysql for performing data operation. I am creating mini search tool for searching jokes.The overview of how i am performing it is shown in image. Everything works fine but when i try to execute sql statement,it shows syntax error. I tried to dig inside it but as i am quite new to this stuff,i lost here and there digging the exact way of using syntax. The error is shown in bold letter and code is provided below where error occurs.I think error must be inside the try block,please help me to sort this out.
//query logic
$select = 'SELECT id,joketext ';
$from = 'FROM joke_info';
$where = 'WHERE TRUE';
$placeholders = array();
if(isset($_GET['author']) != ''){
$where .= " AND authorid = :authorid";
$placeholders[':authorid'] = $_GET['author'];
}
if(isset($_GET['category']) != ''){
$from .= ' INNER JOIN jokecategory ON id = jokeid';
$where .= " AND categoryid = :categoryid";
$placeholders[':categoryid'] = $_GET['category'];
}
if ($_GET['text'] != '')
{
$where .= " AND joketext LIKE :joketext";
$placeholders[':joketext'] = '%' . $_GET['text'] . '%';
}
print_r($placeholders);
try
{
$sql = $select . $from . $where;
$s = $pdo->prepare($sql);
$s->execute($placeholders);
}catch (PDOException $e)
{
$error = 'Error fetching jokes. ';
echo $error.$e->getMessage();
exit();
}
Error Says this:
Array ( [:authorid] => 6 [:categoryid] => 10 [:joketext] => %been working% ) Error fetching jokes. SQLSTATE[42000]: Syntax error or access violation: 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 'TRUE AND authorid = '6' AND categoryid = '10' AND joketext LIKE '%been working%'' at line 1
Your generated query after $sql = $select . $from . $where; will be something like SELECT id,joketext FROM joke_info WHERE TRUE
So there is no space between joke_info and Where clause
It should be $from = ' FROM joke_info' and $where = ' WHERE TRUE '; instead of $from = 'FROM joke_info' and '$where ='WHERE TRUE';
Ther is no space between $from and $where variables. Currently they are concatenating to: 'FROM joke_infoWHERE TRUE'. Try giving a space in the $from variable.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
The executing script does mostly nothing but call the following function. This problem doesn't happen if I am not inserting anything beforehand (and just select the entire table).
function create_author()
{
$config = config();
$database = mysql_connect($config['database']['hostname'], $config['database']['username'], $config['database']['password']);
mysql_select_db($config['database']['database']) || die('Cannot select database');
$table = 'authors';
$values = array();
$values[] = sprintf(
'first_name = "%s"',
mysql_real_escape_string($_POST['first_name'])
);
if (array_key_exists('middle_initial', $_POST) && !empty($_POST['middle_initial'])) {
$values[] = sprintf(
'middle_initial = "%s"',
mysql_real_escape_string($_POST['middle_initial'])
);
}
$values[] = sprintf(
'last_name = "%s"',
mysql_real_escape_string($_POST['last_name'])
);
if (array_key_exists('biography', $_POST) && !empty($_POST['biography'])) {
$values[] = sprintf(
'biography = "%s"',
mysql_real_escape_string($_POST['biography'])
);
}
$query = sprintf(
'INSERT INTO %s SET %s;',
$table,
implode(', ', $values)
);
mysql_query($query, $database) || die(mysql_error());
$genre_id = mysql_insert_id($database);
$query = sprintf(
'SELECT * FROM authors WHERE id = %u LIMIT 1;',
$genre_id
);
echo $query, PHP_EOL, '<br />', PHP_EOL;
$result = mysql_query($query, $database) || die('Failed to read author.');
//header('Content-Type: application/json');
echo '$result: ', is_bool($result) ? ($result ? 'true' : 'false') : strval($result), PHP_EOL, '<br />', PHP_EOL;
echo 'Number of selected rows: ', mysql_affected_rows($database), PHP_EOL, '<br />', PHP_EOL;
echo 'Error: ', mysql_error($database), PHP_EOL, '<br />', PHP_EOL;
mysql_close($database);
echo json_encode(mysql_fetch_assoc($result));
}
This gives the following output:
SELECT * FROM authors WHERE id = 27 LIMIT 1;
$result: true
Number of selected rows: 1
Error:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in file_path.php on line 89
null
Using a different SQL client, the above SELECT query returns a row. I am using PHP 5.3.3.
The "duplicate" questions don't seem to answer the question.
Before the anti-mysql_* bandwagon rolls around the corner, I am using a server in which I have no control over for a school project. The server is controlled by the IT department. The pdo_mysql and mysqli extensions are not installed. Additionally, there are no other SQL extensions enabled except for the sqlite extension (which wouldn't suffice since I need to use foreign keys). And the system administrator will not make changes. The point is that I have no choice but to use the mysql_* functions.
Using the || operator converts the result to a boolean value.
i am trying to catch this error in a php webapp that captures input data from a user. im using php and sql on mypgadmin, sourcecode column constraints : "CONSTRAINT sourcecodes_sourcecode_key UNIQUE (sourcecode)"
ERROR
Warning: pg_execute() [function.pg-execute]: Query failed: ERROR: duplicate key value violates unique constraint "sourcecodes_sourcecode_key" DETAIL: Key (sourcecode)=(Source3) already exists. in D:\xampp\htdocs ...php on line 289
Problem when user enters an existing sourcecode then the error is generated on POSTING the value.
Code
if(array_key_exists("btnsubmit", $_POST)) {
$pk_check = "true";
if($pk_check == "true") {
$parameters = array();
$parameters[] = $counter;
$parameters[] = $_POST['source'];
$sql_string = "INSERT INTO config.sourcecodes (id,sourcecode ) VALUES ($1,$2 )";
try {
$saved = $db_cfg->Execute($sql_string, "", $parameters);
if($saved) {
$feedback = 'New Campaign: ' . $_POST['campaign_id'] . ' Saved successfully';
} else {
error_reporting(32);
$feedback = "_Error Saving Source Code ! _";
//echo pg_last_notice($sql_string) ;
// throw new $exception('Error saving new source code');
}
}
catch(\sdException $exception) {
throw new Exception('fatal err trycatch nt wrkn');
}
Try to change your query adding a 'ON DUPLICATE KEY' to catch the error on a db level, check out : https://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
Note:
You can fake the update by ...UPDATE c=c; for example
Ex: 'INSERT INTO config.sourcecodes (id,sourcecode ) VALUES ($1,$2 ) ON DUPLICATE KEY UPDATE id = $1'
PostgreSQL 9.5:
INSERT INTO config.sourcecodes (id,sourcecode ) VALUES ($1,$2 )
ON CONFLICT (sourcecode) DO UPDATE SET id = id;
Or by catching exceptions:
BEGIN
INSERT INTO config.sourcecodes (id,sourcecode ) VALUES ($1,$2 );
EXCEPTION
WHEN unique_violation THEN
UPDATE config.sourcecodes SET sourcecode = sourcecode WHERE id = $1;
END;
Relevant code:
add_action( 'wp_ajax_proj_update', 'proj_update' );
function proj_update ( )
{
$id = $_POST['id'];
$compname = $_POST['compname'];
$projname = $_POST['projname'];
$imageurl = $_POST['imageurl'];
$sumsmall = $_POST['sumsmall'];
$sumfull = $_POST['sumfull'];
$results = $_POST['results'];
$caseid = (!isset($_POST['caseid']) || strcmp($_POST['caseid'],'none')) ? $_POST['caseid'] : "NULL"; // weirdness required to get the value of our <select name="caseid"> element converted to something we can insert to database
$hide = array_key_exists('hide',$_POST) !== false ? 1 : 0; // weirdness required to get the value of <input type="checkbox" name="hide"> converted to something we can insert to database
$thisAction = $_POST['thisAction'];
global $wpdb;
$message = "";
switch ($thisAction)
{
case 'add':
{
/* Note: Have to break up prepare statement because https://core.trac.wordpress.org/ticket/12819 */
$addQuery = $wpdb->prepare("INSERT INTO projs (compname,projname,imageurl,sumsmall,sumfull,results,caseid,hide) VALUES (%s,%s,%s,%s,%s,%s,%d," . $caseid . ",%d)",
array($compname, $projname,$imageurl,$sumsmall,$sumfull,$results,$hide));
$message .= $wpdb->query($addQuery)
? 'Successfully added project to the database.'
: 'Error occurred when trying to add project to database: ' . $wpdb->last_error;
break;
}
For some reason, $wpdb->last_error is turning out to be Query was empty, and I can't figure out why. I've looked at other S.O. posts on this topic and they say that an undefined object is being used as the query, but here I'm using $addQuery as the query and I don't see any reason why it is not defined.
Any ideas?
Try using $wpdb->insert instead of $wpdb->query to do an insert into the database.
More information regarding the use of $wpdb can be found here:
https://codex.wordpress.org/Class_Reference/wpdb
The call to ->prepare is failing because you have an error on your query. You have got 8 columns and 8 placeholders plus $caseid that is manually added, and the array you pass to the function contains 7 elements.
You probably have one exceeding %s.