Page 1 of 1

Works on WAMP but not on remote server

Posted: Wed Dec 10, 2014 7:31 pm
by raydona
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.

Code: Select all

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

Re: Works on WAMP but not on remote server

Posted: Wed Dec 10, 2014 8:07 pm
by requinix
Have you tried tracking the error message? If the query itself failed then there will be an error message you can surface to your code to find out why.

If not the query then the only other way you could get false is if $fields is empty. Checked that yet?

Re: Works on WAMP but not on remote server

Posted: Thu Dec 11, 2014 1:19 pm
by Christopher
The only way insert() can return false is if $this->_query->execute() fails. Maybe do a print_r($this->_query->errorInfo()) if it fails to see the database error?

PS - There are debuggers for PHP. See xdebug.

Re: Works on WAMP but not on remote server

Posted: Thu Dec 11, 2014 5:58 pm
by raydona
Hello,
Many thanks for your help.
I have checked and $fields is not empty.

Code: Select all

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();
print_r($this->_query->errorInfo());
          }
          else      //an error has occured in SQL query
          { $this->_error = true;
          }
        }
        return $this;
      }
The message I get is:
Array ( [0] => 00000 [1] => [2] => ) Problem creating an account!
I don't how to interpret this error message. Could you please help?

Re: Works on WAMP but not on remote server

Posted: Thu Dec 11, 2014 6:08 pm
by requinix
You can't put the print_r() inside the if because then it will only execute if the query was successful. Put it inside the else.

Code: Select all

          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;
print_r($this->_query->errorInfo());
          }

Re: Works on WAMP but not on remote server

Posted: Thu Dec 11, 2014 6:44 pm
by raydona
Sorry, in my confusion I did the wrong thing and should have written:

Code: Select all

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;
      }  
The message I get is:
Array ( [0] => 00000 [1] => [2] => ) 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?

Re: Works on WAMP but not on remote server

Posted: Fri Dec 12, 2014 12:18 am
by Christopher
So in insert() if you do:

Code: Select all

   $sql = "INSERT INTO {$table} (`".implode('`, `', $keys) . "`) VALUES({$values})";
echo "SQL=$sql<br>";
Does the SQL displayed look valid? If you run that SQL statement directly does it work or give an error?

Re: Works on WAMP but not on remote server

Posted: Mon Dec 15, 2014 8:50 pm
by raydona
Hi,
I have played around with my code. I have changed the code for the function that hashes the password using salt from:

Code: Select all

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

Code: Select all

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. I still get:
Array ( [0] => 42S22 [1] => 1054 [2] => Unknown column 'postcode' in 'field list' ) Problem creating an account!
Can anyone please help.

Re: Works on WAMP but not on remote server

Posted: Mon Dec 15, 2014 10:34 pm
by Christopher
Post the SQL generated and the schemas for the tables on both servers.