How to View Query Error in Pdo PHP

Can someone tell me the error in PDO update query

I made change on my code, now it's working perfectly.

Thank you all for your contribution.

working code is :

<?php

if (isset($_POST['save'])) {

$sql = "UPDATE user SET username = :username,
password = :password,
firstname = :firstname,
lastname = :lastname
WHERE user_id = :user_id";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':username', $_POST['username'], PDO::PARAM_STR);
$stmt->bindParam(':password', $_POST['password'], PDO::PARAM_STR);
$stmt->bindParam(':firstname', $_POST['firstname'], PDO::PARAM_STR);
// use PARAM_STR although a number
$stmt->bindParam(':lastname', $_POST['lastname'], PDO::PARAM_STR);
$stmt->bindParam(':user_id', $_GET['id'], PDO::PARAM_INT);
if($stmt->execute()){
header ("Location: user.php");

}
}
?>

How to catch exact sql error in php pdo statement

You can check any error after execute query with following method.

$selectQueryResult1->execute();
$arr = $selectQueryResult1->errorInfo();
print_r($arr);

PDO error message?

Try this instead:

print_r($sth->errorInfo());

Add this before your prepare:

$this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );

This will change the PDO error reporting type and cause it to emit a warning whenever there is a PDO error. It should help you track it down, although your errorInfo should have bet set.

Error Checking for PDO Prepared Statements

I preffer setting the error mode to throwing exceptions like this:

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

right after I connect to the database. So every problem will throw an PDOException
So your code would be:

$selectQuery = '
SELECT
' . implode($allFields, ', ') . '
FROM
People
WHERE
' . $fieldName . ' = :value
';
try
{
$selectQueryResult = $db->prepare($selectQuery);
selectQueryResult->bindParam(':value', $fieldValue);
$selectQueryResult->execute();
}
catch(PDOException $e)
{
handle_sql_errors($selectQuery, $e->getMessage());
}

where the function would be:

function handle_sql_errors($query, $error_message)
{
echo '<pre>';
echo $query;
echo '</pre>';
echo $error_message;
die;
}

In fact I am using a general function that also has something like

$debug = debug_backtrace();
echo 'Found in ' . $debug[0]['file'] . ' on line ' . $debug[0]['line'];

to tell me where was the problem if I am running multiple queries

PHP doesn't display PDO error even if I set error handling

Probably you have error reporting turned off and you are not trying to catch an exception if your code throws one.

So you have to put your code into a try and catch block e.g.

try {
//your stuff
} catch(PDOException $e) {
echo $e->getMessage();
}

Because your PDO is set to throw an exception if an error occurred, so you have to catch that with this block. Also I would recommend you to turn on error reporting for useful error messages (Only while staging never in production).

How to debug PDO database queries?

Looking in the database log

Although Pascal MARTIN is correct that PDO doesn't send the complete query to the database all at once, ryeguy's suggestion to use the DB's logging function actually allowed me to see the complete query as assembled and executed by the database.

Here's how:
(These instructions are for MySQL on a Windows machine - your mileage may vary)

  • In my.ini, under the [mysqld] section, add a log command, like log="C:\Program Files\MySQL\MySQL Server 5.1\data\mysql.log"
  • Restart MySQL.
  • It will start logging every query in that file.

That file will grow quickly, so be sure to delete it and turn off logging when you're done testing.

PDO Transaction: how to know which queries caused an error?

try
{
$dbh->beginTransaction();

try
{
$dbh->exec($query1);
}
catch (PDOException $e)
{
throw new MyNewExtendedPdoException('first query exception'); // or just simply rollback + message
}

try
{
$dbh->exec($query2);
}
catch(PDOException $e)
{
throw new MyNewExtendedPdoException('2nd query exception');// or just simply rollback + message
}

$dbh->commit();
}
catch (MyNewExtendedPdoException $ex)
{
$dbh->rollBack();
echo $ex->getMessage();
}

it can be done on various ways that is one of this way. You don't have to create your own exception class you can rollback your transaction in every try catch {} block then it will be simpler.

PHP PDO MYSQL/MARIADB How to query a view? (view not found error)

This works without a problem, so you should check for typos or check in your database, if the view was created

<?php
$stmt = $pdo->query("SELECT * FROM view_name");
while ($row = $stmt->fetch()) {
echo $row['name']."<br />\n";
}

See example



Related Topics



Leave a reply



Submit