I have two inserts on a processing page. The first works without a hitch. The second will not even activate. I even tried putting a PDO query to see if it would work but still nothing.
$cpinsert = $db->prepare('insert into Chatposts values (0, :chatid, :name, :url, :text, now(), :ipaddress, 0)');
$cpinsert -> bindParam(':chatid', $chatroomid, PDO::PARAM_INT);
$cpinsert -> bindParam(':name', $name, PDO::PARAM_STR);
$cpinsert -> bindParam(':url', $url, PDO::PARAM_STR);
$cpinsert -> bindParam(':text', $text, PDO::PARAM_STR);
$cpinsert -> bindParam(':ipaddress', $ipaddress, PDO::PARAM_STR);
$cpinsert -> execute();
// Needs an error checker
$cpid = $cpinsert ->lastInsertID();
$cpinsert->closeCursor();
^ That works fine, though I don't know about the lastinsertid since I cannot test it.
\V/ Nothing in there will execute no matter what I try. Something is preventing anything there from executing or I didn't close the above's connection properly.
// Targets Insert
//if (isset($target)):
$query = "insert into Targets values (9,'rommel')";
$db->query($query);
$targetinsert = $db->prepare('insert into Targets values (:cpid,:tname)');
foreach ($target as $tname):
$targetinsert -> bindParam(':cpid', $cpid, PDO::PARAM_INT);
$targetinsert -> bindParam(':tname', $tname, PDO::PARAM_STR);
endforeach;
$targetinsert -> execute();
//endif;
I have tried everything I know of and no luck. It is quite possible I did a minor mistake since I am new to PDO. Closecursor didn't seem to do anything either when I added it in.
You said you need to do execute many inserts, but for some reason you are executing your query only once.
Also, please remove these try..catch things from your code - they are the reason why you have no idea what's going wrong
I had to recreate a half functional model of my page but I found the problem, it was lastinsertid(). I was applying a PDOstatement class on a PDO class.
Before:
$cpid = $cpinsert->lastInsertID();
After:
$cpid = $db->lastInsertID();
I just had to call the Database instead of the prepare with that line of code.
Related
I have a mysqli database object $DataBase and the following code works as intended:
$stmt = $DataBase->stmt_init();
$stmt->prepare("UPDATE `optshop_stock` SET quantity_b = ? WHERE product_id = ?;");
$stmt->bind_param('ii', $qty, $sku);
$stmt->execute();
$stmt->close();
But when I add:
echo $stmt->affected_rows;
between $stmt->execute() and $stmt->close() no value is echoed, not even a zero if nothing happened. Am I using this statement in a correct manner?
( I followed this http://php.net/manual/en/mysqli-stmt.affected-rows.php example)
The property can be retrieved at database level (reference here):
echo $DataBase->affected_rows;
I think you should try to add $stmt->store_result() after $stmt->execute(), otherwise you can't get right results when use $stmt->affected_rows
I am calling a stored procedure from PHP and it is supposed to give me a date.
global $dbh;
$stmt = $dbh->prepare("CALL date_sp(?)");
$stmt->bindParam($employee_id, $return_value, PDO::PARAM_STR, 4000);
// Call the stored procedure
$stmt->execute();
I keep getting an error that variable $return_value (in the third line) is not initialized.
Be careful here. bindParam is used only for input parameters. Return values are handled with a different method. You should be doing:
$stmt = $dbh->prepare("CALL date_sp(:employee_id)");
$stmt->bindParam('employee_id', $employee_id, PDO::PARAM_STR, 4000);
// call the stored procedure
$stmt->execute();
Whenever writing code of this sort, be sure to check the API reference very carefully. There's often some subtle things you have to get perfectly right or nothing will work.
If you want to bind result values you can use bindColumn but it's often easier to just fetch it as an array and deal with it directly.
You don't actually need a named placed holder. Using the ? you would just do the following.
global $dbh;
$stmt = $dbh->prepare("CALL date_sp(?)");
$stmt->bindParam(1, $employee_id, PDO::PARAM_STR, 4000);
// call the stored procedure
$stmt->execute();
To actually fetch the result you would do something like:
$result = $stmt->fetch()
I am trying to get this script to run, but I fail on a very simple stupid thing that has kept me getting further for the past 2 hours.
I have written a PDO statement in a function of a class that inserts a bunch of integers in a mysql db.
If I call on the function from a file using
$result->createResult(0, 0, 0,0,0,0,0,"1234",0);
it does not work.
If I use
$result->createResult(1, 1, 1,1,1,1,1,"1234",1);
it runs just fine!
the function that created the mysql entry is
public function createResult($id, $pp_id, $i_id, $ii_id, $cs_id, $ed_id, $em_id, $l_id, $ud_id){
if ($this->databaseConnection()) {
$query_new_result_insert = $this->db_connection->prepare('INSERT INTO results (id, pp_id, i_id, ii_id, cs_id, ed_id, em_id, l_id, ud_id, created_at) VALUES(:id, :pp_id, :i_id, :ii_id, :cs_id, :ed_id, :em_id, :l_id, :ud_id, now())');
$query_new_result_insert->bindValue(':id', $id, PDO::PARAM_INT);
$query_new_result_insert->bindValue(':pp_id', $pp_id, PDO::PARAM_INT);
$query_new_result_insert->bindValue(':i_id', $i_id, PDO::PARAM_INT);
$query_new_result_insert->bindValue(':ii_id', $ii_id, PDO::PARAM_INT);
$query_new_result_insert->bindValue(':cs_id', $cs_id, PDO::PARAM_INT);
$query_new_result_insert->bindValue(':ed_id', $ed_id, PDO::PARAM_INT);
$query_new_result_insert->bindValue(':em_id', $em_id, PDO::PARAM_INT);
$query_new_result_insert->bindValue(':l_id', $l_id, PDO::PARAM_STR);
$query_new_result_insert->bindValue(':ud_id', $ud_id, PDO::PARAM_INT);
$query_new_result_insert->execute();
}
}
this seems to crash on 0 values. if I check (empty($i_id)) it returns true.
I can't get my head around this! Could anyone be so kind to help?
Ok, I finally figured it out...
It wasn't the table structure, but the empty check...
empty returns true on value 0 as described here
Good evening to everyone,
Sorry if my question apperes sily but I'm pazzled by my very trivial problem
In one of the pages of my project I can't concatenate a string containing ' signs
this string can't be concatenated:
$stidR = "INSERT INTO rec_ret_info VALUES('".$rrcode."', ".$modnum.", '".$sdate."', '".$venue."', ".$fac.", ".$date.", ".$sem.")";
but this can:
$stidR = "INSERT INTO rec_ret_info VALUES(".$rrcode.", ".$modnum.", ".$sdate.", ".$venue.", ".$fac.", ".$date.", ".$sem.")";
Apparently if I remove ' signs it works. But i really need them. I really don't know where is the problem. Would be gratefull if you can point me on it.
Can you use a prepared statement to bind a variable?
Connection to Oracle with PDO - More information!
Update
PDO Prepared Statement as an example. The only thing you need to change is the query structure if Oracle is different to MySql in that regard. The binding of variables and the execution will work the same :)
$queryString= "INSERT INTO tablename (ColumnName1,ColumnName2,ColumnName3,ColumnName4,ColumnName5,ColumnName6,ColumnName7) VALUES (?,?,?,?,?,?,?)";
$query = $db->prepare($queryString);
$query->bindValue(1, $variable1, PDO::PARAM_STR);
$query->bindValue(2, $variable2, PDO::PARAM_STR);
$query->bindValue(3, $variable3, PDO::PARAM_STR);
$query->bindValue(4, $variable4, PDO::PARAM_STR);
$query->bindValue(5, $variable5, PDO::PARAM_STR);
$query->bindValue(6, $variable6, PDO::PARAM_STR);
$query->bindValue(7, $variable7, PDO::PARAM_STR);
$query->execute();
simply make echo of your statement like
echo $stidR ; and check the resulting sql, and see what you are doing wrong
I can't get this code to update my, mysql database.
$SQL = $odb -> prepare("UPDATE `LB` SET `running` = `running` + 1 WHERE `url`= :url");
$SQL -> execute(array(":url"=> $url ));
May someone please help, I have searched for this and couldn't find something like this.
Don't do :url in your array, there is no need for it.
You can also use a question mark in place of your =:url like so:
url=?
Then in your array, you can either place a direct value:
$SQL->execute(array($url));
Or you can bind values incrementally:
$SQL->bindValue(1, $url, PDO::PARAM_INT);
$SQL->execute();
Except, instead of using PDO::PARAM_INT, you would use your own parameters...
So I'm guessing in your instance you would use PDO::PARAM_STR
Hopefully this helps :)