1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Debugging PHP?

Discussion in 'PHP' started by HenryCan, May 19, 2017.

  1. #1
    The hosting service I've been using has recently upgraded its servers and facilities and now my code doesn't work as well as it once did. The PHP level went from 5.x to 7.1.4 and I think that may be why things are misbehaving now.

    I have never been a terribly fluent PHP coder - I'm more of a Java guy - but I feel like it is probably time for me to learn how to debug my own code. I use Eclipse for my Java code and my HTML development but I don't have a PHP editor installed so I suppose I need to know how to add PHP support to it first. Or maybe I should have a separate copy of Eclipse exclusively for my PHP code. I'd be grateful for some advice from someone in roughly my position on that point.

    I've done a bit of googling and found that there seem to be two main debuggers for PHP, XDebug and Zend, as well as some standalone products like PHPed. I watched a video of a guy debugging with PHPed and it seemed simple enough but I'm guessing it would be better to use XDebug or Zend. Can someone explain which debugger is best for PHP 7.x code and why?

    My code is actually on my hosting services servers and much of it makes use of MySQL. In fact, that's my main reason for getting PHP involved: it goes after data in my database and then displays reports based on that data on my website. Therefore, I want a debugger that will be able to handle PDO calls to my databases, not just routine non-database statements.

    I'm eager to hear your recommendations based on your far more extensive knowledge of PHP.
     
    HenryCan, May 19, 2017 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    There is absolutely no reason to use a dedicated debugger for PHP (not really). Turn on error reporting, view server logs, and make sure you have proper error handling in the code.

    There shouldn't really be any reason why PHP code should stop working between versions, if the code is using proper, updated methods.
     
    PoPSiCLe, May 19, 2017 IP
  3. SpacePhoenix

    SpacePhoenix Well-Known Member

    Messages:
    196
    Likes Received:
    28
    Best Answers:
    2
    Trophy Points:
    155
    #3
    Do you currently use PDO for connection/interacting with the database?
     
    SpacePhoenix, May 19, 2017 IP
  4. HenryCan

    HenryCan Member

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #4
    <Shrug>. My code worked perfectly before. Now it doesn't.</Shrug>

    Can you point me to a tutorial or manual that tells me how to turn on error reporting, write/view server logs and write proper error handling?
    I put my PDO calls in try/catch blocks but maybe I need to do more. I don't think I ever turned on error reporting before; I just figured out from the behaviour of the code what was going wrong. I don't think I've ever used server logs either, although I'm not sure the hosting service will let me access those if they belong to the service. If I can write my own server logs though, then great, I'd love to look at them.

    I would have expected my old code to work fine with a newer version of PHP with, perhaps, some techniques no longer being the preferred approach because better ones had come along. But some of my code isn't working at all and some of the stuff that does work generates messages that don't make much sense to me. And some of it just doesn't work at all. For instance, I have an input form that should be getting turned into a new record in the database: when I press Enter, no error messages appear on the screen (I've got extensive edits for the data) and it just eventually craps out on a HTTP 504 Gateway Timeout. I have no idea why that's happening.

    I hope none of this sounds snarky; I really am trying to learn what is wrong. I'm not wedded to the idea of using a debugger, I just want to figure out what is wrong so that I can fix it.
     
    HenryCan, May 20, 2017 IP
  5. HenryCan

    HenryCan Member

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #5
    Yes, I am doing all my database access via PDO.
     
    HenryCan, May 20, 2017 IP
  6. lulzicon

    lulzicon Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    You can look at PDOException class http://php.net/manual/en/class.pdoexception.php
    This is the __construct function of my own Dbcnx class
    
    
      private function __construct()
      {
          try
          {
          $this->dbcnx = new PDO("mysql:host=$this->hostname;dbname=$this->db", $this->username, $this->password, array(PDO::ATTR_PERSISTENT => true));
          $this->dbcnx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);  //SETS ERROR MODES TO REPORT
          }
        catch(PDOException $e)
          {
          $e->getMessage();  // THAT WILL PRINT ANY PDO ERRORS
          }
       }
    
    PHP:
    And you can set error reporting to 'on' in your site like so:
    
    <?php 
    ini_set('display_errors',1);
    error_reporting(E_ALL);
    
    PHP:
    Just put that code at the very top of your page. Even before your DOCTYPE tag. And remember to remove it when your done. If you don't the trolls will eat your website.

    As for the problem, my guess is you're using a lot of deprecated functions and your site is slowing down because of errors being written to your log file. You should be able to check those log files in your hosting control panel. Just look for something that says error logs, or Apache2 log file or something of the such.

    What you can do is request that your hosting provider revert back to php5 on your site. Most will do that if you ask.

    As for PHP debuggers... I've never had PHP (with error_reporting turned on) give me an error that didn't point me to the exact spot where I messed up. Even the Eclipse plugin for PHP really doesn't do anything more than PHP's error_reporting function.

    Hope that gets you a little closer to solving the issues.

    JohnP
     
    lulzicon, May 21, 2017 IP
  7. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #7
    You can turn on error reporting like this:

    error_reporting(E_ALL);
    or a mix like this:

    error_reporting( E_ALL & E_PARSE & ~E_NOTICE );

    But anyways, I don't think that this is the problem. Your code worked earllier.
    Try this instead:

    Login to your CPanel
    Click the link that says PHP version
    Here select PHP 7 in the drop box and save

    I think that'll work...
    If I'm right, the errors are only on the php pages where you connect to the database... This should fix it...
     
    JEET, Jun 1, 2017 IP
  8. gallitin

    gallitin Well-Known Member

    Messages:
    722
    Likes Received:
    33
    Best Answers:
    2
    Trophy Points:
    165
    #8
    It depends on how PHP was upgraded. I assume the old version was completely removed and php 7 was then installed. Reverting all previous changes back to default. In my experience upgrading I've had the same happen and it ended up being that the php shortcode setting in the php.ini file was turned to OFF. Which means anywhere you have php code starting with <? vs <?php it breaks. I'd start there.
     
    gallitin, Jun 2, 2017 IP
  9. linkstraffic

    linkstraffic Well-Known Member

    Messages:
    388
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    133
    #9
    XDebug is a reference, I use it sometimes to debug my zend projects, but you can implement it on any type of projects, I've explained that in a short tutorial but I was refering to NetBeans, on Eclipse, that will be similar, you may need to google further...
     
    linkstraffic, Jun 26, 2017 IP