Php PDO Not showing Errors


#1

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); 

#2

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;
}

#3

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

… and a huge SQL injection problem.


#4

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.


#5

What are the error settings in your php.ini?


#6

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


#7

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.


#8

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


#9

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

#10

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.


#11

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;
}


#12

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.


#13

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.


#14

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