Hi,
My program works on WAMP (PHP Version 5.5.12) running on my machine. However, the same program will not run on remote server (PHP Version 5.4.32). Basically, I'm trying to insert values obtained from a form into a database. I've checked and rechecked the table I'm inserting into and I can't find anything wrong with it. The trouble is it's hard to debug a PHP program. Unlike, say, C++ which requires a compiler, you can trace the program line by line to look for errors. That's not possible with PHP and makes it so much harder to look for faults.
The relevant functions used to send data to database are shown below. insert() is returning false every time and I can't understand why! I wonder if anyone can suggest where things might be going wrong. I would be very grateful.

public function insert($table, $fields = array())
   { if(count($fields)) //true if $fields holds data
     { $keys = array_keys($fields);

   $values = '';  //to put inside query

   $x = 1;

  foreach($fields as $field)
  { $values .= '?';
    if($x < count($fields))
    { $values .= ', ';
    }
    $x++;
  }
   $sql = "INSERT INTO {$table} (`".implode('`, `', $keys) . "`) VALUES({$values})";
      if(!$this->query($sql, $fields)->error())
      { return true;
      }
     }
     return false;
   }

  public function query($sql, $darams = array())
  { 
    $this->_error = false;   //reset error
    if($this->_query = $this->_pdo->prepare($sql))
    { $x = 1;
      if(count($darams))
      { foreach($darams as $param)
        { $this->_query->bindValue($x, $param);
          $x++;
        }
      }
      if($this->_query->execute())
      { $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
        $this->_count = $this->_query->rowCount();
      }
      else      //an error has occured in SQL query
      { $this->_error = true;
      }
    }
    return $this;
  }

  public function error()
  { return $this->_error;
   }

   public function count()
   { return $this->_count;
   }
    raydona;11044205 wrote:

    Hi,
    Unlike, say, C++ which requires a compiler, you can trace the program line by line to look for errors. That's not possible with PHP and makes it so much harder to look for faults.

    This is not true. xdebug provides those features for php. Remote debugging is also possible. And a lot of php IDEs, such as PhpStorm, Komodo and Eclipse comes with xdebug integration or provide it through plugins.

    It's either that or logging errors. Preferably both though.

      PHP 5.6 also comes with a debugging environment that provides things like line-by-line stepping and breakpoints. It's also available for 5.4/5 as a separate download.

        Why not at least log something where the error is detected?

           $sql = "INSERT INTO {$table} (`".implode('`, `', $keys) . "`) VALUES({$values})";
              if(!$this->query($sql, $fields)->error())
              { return true;
              }
             }
             error_log("Query failed:".PHP_EOL.print_r($this->errorInfo(), 1).PHP_EOL.$sql);
             return false;
           }
        

        (Assumes $this is a PDO type obejct. If not, substitute applicable method for getting the last error.)

          johanafm;11044209 wrote:

          This is not true. xdebug provides those features for php. Remote debugging is also possible. And a lot of php IDEs, such as PhpStorm, Komodo and Eclipse comes with xdebug integration or provide it through plugins.

          I have tried several times in the past to get Eclipse PDT to allow me to step through PHP code executed either via command line or via apache and have found it to be a nightmare. Can you offer any tips or suggest any links that explain step-by-step how to accomplish this? I find the dialogs to set up project debugging options in Eclipse to be baffling. I suppose I mean to suggest that this is not at all easy.

          Weedpacket wrote:

          PHP 5.6 also comes with a debugging environment that provides things like line-by-line stepping and breakpoints. It's also available for 5.4/5 as a separate download.

          Holy cow!! Pretty exciting to see an orthodox debugger. Thanks for posting this.

          As for the original code, it looks like NogDog has the right idea, namely check the error condition by using the class and write the result to a log file. If you want something quicker and dirtier, just add a die(); statement that spits out the error message.

            Hello,
            Many thanks for your help. I have used the following debugging technique.

              [code=php]public function query($sql, $darams = array())
              {
                $this->_error = false;
                if($this->_query = $this->_pdo->prepare($sql))
                { $x = 1;
                  if(count($darams))
                  { foreach($darams as $param)
                    { $this->_query->bindValue($x, $param);
                      $x++;
                    }
                  }
                  if($this->_query->execute())
                  { $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                            $this->_count = $this->_query->rowCount();
                  }
                  else      //an error has occured in SQL query
                  { print_r($this->_query->errorInfo());
                    $this->_error = true;
                  }
                }
                return $this;
              }[/code]

            The message I get is:

            Array ( [0] => 00000 [1] => [2] => ) &#65279;&#65279;&#65279;Array ( [0] => 42S22 [1] => 1054 [2] => Unknown column 'postcode' in 'field list' ) Problem creating an account!

            Previously it said "Unknown column 'address' in 'field list'". Now it's saying "Unknown column 'postcode' in 'field list' ". There is nothing wrong with the fields in the database. I've checked them a hundred times. There is something wrong with the database. Could this be a problem with the server rather than my coding? Please help?

              We are not privy to your other code, but it looks like the function insert is receiving an array which has some key (postcode) which does not correspond to one of the columns in the table you have specified. See how your function takes two parameters:

              public function insert($table, $fields = array())
               {
                // blah blab blah
              }
              

              The first parameter is the table name (and we have no idea what this is). The second parameter is some associative array that has a bunch of values in it. Your code assumes that EVERY key in that array corresponds to some column the table you specify in $table.

                4 days later

                Hi,

                Why not at least log something where the error is detected?

                $sql = "INSERT INTO {$table} (".implode(', ', $keys) . ") VALUES({$values})";
                if(!$this->query($sql, $fields)->error())
                { return true;
                }
                }
                error_log("Query failed:".PHP_EOL.print_r($this->errorInfo(), 1).PHP_EOL.$sql);
                return false;
                }

                When I do that this is the message I receive:

                500 - Internal server error.
                There is a problem with the resource you are looking for, and it cannot be displayed.

                I have played around with my code. I have changed the code for the function that hashes the password using salt from:

                public static function salt($length)
                       {
                         return mcrypt_create_iv($length);
                       }

                to:

                public static function salt($length)
                       {
                         $intermediateSalt = md5(uniqid(rand(), true));
                         return(substr($intermediateSalt, 0, $length));
                       }

                Result is my program works on a remote server with operating system linux but not on a remote server using windows operating system, my program is intended for latter. Can anyone please help.

                  sneakyimp;11044231 wrote:

                  I have tried several times in the past to get Eclipse PDT to allow me to step through PHP code executed either via command line or via apache and have found it to be a nightmare. Can you offer any tips or suggest any links that explain step-by-step how to accomplish this?

                  I've never setup Eclipse myself. But looking at a guide for it, it seems rather similar to PhpStorm setup. I'll create a separate thread so as not to side-track in this one.

                  raydona;11044279 wrote:

                  When I do that this is the message I receive:

                  Then you should turn on error logging and check your error log. It will tell you what is wrong.

                  raydona;11044279 wrote:

                  Result is my program works on a remote server with operating system linux but not on a remote server using windows operating system, my program is intended for latter. Can anyone please help.

                  Well, considering that

                  Array ( [0] => 00000 [1] => [2] => ) &#65279;&#65279;&#65279;Array ( [0] => 42S22 [1] => 1054 [2] => Unknown column 'postcode' in 'field list' ) Problem creating an account!

                  you might want to add the actual sql statement that led to this error, i.e.

                  else {
                      print_r($this->_query->errorInfo());
                      echo $sql;
                      $this->_error = true;
                  }
                  

                  Although I'd recommend NOT echoing errors. Use [man]error_log[/man] instead so that your server settings will determine what is displayed and what is logged.

                  You may also want to make sure you stick to all lower-case letters in sql identifiers when using mysql on separate platforms.

                    Write a Reply...