I have an array like this
$a = array( 'phone' => 111111111, 'image' => "sadasdasd43eadasdad" );
When I do var-dump, I get this β
{ ["phone"]=> int(111111111) ["image"]=> string(19) "sadasdasd43eadasdad" }
Now I'm trying to add this to the database using the IN statement -
$q = $DBH->prepare("INSERT INTO user :column_string VALUES :value_string"); $q->bindParam(':column_string',implode(',',array_keys($a))); $q->bindParam(':value_string',implode(',',array_values($a))); $q->execute();
The problem I am facing is that implode returns a string. But the βtelephoneβ column is an integer in the database, and the array also stores it as an integer. Therefore, I get an SQL error since my last query looks like this:
INSERT INTO user 'phone,image' values '111111111,sadasdasd43eadasdad';
What a wrong request. Is there any way around this.
My column names are dynamically based on what the user wants to insert. Therefore, I cannot use placeholders such as : phone and : image , since I cannot always get values ββfor these two columns. Please let me know if there is a way around this. otherwise, I will have to define several functions for each type of update.
Thanks.
arrays database php mysql pdo
Fox
source share