SQL statement from array

Hi. Ive just started to use php migrating from classic asp. on my webpage I am sending a string of characters in the variable $mask to a python file and sending it back as a dictionary. In php I am taking the first element of the dictionary and converting it back to a string and putting it in the variable $sqltxt.

This section of the code works fine and I can see the string contents and its data type on the webpage. The problem comes when I try to use the string as an SQL statement against the database. It returns an error.

Fatal error : Uncaught Error: Call to a member function fetchall() on boolean in C:\xampp\htdocs\Crossword\test.php:26 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Crossword\test.php on line 26

However if I replace the contents of the sqltxt variable with a copy of the text from the webpage, it works fine! (see line 20). I am at a loss as to what is going wrong, can anyone help?

Thanks in advance.

Why would you be doing this? It makes no sense. What is the real problem you are trying to solve by doing this?

Because im a lot more comfortable with Python than php, Im validating my input string and constructing several complicated SQL statements there. Then Im sending them back in the form of a dictionary. Each item in the dictionary is a different SQL statement for a different purpose. I just want to unpack the items in php and use the statements to retrieve different data sets from the database.

Then focus on learning Php instead of wasting your time with some ridiculous hack.

Explain in detail what you want to do (Not how you want to do it) and we will help you get there.

A high level overview of what you have going on will also be helpful. So far I have this:

  • validate input string
  • construct several complicated SQL statements

I’m not clear… but doesnt fetchAll() return a named/associative array?

try fetch instead perhaps?

Just looked over the code. FetchAll does not belong when you are using the query method.

Because you are json_encoding() this, the resulting value in the php variable actually contains the initial/final double-quotes, because json carries the string data type. These are not part of the sql syntax and are producing an sql error. Why are you json_encoding this?

Next, you ALWAYS need error handling for statements that can fail. For database statements, the simplest way of doing this without adding logic at every statement that can fail is to use exceptions for errors and in most cases let php catch and handle the exception, where php will use its error related settings to control what happens with the actual error information (database statement errors will ‘automatically’ get displayed/logged the same as php errors.) You would be getting some type of sql syntax error near a '" at the start of the sql statement.

Lastly, please post your actual code in the forum. A picture makes it impossible for us to quote any of the code when writing about problems with it.

Thanks for this, I had a similar thought that quotes might be causing the problem as I was falling asleep last night. Removing the json_encode solves the problem. I will take note of your other suggestions and implement them. :slight_smile:

With the code now fixed, the FetchAll seems to do the job, just using Fetch only bring back the first letter of each field in the record. Thanks for taking the time to give input though.

Sponsor our Newsletter | Privacy Policy | Terms of Service