I created this function to update or insert a row based on if a row already exist, or not; the error I am getting is VAR1 is not a variable. This is my first time setting up a merge in this fashion on a iSeries and DB2. Hope someone can help, thanks to all that takes the time to do so.
function mergeRecord($var1, $var2, $var3, $var4, $var5, $var6){
db2RunSql("
MERGE INTO schema.tableName AR
USING (VALUES (:var1, :var2, var3, :var4, :var5, :var6)
FOR :var1 ROWS)
AS AC (VAR1, VAR2, VAR3, VAR4, VAR5, VAR6)
ON (AR.VAR1 = AC.VAR1 AND AR.VAR4 = AC.VAR4 AND AR.VAR6 = AC.VAR6)
WHEN MATCHED THEN UPDATE SET VAR1=?, VAR2=?, VAR3=?, VAR4=?, VAR5=?, VAR6=?
WHEN NOT MATCHED THEN INSERT (:var1, :var2, :var3, :var4, :var5, :var6)
VALUES (?, ?, ?, ?, ?, ?)",
array($var1, $var2, $var3, $var4, $var5, $var6)
);
}
Maybe you are looking for something like this:
function mergeRecord($var1, $var2, $var3, $var4, $var5, $var6){
db2RunSql("
MERGE INTO schema.tableName AR
USING (VALUES (?, ?, ?, ?, ?, ?))
AS AC (VAR1, VAR2, VAR3, VAR4, VAR5, VAR6)
ON (ar.var1, ar.var4, ar.var6) = (ac.var1, ac.var4, ac.var6)
WHEN MATCHED THEN
UPDATE SET (ar.var2, ar.var3, ar.var5) = (ac.var2, ac.var2, ac.var5)
WHEN NOT MATCHED THEN
INSERT VALUES (ac.var1, ac.var2, ac.var3, ac.var4, ac.var5, ac.var6)",
array($var1, $var2, $var3, $var4, $var5, $var6)
);
}
I think you are trying to insert too many parameter markers in your example when you could simply be using columns in the source (USING) table as update or insert values.
NOTE: This is just a guess as I am not sure what db2RunSql() does. It is not a standard function, and you have not provided a definition of it.
Related
I have the following query which should insert a 0 but instead inserts 1.
It is executed with prepared statements in PHP:
$insertsql=
"INSERT INTO table1 (column1, column2, column3, column4, column5, column6)
VALUES (?, ?, ?, ?, ?, ?)";
$insertstmt = $pdo->prepare($insertsql);
$insertstmt->execute(array($var1, $var2, $var3, $var4, $var5, 0));
All inserts are performed fine, except a 1 is inserted in column6 instead of a 0.
Column 6 has the datatype BIT.
The query works fine, however, when executing
INSERT INTO table1 (column6) VALUE (0);
directly in the phpmyadmin 'MySQL' tab.
What am I missing?
EDIT:
Using
$insertstmt->execute(array($var1, $var2, $var3, $var4, $var5, false));
works as expected. Why is 0 working directly within a SQL query but not when using pdo to execute it?
You need to explicitly declare this parameter as Boolean when passing it to PDO.
From the PDO execute documentation :
input_parameters
An array of values with as many elements as there are bound parameters in the SQL statement being executed. All values are treated as PDO::PARAM_STR.
This is not what you want.
So you would need to change your logic to avoid using the execute(array()) construct and bind each parameter individually, using bindValue(). For boolean :
$insertstmt->bindValue(':col6', false, PDO::PARAM_BOOL);
Since booleans are just TINYINT(1) in MySQL, PDO::PARAM_INT should work fine as well :
$insertstmt->bindValue(':col6', 0, PDO::PARAM_INT);
Finally : if this boolean value will always be false, then you may pass it directly to the query, like :
$insertstmt->execute(array($var1, $var2, $var3, $var4, $var5, false));
As wisely commented by spencer7593, another option is to do the type casting from within the SQL. For example one could convert the string to a bit value with :
INSERT INTO table1 (column1, column2, column3, column4, column5, column6)
VALUES (?, ?, ?, ?, ?, IF( ? ='0', b'0', b'1') )
I am still learnig using php, and I got some problems.
So, I have a database named dbcoma and these tables;
pic of tables and the data
here is my question if I wanna make a registration for patient using php, what is the correct script?
I tried this one but it wasn't working (cant execute)
public function createPasien($id_alat, $id_patient, $passwordpas, $namepas, $age, $datein, $id_hospital, $id_alat){
$password = md5($passwordpas);
$stmt = $this->con->prepare("INSERT INTO `dbcoma`.`patient` (`id_patient`, `passwordpas`, `namepas`, `age`, `datein`, `id_alat`, `id_hospital`) VALUES (?, ?, ?, ?, ?, ?, ?);");
$stmt->bind_param("sssssss", $id_patient, $passwordpas, $namepas, $age, $datein, $id_alat, $id_hospital);
$stmt->execute();
// …
}
UPDATE: I FIXED IT AND FOUND THE PROBLEM. THANK YOU
There are some points to keep in mind:
Check the response return by execute() statement.
Echo the query and run it on the mysql terminal and see what happens.
While dealing with foreign key the value you are trying to insert must present in the master table, only then you can insert it in child table.
I need to convert an existing project from mysql to mysqli and using prepared statement.
In the existing project there are queries that uses repeated variable values.
One such example is this: where the $prev_yr is used 3 times.
$sqlins = "Insert into studentclass (`StudentID`, `ClassID`, `Year`, `Level`, `SNo`, `TermList`, `DateStart`, `DateEnd`)
select StudentID, '$prev_cl', '$prev_yr', '$prev_lvl', '', '123456789', '$prev_yr-01-01', '$prev_yr-12-31' from student Where StudentID in ($ids) ";
Is there a better method than this:
$sqlins = "Insert into studentclass (`StudentID`, `ClassID`, `Year`, `Level`, `SNo`, `TermList`, `DateStart`, `DateEnd`)
select StudentID, '?', '?', '?', '', '123456789', '?-01-01', '?-12-31' from student Where StudentID in (?) ";
$stmt = $mysqli->prepare($sqlins);
$stmt->bind_param("ssssss", $prev_cl,$prev_yr,$prev_lvl,$prev_yr,$prev_yr,$ids);
$stmt->execute();
I am wondering if there is a way of binding the $prev_yr once for all 3 occurrences.
Because there are other queries that may have 2 occurrences of $prev_lvl, 5 occurrences of $prev_yr etc in one statement. The idea is that when the repeated occurrences of multiple variables becomes many in a statement - it becomes quite confusing to arrange them in the bind_param.
Any solution?
Thank you.
Does it even work like that, typical you wont't do this '?-01-01' in a query. I haven't used Mysqli, in about 4 years, as all I use now a days is PDO. But as far as I know when you send that to prepare it's gonna puke on the ? being in a string.
I would split it, there actually is no real need to do the select because the only thing being selected is the studentID which you already have. Simply
$insert = $mysqli->prepare("Insert into studentclass (`StudentID`, `ClassID`, `Year`, `Level`, `SNo`, `TermList`, `DateStart`, `DateEnd`)VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
foreach( $ids AS $id ){
$stmt->bind_param("issssiss", $id, $prev_cl,$prev_yr,$prev_lvl,'', '123456789', $prev_yr.'-01-01',$prev_yr.'-12-31');
$stmt->execute();
}
I can't test it so hopefully I got everything in the right place.
As I said I don't think you can bind to the Fields part of the query and certainly not inside a partial string, besides it's making a select that is un-needed. Just make sure to prepare the insert before the loop.
Just to clearly the only thing that select actually gets from the DB is this
select StudentID ... from student Where StudentID in (?)
The rest are added in as "fake" columns, I don't know the term for it. It's difficult to read the original query..
I am wondering if there is a way of binding the $prev_yr once for all 3 occurrences.
No.
Besides, it wouldn't work this way anyway, as you cannot bind just an arbitrary query part of your choice. You can bind a complete data literal only. Means instead of '?-01-01' it should be just ?, whereas in your PHP code you should make it
$dateStart = "$prev_yr-01-01";
and then bind this variable for the whole value. So there will be no more repeating variables.
I cannot for the life of me figure out why this prepared statement isn't working.
$thisInsert = $db->prepare("INSERT INTO conversations (person_a, person_b, exchange_count, inbox) values(?, ?, ?, ?)");
$thisInsert->bind_param('iiii', $activeUser, $passiveUser, 1, 1);
$thisInsert->execute();
Values are bound by reference, not by value; so you can't bind a value like 1, only something like a variable containing the value that you want to bind
Quoting from the manual (my emphasis)
Note that mysqli_stmt_bind_param() requires parameters to be passed by reference
This is my code :
It gives no error when i change the array to index type instead of associative.
But the moment i change it back to associative it starts giving error.
Any help on this ?
$dbh=new PDO("mysql:host=localhost;dbname=sj_db", 'root', '');
$entryData = array(
"Team Name"=>$_POST['name']
, "Won"=>$_POST['w']
, "Lost"=>$_POST['l']
, "Draw"=>$_POST['d']
, "Points"=>$_POST['p']
);
$sql="INSERT INTO fb (`Team Name`, Won, Lost, Draw, Points) VALUES (?, ?, ?, ?, ?)";
$sth=$dbh->prepare($sql);
//$sth->execute($entryData[`Team Name`],$entryData['Won'],$entryData['Lost'],$entryData['Draw']
// ,$entryData['Points']);
$sth->execute($entryData);
//$sth->closeCursor();
Placeholders in your query are positional (?) ones.
Either change them to named (:name)
or pass array_values($entryData) into execute
Though you have to remove a space from Team Name key in order to use named placeholders