Jump to content

array to string conversion problem


jimfog

Recommended Posts

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

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...