My problem is kinda simple or it maybe a silly mistake from my side. But I don't know I am just getting unexpected results while using PDO in php.
Code goes like this,
try
{
$_pdo = get_pdo_instance();
$_pdo->beginTransaction();
//query 1
$_pdo->query("some query"); // I have error in query 3 but this query 1 is still executed.
//query 2
$_pdo->query("some query"); // only executes when there are no errors.
//query 3
$_pdo->query("some wrong query"); // let's say I have an error in this sql
$_pdo->commit();
}
catch(Exception $ex)
{
$_pdo->rollback();
}
I am explaining the problem now,
In given example I have some sql error in query 3, so none the queries should run as they all belongs to single transaction.
But in my case query 1 always run even if there are errors in that try block.
Maybe its something simple but I have no idea why this is happening.
Edit:
Function definition,
function get_pdo_instance()
{
try
{
$conn = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
die('ERROR: ' . $e->getMessage());
}
return $conn;
}
Exceptions are used only for PHP errors. In the code above, you're trying to catch a MySQL error. When PHP reads your try block, everything looks fine, and it therefore has no exception to catch.
You can do error checking with PDO and MySQL like:
if(!$_pdo->query("some query")) {
// Do something
}
See the documentation for PDO Error Handling. You need to turn exception raising on.
try {
$_pdo = get_pdo_instance();
$_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$_pdo->beginTransaction();
$_pdo->query("some query");
$_pdo->query("some query");
$_pdo->query("some wrong query");
$_pdo->commit();
}
catch(Exception $ex) {
$_pdo->rollback();
}
Here is my solution,
I was catching Exception, instead in this case I had to do catch PDOException.
I guess that solved my problem, but I haven't tested this thing completely. I will post updates if any.
Related
RollBack () and beginTransaction() not work in my PHP PDO and my table type is innoDB. In the following code my $sql1 is correct and my $sql2 is wrong (I added d to $last_id to just make it wrong). But it still executes sql1 meaning roll back no effect. Thank you for your advice.
<?php
include 'connect.php';
// Get multiple input field's value
try {
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Starts our transaction
$conn->beginTransaction();
foreach ($_POST['phone'] as $value) {
$sql1 = "INSERT INTO tbl_contact_info (type)
VALUES ('$value')";
// use exec() because no results are returned
$conn->exec($sql1);
$last_id = $conn->lastInsertId();
$sql2="INSERT INTO tbl_img (img_type)
VALUES ('$dlast_id')";
$conn->exec($sql2);
}
// Commits out queries
$conn->commit();
echo "New record created successfully";
}
catch(PDOException $e)
{
// Something borked, undo the queries!!
$conn->rollBack();
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
First of all (and sorry it it's obvious but I realised it's not always clear to everyone) SQL and PHP are different languages. MySQL Server will not react to Undefined variable notices triggered in PHP code.
Secondly, notices are not exceptions so they cannot be caught with try/catch statements. (You can certainly write a custom error handler that will throw exceptions on errors but it doesn't seem to be the case here.)
I have a transaction that does an insert into several tables. I want to catch any errors so that if any of the inserts fail they will all be rolled back. Here is a short version of what I am trying to do:
try {
$this->conn->beginTransaction();
//build an sql insert...
$sql = "INSERT INTO...
$stmt = $this->conn->prepare($sql);
$stmt->execute();
//do more inserts...
//commit the transaction
$this->conn->commit();
$this->message = 'The Program has been added.';
//catch errors and rollback
} catch (PDOException $e) {
$this->message = $e->getMessage();
$this->conn->rollBack();
}
All of the inserts work properly but if there is a problem, I don't seem to be catching the error and the rollback does not happen. For instance, if I write bad sql or don't pass values for parameters, I just get php errors but the inserts proceed anyway. I can post the full code if it helps but maybe it is something obvious?
This day is my first time using PDO. And when I try 'INSERT' with PDO, it does nothing.
This is my code :
session_start();
if($_GET['feedback']!="")
{
$fb = $_GET['feedback'];
$date = date('Y-m-d H:i:s');
$query = "INSERT INTO feedback(number, sender, content, date) VALUES (NULL,?,?,?)";
$STH = $DBH->prepare($query);
$STH->bindParam(1, $_SESSION['alias']);
$STH->bindParam(2, $fb);
$STH->bindParam(3, $date);
$STH->execute();
}
I have tried a 'SELECT' query with PDO and it works.
What should I do?
why dont you try and enclose your code in try and catch blocks to get any exceptions:
try {
//your code
} catch( PDOEXception $e ) {
echo $e->getMessage(); // display error
exit();
}
This should give you a clue whats going wrong.
What should I do?
Error reporting.
Check tag wiki for the example.
It is essential to set ERRMODE_EXCEPTION as a connection option as it
will let PDO throw exceptions on connection errors. Setting
ATTR_DEFAULT_FETCH_MODE is also a good idea.
Please note that connection errors shouldn't be catched, at least not
using try..catch operator. A custom exception handler could be used,
but not required. Especially for new users, it is okay to have
unhandled exceptions, as they are extremely informative and helpful.
Here's PHP code that I'm using:
$query="select * from `myTable` where `email`='$email' limit 0,1";
if(empty($conn))
{
echo "not connected".PHP_EOL;
}
$result = mysql_query($query,$conn);
$row = mysql_fetch_array($result);
if(empty($row))
{
....
When the query is executed in phpmyadmin, I get a single row selected.
However, when I execute the code in php, the row is always empty.
The same goes for several other queries that I've tried to execute. mysql_query always fails.
What could be wrong?
I do not feel there is enough of the code to see what is going on. But based on just what you are showing us, after you get the $result and assign it to $row you have a if statement
if(empty($row)) {...doing something secret...}
which means if something was returned like the row you are expecting NOTHING would happen because (empty($row)) would be false and not execute.
Try this using PDO:
<?php
$email = "example#example.com";
try {
//Instantiate PDO connection
$conn = new PDO("mysql:host=localhost;dbname=db_name", "user", "pass");
//Make PDO errors to throw exceptions, which are easier to handle
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//Make PDO to not emulate prepares, which adds to security
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$query = <<<MySQL
SELECT *
FROM `myTable`
WHERE `email`=:email
LIMIT 0,1;
MySQL;
//Prepare the statement
$stmt = $conn->prepare($query);
$stmt->bindParam(":email", $email, PDO::PARAM_STR);
$stmt->execute();
//Work with results
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
//Do stuff with $row
}
}
catch (PDOException $e) {
//Catch any PDOExceptions errors that were thrown during the operation
die("An error has occurred in the database: " . $e->getMessage());
}
Using mysql_* functions is highly discouraged. It's a guarantee to produce broken code. Please learn PDO or MySQLi from the links in the comment I gave you, and use those instead.
First, confirm $email's value. Echo it right before defining $query to make sure it's what you think it is.
If you've already done that, then you know that's the problem--instead, it's likely that your link identifier $conn is the problem. Instead of using a link identifier, try leaving the second parameter of your query empty, and instead run mysql_connect() at the beginning of your script. That's the best way to do things 99.5% of the time.
See: http://php.net/manual/en/function.mysql-connect.php
Using SQLite in PHP (thus using PDO), I have this code:
try {
$db = new PDO("sqlite:C:\Program Files\Spiceworks\db\spiceworks_prod.db");
echo "Done.<br /><b>";
$query = "SELECT id FROM Devices LIMIT 5";
echo "Results: ";
$result = $db->query($query);
while ($row = $result->fetchArray()) {
print_r($row)."|";
}
}
catch(PDOException $e) {
echo $e->getMessage();
}
But that does not print out any data from the SQL. I know the database has data in it and the connection is valid. If I change the query to say:
$query = "SELECT BLAHid FROM FakeDevices LIMIT 5";
Nothing changes. Nothing from SQL gets printed out again, and I see no errors even though this is clearly an invalid SQL query.
In both situations, the "Done" and "Results" gets printed out okay. How can I print out SQL errors, like if the query is invalid?
You need to tell PDO to throw exceptions. You can do that by adding the following line after you connect to the database:
$db = new PDO("sqlite:C:\Program Files\Spiceworks\db\spiceworks_prod.db");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
That way you can catch all exceptions except for a possible problem with the first line, the database connection itself.