Php PDO Not showing Errors

Hi folks,

Strange, my php scripts only showing up php errors and when encounter pdo errors it shows internal server error in console. This is an ajax post btw, so how do i enable the server to show pdo errors as well along php errors ?

adding set attribute does not help either :slight_smile:

<?php
error_reporting(E_ALL);
error_reporting(-1); 
ini_set('display_errors', 'true');

include ($_SERVER["DOCUMENT_ROOT"] . "/config.php");
require_once($_SERVER['DOCUMENT_ROOT'] . BASE_PATH ."/admin/includes/pdo_connection.php");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

Actually the error appers when it tries to execute this part, but i only see internla server error.

/check already exisit
$query="SELECT * 
        FROM packages 
		  WHERE name='$name';
$result = $db->query($query);
$row_count = $result->rowCount();
if($row_count>0){
  echo "<div class=\"alert alert-danger\">Package Already Exist.</div>";
  exit;
}

As can be seen from the code highlighting, you have a missing quote mark.

… and a huge SQL injection problem.

2 Likes

Thanks i got it. But why the error was not displayed? i wasted lot of time trouble shooting. if it had tell about a missing quote , i would fixed instantly. btw, ill chec for the sql injection issue also. Thanks. pls help me display errors.

What are the error settings in your php.ini?

If you used a proper IDE it would have been as obvious as it shows in your post.

1 Like

I agree, but it also ties in with logical thinking as well. Even if you don’t have an IDE such as myself because I prefer lite weight text editors like Sublime Text, you have to do logical thinking and look at each line. Ask yourself if it makes sense. Then continue to the next line. Repeat this process until you have gone through all the lines in that file. This is just a few techniques I use in lite weight text editors like Sublime.

2 Likes

I agree back. Problem is that it can take time for noobs to develop that logical thinking.

1 Like

i just added the double quote where dormilich mentioned. well, until then the program was showing up internal server error. but now i see php errors after that. i wonder why php did not tell me about the missing double quote while it is showing up other errors.

**Notice** : Undefined variable: day17 in  **/home/accessto/public_html/demo3/admin/content/dashboard/pages/client_admin/packages/add/add.php** on line  **87**

**Fatal error** : Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'price' in 'field list'' in /home/accessto/public_html/demo3/admin/content/dashboard/pages/client_admin/packages/add/add.php:88 Stack trace: #0

i am using blue fish in ubunto. even after fixing the double quote, there is no color changes in coding. so the editor is not smart enought to detect errors.

Try this:

<?php 
declare(strict_types=1);
error_reporting(-1); 
ini_set('display_errors', 'true');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

require ($_SERVER["DOCUMENT_ROOT"] . "/config.php");
require_once($_SERVER['DOCUMENT_ROOT'] . BASE_PATH ."/admin/includes/pdo_connection.php");


//check already exists

// ESSENTIAL: ONLY LINEFEED AFTER  ____TMP
$query = <<< ____TMP
  SELECT 
    * 
  FROM 
    packages 
  WHERE 
    `name`= "$name";
____TMP;
// ESSENTIAL: ONLY SEMICOLON AFTER ____TMP
echo $query;

$result = $db->query($query);
$row_count = $result->rowCount();
if($row_count>0){
  echo '<div class="alert alert-danger">Package Already Exist.</div>';
  exit;
}

Parsing errors such as missing (or too many) closing quotes or curly-brackets are quite difficult to pin down, how would it know where your close-quote should have been? It can’t presume that the next time it finds a newline should have a quote, nor a semi-colon, there’s literally nothing it can presume, even the closing ?> PHP tag might be in some text. I find it will do the best it can, but no more than that.

All you can do is develop a “nose” for strange error messages and yes, use a text editor that does syntax highlighting. I use notepad++ on Windows, and that does show when a quote is opened but not closed, and will “pair” open and close brackets.

Remember that until you added the correct close-quote, there were no errors in your PHP code after that (as far as the server is concerned) because that wasn’t PHP code, it was quoted text. So none of it was being interpreted or syntax-checked.

Then I suggest choosing a different text editor. Atom is completely free and there’s Sublime Text as well. Though you have to pay for Sublime to remove the annoying purchase alert that pops up every so often.

Are you sure you have the document language set to PHP?

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.