0

Is there a way to insert values into multiple databases using db prepare? I use following code to insert in one database:

$insert_stmt = $db->prepare("INSERT INTO database(name) VALUES(:name)");

I would like to insert a value into another database in the same rule, but this does not work, par example:

$insert_stmt = $db->prepare("INSERT INTO database(name) VALUES(:name") INSERT INTO example(street) VALUES (:street)");
  • Nothing wrong with running two separate queries through two separate prepared statements. – El_Vanja Feb 16 '21 at 12:10
  • *but this does not work* Excess dquote and lost delimiter. `$insert_stmt = $db->prepare("INSERT INTO database(name) VALUES(:name); INSERT INTO example(street) VALUES (:street);");`. And ensure that multi-queries are allowed and enabled. PS. Do not confuse the terms "database" and "table". – Akina Feb 16 '21 at 12:13
  • 1
    Are you sure you're using the term "database" correctly? Everything in the question, save for the exact words, imply you mean tables. – Álvaro González Feb 16 '21 at 12:20

1 Answers1

0

you can't use db object name as parameter in sql the only way for to somethings equivalent is so called "dynamic sql" .

sql string builded dynamically

$name =  'my_table_name";
$insert_stmt = $db->prepare("INSERT INTO database" . $name . "(col1, col2, col3)  
                         VALUES(:param1, :param2, :param3) ;";"

anyway be careful using php var in sql .. provide always a proper sanitize for avoid risk of sqlinjection

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107