Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I resolve errors in my SQL queries?
#1
Errors are inevitable. They assail all of us and can, at times, be caused by circumstances outside our control—database crashes, database upgrades, downtime for maintenance, and so on. If something goes wrong when you’re trying to deal with PHP and SQL together, it’s often difficult to find the cause. The trick is to get PHP to tell you where the problem is, bearing in mind that you must be able to hide this information from visitors when the site goes live.


Quote:$country = 'USA';
$dbh = new PDO($dsn, $user, $password);
$sql = 'Select * from cities where CountryCode =:country';
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':country', $country, PDO:TongueARAM_STR);
$stmt->execute();
$code = $stmt->errorCode();
if (empty($code))
{

proceed to fetch data
}
else
{
echo 'Error with the database: <br />';
echo 'SQL Query: ', $sql;
echo '<pre>';
var_dump($stmt->errorInfo());
echo '</pre>';
}
The default error mode sets the errorCode property of the PDOStatement object, but does nothing else. As you can see in this example, you need to check the error code manually to ascertain whether or not an error was found—otherwise your script will happily continue on its merry way.
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  Logic Errors in PHP ? beingchinmay 1 3,228 07-15-2020, 11:56 AM
Last Post: RH-Calvin
  Environment Errors beingchinmay 0 2,441 07-02-2016, 03:50 PM
Last Post: beingchinmay

Forum Jump:


Users browsing this thread: 1 Guest(s)