jimfog Posted September 11, 2014 Share Posted September 11, 2014 This post might be beeb as well to the PHP forum.I was not sure(and I am still not) if this is the proper place for this question,anyway... I am trying to insert some data to a table...here is the INSERT statement: $result=$connection->query('insert into appointments.services_list (servicename,price) values ("'. $serve[0].'","'.$price.'")'); When I try to run the above code though I get a notice:array to string conversion problem... This has to do with the $serve[0] variable...it is an array as you can see,and here specifically,in this example contains a string data type-it is comprised of only one element. I do not want to add more elements for now... So the question is why do I get this notice? Link to comment Share on other sites More sharing options...
justsomeguy Posted September 11, 2014 Share Posted September 11, 2014 Either $serve[0] or $price is an array. Link to comment Share on other sites More sharing options...
jimfog Posted September 11, 2014 Author Share Posted September 11, 2014 Either $serve[0] or $price is an array. And what is wrong with that? Why the query/database does not work with arrays? Link to comment Share on other sites More sharing options...
davej Posted September 11, 2014 Share Posted September 11, 2014 What is the value of $serve[0] and $price? Link to comment Share on other sites More sharing options...
justsomeguy Posted September 11, 2014 Share Posted September 11, 2014 You're not sending an array to the database, you're sending a string. The SQL query is a string of text, that is what goes to the database. PHP is just informing you that you are implicitly trying to convert an array to a string, because one of the values that you are using with string concatenation is an array. With that warning, instead of whatever value you're meaning to send, you're just putting the word "Array" into the query. It's the exact same thing that would happen if you did this: echo '$serve[0] is ' . $serve[0] . ' and $price is ' . $price;That's going to do the same thing, it's going to print the word "Array" for one or both of those variables, because that's what happens when you convert an array to a string in PHP. Link to comment Share on other sites More sharing options...
jimfog Posted September 11, 2014 Author Share Posted September 11, 2014 well it seems I found a solution to it...a simple one,I just used this syntax to "take" the string from the array an assign it to a separate variable. $serve=$service[0]; With the above, the query worked. The only thing that remains is how to write the code in case the array contains more than one element....but I will write a different post for that, besides I do not think it would be difficult....a for loop for that matter. Thanks. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 11, 2014 Share Posted September 11, 2014 well it seems I found a solution to it...a simple one,I just used this syntax to "take" the string from the array an assign it to a separate variable.That's literally no different than referring to the value directly in the array. I can't tell what the real problem was without seeing the rest of your code though, where all those variables get set. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now