raydona Posted December 11, 2014 Share Posted December 11, 2014 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; } Quote Link to comment Share on other sites More sharing options...
mikosiko Posted December 11, 2014 Share Posted December 11, 2014 Did you check if your $sql contain a valid INSERT sentence? And in your query() function... What are you trying to do in this line If ($this->_query = $this->_pdo->prepare($sql)) a comparison or an assignment? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted December 11, 2014 Share Posted December 11, 2014 it's not hard at all to debug php code. that you know a specific function is returning a false value, means you have narrowed down the problem to a specific section of code. i in fact gave you a specific debugging suggestion in your previous thread, along with the suggestion to actually have proper error handling logic in your code to get it to tell you when, where, and why it is failing - http://forums.phpfreaks.com/topic/292690-call-to-a-member-function-on-a-non-object/?do=findComment&comment=1497936 and here http://forums.phpfreaks.com/topic/292690-call-to-a-member-function-on-a-non-object/?do=findComment&comment=1497716 Quote Link to comment Share on other sites More sharing options...
raydona Posted December 12, 2014 Author Share Posted December 12, 2014 Hello,Many thanks for your help. Did you check if your $sql contain a valid INSERT sentence?And in your query() function... What are you trying to do in this lineIf ($this->_query = $this->_pdo->prepare($sql))a comparison or an assignment? I've checked, $sql does contain a valid INSERT sentence. It's both a comparison and an assignment. Whatever is returned by prepare() is assigned to _query and this is tested I've tried the following debugging. 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? Quote Link to comment Share on other sites More sharing options...
raydona Posted December 12, 2014 Author Share Posted December 12, 2014 Sorry, in my confusion I did the wrong thing and should have written: 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? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted December 12, 2014 Share Posted December 12, 2014 it would take seeing the echo/var_dump() of the $sql statement and seeing the result of a SHOW CREATE TABLE your_table query in order to help you further. you have also implied there may be two separate queries being ran because the debugging output shows two print_r() arrays worth of data - Array ( [0] => 00000 [1] => [2] => ) Array ( [0] => 42S22 [1] => 1054 [2] => Unknown column 'postcode' in 'field list' ). any chance the code is being called twice, the first time it runs a query without error, and the second time, whatever the actual query is, produces an error? and while you are checking the database table definition, make sure you don't have multiple databases and that you are selecting the correct one to run the query against and that you are connecting to the correct database server as well. Quote Link to comment Share on other sites More sharing options...
raydona Posted December 16, 2014 Author Share Posted December 16, 2014 Hi, Thanks for your help. Before I try out some of your suggestions can I ask does it make a difference if the collation of the database is "latin1_swedish_ci" while the collation of the table is "utf8_general_ci"? Also I have just discovered that 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! you have also implied there may be two separate queries being ran because the debugging output shows two print_r() arrays worth of data - Array ( [0] => 00000 [1] => [2] => ) Array ( [0] => 42S22 [1] => 1054 [2] => Unknown column 'postcode' in 'field list' ). any chance the code is being called twice, the first time it runs a query without error, and the second time, whatever the actual query is, produces an error? The error is now showing only one array. I don't know why it showed two previously?? This problem is beyond me!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.